syncthing/vendor/github.com/gernest/wow/magefile.go
Jakob Borg 916ec63af6 cmd/stdiscosrv: New discovery server (fixes #4618)
This is a new revision of the discovery server. Relevant changes and
non-changes:

- Protocol towards clients is unchanged.

- Recommended large scale design is still to be deployed nehind nginx (I
  tested, and it's still a lot faster at terminating TLS).

- Database backend is leveldb again, only. It scales enough, is easy to
  setup, and we don't need any backend to take care of.

- Server supports replication. This is a simple TCP channel - protect it
  with a firewall when deploying over the internet. (We deploy this within
  the same datacenter, and with firewall.) Any incoming client announces
  are sent over the replication channel(s) to other peer discosrvs.
  Incoming replication changes are applied to the database as if they came
  from clients, but without the TLS/certificate overhead.

- Metrics are exposed using the prometheus library, when enabled.

- The database values and replication protocol is protobuf, because JSON
  was quite CPU intensive when I tried that and benchmarked it.

- The "Retry-After" value for failed lookups gets slowly increased from
  a default of 120 seconds, by 5 seconds for each failed lookup,
  independently by each discosrv. This lowers the query load over time for
  clients that are never seen. The Retry-After maxes out at 3600 after a
  couple of weeks of this increase. The number of failed lookups is
  stored in the database, now and then (avoiding making each lookup a
  database put).

All in all this means clients can be pointed towards a cluster using
just multiple A / AAAA records to gain both load sharing and redundancy
(if one is down, clients will talk to the remaining ones).

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4648
2018-01-14 08:52:31 +00:00

229 lines
4.2 KiB
Go

// +build mage
package main
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/mg"
)
// Spinners generates easy/accessible Go types for spinners from
// cli-spinners/spinners.json.
func Spinners() error {
pkg := "spin"
mg.Deps(func() error {
_, err := os.Stat(pkg)
if err != nil {
if os.IsNotExist(err) {
return os.Mkdir(pkg, 0777)
}
return err
}
return nil
})
b, err := ioutil.ReadFile("cli-spinners/spinners.json")
if err != nil {
return err
}
o := make(map[string]interface{})
err = json.Unmarshal(b, &o)
if err != nil {
return err
}
tpl, err := template.New("spinner").Funcs(helpers()).Parse(spinnersTpl)
if err != nil {
return err
}
var buf bytes.Buffer
err = tpl.Execute(&buf, o)
if err != nil {
return err
}
bo, err := format.Source(buf.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(pkg, "spinners.go"), bo, 0600)
}
// ExampleAll generates example executable file to demo all spinners
func ExampleAll() error {
b, err := ioutil.ReadFile("cli-spinners/spinners.json")
if err != nil {
return err
}
o := make(map[string]interface{})
err = json.Unmarshal(b, &o)
if err != nil {
return err
}
// Generate example/all/main.go
tpl, err := template.New("example-all").Funcs(helpers()).Parse(exampleAllTpl)
if err != nil {
return err
}
var buf bytes.Buffer
err = tpl.Execute(&buf, o)
if err != nil {
return err
}
bo, err := format.Source(buf.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile("example/all/main.go", bo, 0600)
}
func helpers() template.FuncMap {
return template.FuncMap{
"title": strings.Title,
"stringify": func(a []interface{}) string {
o := ""
switch len(a) {
case 0:
return ""
case 1:
return fmt.Sprintf("\"%s\"", a[0])
default:
for k, v := range a {
if k == 0 {
o += fmt.Sprintf("`%s`", v)
} else {
if v == "`" {
o += fmt.Sprintf(",\"%s\"", v)
} else {
o += fmt.Sprintf(",`%s`", v)
}
}
}
return fmt.Sprintf("[]string{ %v }", o)
}
},
"all": func(a map[string]interface{}) string {
o := ""
for k := range a {
if o == "" {
o += fmt.Sprintf("%s", strings.Title(k))
} else {
o += fmt.Sprintf(",%s", strings.Title(k))
}
}
return fmt.Sprintf("[]Name{%s}", o)
},
"allwithpkg": func(a map[string]interface{}) string {
o := ""
for k := range a {
if o == "" {
o += fmt.Sprintf("spin.%s", strings.Title(k))
} else {
o += fmt.Sprintf(",spin.%s", strings.Title(k))
}
}
return fmt.Sprintf("[]spin.Name{%s}", o)
},
}
}
const spinnersTpl = `//DO NOT EDIT : this file was automatically generated.
package spin
// Spinner defines a spinner widget
type Spinner struct{
Name Name
Interval int
Frames []string
}
// Name represents a name for a spinner item.
type Name uint
// available spinners
const(
Unknown Name=iota
{{range $k,$v:= .}}
{{- $k|title}}
{{end}}
)
func (s Name)String()string{
switch s{
{{- range $k,$v:=.}}
case {{$k|title}} :
return "{{$k}}"
{{- end}}
default:
return ""
}
}
func Get( name Name)Spinner{
switch name{
{{- range $k,$v:=.}}
case {{$k|title}} :
return Spinner{
Name: {{$k|title}},
Interval: {{$v.interval}},
Frames: {{$v.frames|stringify }},
}
{{- end}}
default:
return Spinner{}
}
}
`
const exampleAllTpl = `//DO NOT EDIT : this file was automatically generated.
package main
import (
"os"
"time"
"github.com/gernest/wow"
"github.com/gernest/wow/spin"
)
var all = {{allwithpkg .}}
func main() {
for _, v := range all {
w := wow.New(os.Stdout, spin.Get(v), " "+v.String())
w.Start()
time.Sleep(2)
w.Persist()
}
}
`
// Update updates cli-spinners to get latest changes to the spinners.json file.
func Update() error {
return sh.Run("git", "submodule", "update", "--remote", "cli-spinners")
}
//Setup prepares the project for local development.
//
// This runs git submodule init && git submodule update
func Setup() error {
err := sh.Run("git", "submodule", "init")
if err != nil {
return err
}
return sh.Run("git", "submodule", "update")
}