mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-09 09:50:30 +00:00
916ec63af6
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
132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
// Command toml-test-encoder satisfies the toml-test interface for testing
|
|
// TOML encoders. Namely, it accepts JSON on stdin and outputs TOML on stdout.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
func init() {
|
|
log.SetFlags(0)
|
|
|
|
flag.Usage = usage
|
|
flag.Parse()
|
|
}
|
|
|
|
func usage() {
|
|
log.Printf("Usage: %s < json-file\n", path.Base(os.Args[0]))
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
}
|
|
|
|
func main() {
|
|
if flag.NArg() != 0 {
|
|
flag.Usage()
|
|
}
|
|
|
|
var tmp interface{}
|
|
if err := json.NewDecoder(os.Stdin).Decode(&tmp); err != nil {
|
|
log.Fatalf("Error decoding JSON: %s", err)
|
|
}
|
|
|
|
tomlData := translate(tmp)
|
|
if err := toml.NewEncoder(os.Stdout).Encode(tomlData); err != nil {
|
|
log.Fatalf("Error encoding TOML: %s", err)
|
|
}
|
|
}
|
|
|
|
func translate(typedJson interface{}) interface{} {
|
|
switch v := typedJson.(type) {
|
|
case map[string]interface{}:
|
|
if len(v) == 2 && in("type", v) && in("value", v) {
|
|
return untag(v)
|
|
}
|
|
m := make(map[string]interface{}, len(v))
|
|
for k, v2 := range v {
|
|
m[k] = translate(v2)
|
|
}
|
|
return m
|
|
case []interface{}:
|
|
tabArray := make([]map[string]interface{}, len(v))
|
|
for i := range v {
|
|
if m, ok := translate(v[i]).(map[string]interface{}); ok {
|
|
tabArray[i] = m
|
|
} else {
|
|
log.Fatalf("JSON arrays may only contain objects. This " +
|
|
"corresponds to only tables being allowed in " +
|
|
"TOML table arrays.")
|
|
}
|
|
}
|
|
return tabArray
|
|
}
|
|
log.Fatalf("Unrecognized JSON format '%T'.", typedJson)
|
|
panic("unreachable")
|
|
}
|
|
|
|
func untag(typed map[string]interface{}) interface{} {
|
|
t := typed["type"].(string)
|
|
v := typed["value"]
|
|
switch t {
|
|
case "string":
|
|
return v.(string)
|
|
case "integer":
|
|
v := v.(string)
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
log.Fatalf("Could not parse '%s' as integer: %s", v, err)
|
|
}
|
|
return n
|
|
case "float":
|
|
v := v.(string)
|
|
f, err := strconv.ParseFloat(v, 64)
|
|
if err != nil {
|
|
log.Fatalf("Could not parse '%s' as float64: %s", v, err)
|
|
}
|
|
return f
|
|
case "datetime":
|
|
v := v.(string)
|
|
t, err := time.Parse("2006-01-02T15:04:05Z", v)
|
|
if err != nil {
|
|
log.Fatalf("Could not parse '%s' as a datetime: %s", v, err)
|
|
}
|
|
return t
|
|
case "bool":
|
|
v := v.(string)
|
|
switch v {
|
|
case "true":
|
|
return true
|
|
case "false":
|
|
return false
|
|
}
|
|
log.Fatalf("Could not parse '%s' as a boolean.", v)
|
|
case "array":
|
|
v := v.([]interface{})
|
|
array := make([]interface{}, len(v))
|
|
for i := range v {
|
|
if m, ok := v[i].(map[string]interface{}); ok {
|
|
array[i] = untag(m)
|
|
} else {
|
|
log.Fatalf("Arrays may only contain other arrays or "+
|
|
"primitive values, but found a '%T'.", m)
|
|
}
|
|
}
|
|
return array
|
|
}
|
|
log.Fatalf("Unrecognized tag type '%s'.", t)
|
|
panic("unreachable")
|
|
}
|
|
|
|
func in(key string, m map[string]interface{}) bool {
|
|
_, ok := m[key]
|
|
return ok
|
|
}
|