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
109 lines
2.1 KiB
Go
109 lines
2.1 KiB
Go
package humanize
|
|
|
|
import (
|
|
"bytes"
|
|
"math"
|
|
"math/big"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Comma produces a string form of the given number in base 10 with
|
|
// commas after every three orders of magnitude.
|
|
//
|
|
// e.g. Comma(834142) -> 834,142
|
|
func Comma(v int64) string {
|
|
sign := ""
|
|
|
|
// Min int64 can't be negated to a usable value, so it has to be special cased.
|
|
if v == math.MinInt64 {
|
|
return "-9,223,372,036,854,775,808"
|
|
}
|
|
|
|
if v < 0 {
|
|
sign = "-"
|
|
v = 0 - v
|
|
}
|
|
|
|
parts := []string{"", "", "", "", "", "", ""}
|
|
j := len(parts) - 1
|
|
|
|
for v > 999 {
|
|
parts[j] = strconv.FormatInt(v%1000, 10)
|
|
switch len(parts[j]) {
|
|
case 2:
|
|
parts[j] = "0" + parts[j]
|
|
case 1:
|
|
parts[j] = "00" + parts[j]
|
|
}
|
|
v = v / 1000
|
|
j--
|
|
}
|
|
parts[j] = strconv.Itoa(int(v))
|
|
return sign + strings.Join(parts[j:], ",")
|
|
}
|
|
|
|
// Commaf produces a string form of the given number in base 10 with
|
|
// commas after every three orders of magnitude.
|
|
//
|
|
// e.g. Commaf(834142.32) -> 834,142.32
|
|
func Commaf(v float64) string {
|
|
buf := &bytes.Buffer{}
|
|
if v < 0 {
|
|
buf.Write([]byte{'-'})
|
|
v = 0 - v
|
|
}
|
|
|
|
comma := []byte{','}
|
|
|
|
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
|
|
pos := 0
|
|
if len(parts[0])%3 != 0 {
|
|
pos += len(parts[0]) % 3
|
|
buf.WriteString(parts[0][:pos])
|
|
buf.Write(comma)
|
|
}
|
|
for ; pos < len(parts[0]); pos += 3 {
|
|
buf.WriteString(parts[0][pos : pos+3])
|
|
buf.Write(comma)
|
|
}
|
|
buf.Truncate(buf.Len() - 1)
|
|
|
|
if len(parts) > 1 {
|
|
buf.Write([]byte{'.'})
|
|
buf.WriteString(parts[1])
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
// BigComma produces a string form of the given big.Int in base 10
|
|
// with commas after every three orders of magnitude.
|
|
func BigComma(b *big.Int) string {
|
|
sign := ""
|
|
if b.Sign() < 0 {
|
|
sign = "-"
|
|
b.Abs(b)
|
|
}
|
|
|
|
athousand := big.NewInt(1000)
|
|
c := (&big.Int{}).Set(b)
|
|
_, m := oom(c, athousand)
|
|
parts := make([]string, m+1)
|
|
j := len(parts) - 1
|
|
|
|
mod := &big.Int{}
|
|
for b.Cmp(athousand) >= 0 {
|
|
b.DivMod(b, athousand, mod)
|
|
parts[j] = strconv.FormatInt(mod.Int64(), 10)
|
|
switch len(parts[j]) {
|
|
case 2:
|
|
parts[j] = "0" + parts[j]
|
|
case 1:
|
|
parts[j] = "00" + parts[j]
|
|
}
|
|
j--
|
|
}
|
|
parts[j] = strconv.Itoa(int(b.Int64()))
|
|
return sign + strings.Join(parts[j:], ",")
|
|
}
|