all: Implement suture v4-api (#6947)

This commit is contained in:
Simon Frei
2020-11-17 13:19:04 +01:00
committed by GitHub
parent e8fc465ea8
commit 9524b51708
65 changed files with 617 additions and 693 deletions
+7 -4
View File
@@ -7,6 +7,7 @@
package main
import (
"context"
"crypto/rand"
"encoding/binary"
"flag"
@@ -47,14 +48,16 @@ func main() {
log.Println("My ID:", myID)
}
runbeacon(beacon.NewMulticast(mc), fake)
runbeacon(beacon.NewBroadcast(bc), fake)
ctx := context.Background()
runbeacon(ctx, beacon.NewMulticast(mc), fake)
runbeacon(ctx, beacon.NewBroadcast(bc), fake)
select {}
}
func runbeacon(bc beacon.Interface, fake bool) {
go bc.Serve()
func runbeacon(ctx context.Context, bc beacon.Interface, fake bool) {
go bc.Serve(ctx)
go recv(bc)
if fake {
go send(bc)
+6 -4
View File
@@ -66,12 +66,12 @@ func newAPISrv(addr string, cert tls.Certificate, db database, repl replicator,
}
}
func (s *apiSrv) Serve() {
func (s *apiSrv) Serve(ctx context.Context) error {
if s.useHTTP {
listener, err := net.Listen("tcp", s.addr)
if err != nil {
log.Println("Listen:", err)
return
return err
}
s.listener = listener
} else {
@@ -93,7 +93,7 @@ func (s *apiSrv) Serve() {
tlsListener, err := tls.Listen("tcp", s.addr, tlsCfg)
if err != nil {
log.Println("Listen:", err)
return
return err
}
s.listener = tlsListener
}
@@ -107,9 +107,11 @@ func (s *apiSrv) Serve() {
MaxHeaderBytes: httpMaxHeaderBytes,
}
if err := srv.Serve(s.listener); err != nil {
err := srv.Serve(s.listener)
if err != nil {
log.Println("Serve:", err)
}
return err
}
var topCtx = context.Background()
+5 -8
View File
@@ -10,6 +10,7 @@
package main
import (
"context"
"log"
"sort"
"time"
@@ -37,7 +38,6 @@ type database interface {
type levelDBStore struct {
db *leveldb.DB
inbox chan func()
stop chan struct{}
clock clock
marshalBuf []byte
}
@@ -50,7 +50,6 @@ func newLevelDBStore(dir string) (*levelDBStore, error) {
return &levelDBStore{
db: db,
inbox: make(chan func(), 16),
stop: make(chan struct{}),
clock: defaultClock{},
}, nil
}
@@ -155,7 +154,7 @@ func (s *levelDBStore) get(key string) (DatabaseRecord, error) {
return rec, nil
}
func (s *levelDBStore) Serve() {
func (s *levelDBStore) Serve(ctx context.Context) error {
t := time.NewTimer(0)
defer t.Stop()
defer s.db.Close()
@@ -183,7 +182,7 @@ loop:
// the next.
t.Reset(databaseStatisticsInterval)
case <-s.stop:
case <-ctx.Done():
// We're done.
close(statisticsTrigger)
break loop
@@ -192,6 +191,8 @@ loop:
// Also wait for statisticsServe to return
<-statisticsDone
return nil
}
func (s *levelDBStore) statisticsServe(trigger <-chan struct{}, done chan<- struct{}) {
@@ -255,10 +256,6 @@ func (s *levelDBStore) statisticsServe(trigger <-chan struct{}, done chan<- stru
}
}
func (s *levelDBStore) Stop() {
close(s.stop)
}
// merge returns the merged result of the two database records a and b. The
// result is the union of the two address sets, with the newer expiry time
// chosen for any duplicates.
+4 -2
View File
@@ -7,6 +7,7 @@
package main
import (
"context"
"fmt"
"os"
"testing"
@@ -20,8 +21,9 @@ func TestDatabaseGetSet(t *testing.T) {
if err != nil {
t.Fatal(err)
}
go db.Serve()
defer db.Stop()
ctx, cancel := context.WithCancel(context.Background())
go db.Serve(ctx)
defer cancel()
// Check missing record
+3 -2
View File
@@ -7,6 +7,7 @@
package main
import (
"context"
"crypto/tls"
"flag"
"log"
@@ -21,7 +22,7 @@ import (
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/tlsutil"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
)
const (
@@ -183,5 +184,5 @@ func main() {
}
// Engage!
main.Serve()
main.Serve(context.Background())
}
+16 -27
View File
@@ -7,6 +7,7 @@
package main
import (
"context"
"crypto/tls"
"encoding/binary"
"fmt"
@@ -32,7 +33,6 @@ type replicationSender struct {
cert tls.Certificate // our certificate
allowedIDs []protocol.DeviceID
outbox chan ReplicationRecord
stop chan struct{}
}
func newReplicationSender(dst string, cert tls.Certificate, allowedIDs []protocol.DeviceID) *replicationSender {
@@ -41,11 +41,10 @@ func newReplicationSender(dst string, cert tls.Certificate, allowedIDs []protoco
cert: cert,
allowedIDs: allowedIDs,
outbox: make(chan ReplicationRecord, replicationOutboxSize),
stop: make(chan struct{}),
}
}
func (s *replicationSender) Serve() {
func (s *replicationSender) Serve(ctx context.Context) error {
// Sleep a little at startup. Peers often restart at the same time, and
// this avoid the service failing and entering backoff state
// unnecessarily, while also reducing the reconnect rate to something
@@ -62,7 +61,7 @@ func (s *replicationSender) Serve() {
conn, err := tls.Dial("tcp", s.dst, tlsCfg)
if err != nil {
log.Println("Replication connect:", err)
return
return err
}
defer func() {
conn.SetWriteDeadline(time.Now().Add(time.Second))
@@ -73,13 +72,13 @@ func (s *replicationSender) Serve() {
remoteID, err := deviceID(conn)
if err != nil {
log.Println("Replication connect:", err)
return
return err
}
// Verify it's in the set of allowed device IDs.
if !deviceIDIn(remoteID, s.allowedIDs) {
log.Println("Replication connect: unexpected device ID:", remoteID)
return
return err
}
heartBeatTicker := time.NewTicker(replicationHeartbeatInterval)
@@ -122,20 +121,16 @@ func (s *replicationSender) Serve() {
replicationSendsTotal.WithLabelValues("error").Inc()
log.Println("Replication write:", err)
// Yes, we are loosing the replication event here.
return
return err
}
replicationSendsTotal.WithLabelValues("success").Inc()
case <-s.stop:
return
case <-ctx.Done():
return nil
}
}
}
func (s *replicationSender) Stop() {
close(s.stop)
}
func (s *replicationSender) String() string {
return fmt.Sprintf("replicationSender(%q)", s.dst)
}
@@ -172,7 +167,6 @@ type replicationListener struct {
cert tls.Certificate
allowedIDs []protocol.DeviceID
db database
stop chan struct{}
}
func newReplicationListener(addr string, cert tls.Certificate, allowedIDs []protocol.DeviceID, db database) *replicationListener {
@@ -181,11 +175,10 @@ func newReplicationListener(addr string, cert tls.Certificate, allowedIDs []prot
cert: cert,
allowedIDs: allowedIDs,
db: db,
stop: make(chan struct{}),
}
}
func (l *replicationListener) Serve() {
func (l *replicationListener) Serve(ctx context.Context) error {
tlsCfg := &tls.Config{
Certificates: []tls.Certificate{l.cert},
ClientAuth: tls.RequestClientCert,
@@ -196,14 +189,14 @@ func (l *replicationListener) Serve() {
lst, err := tls.Listen("tcp", l.addr, tlsCfg)
if err != nil {
log.Println("Replication listen:", err)
return
return err
}
defer lst.Close()
for {
select {
case <-l.stop:
return
case <-ctx.Done():
return nil
default:
}
@@ -211,7 +204,7 @@ func (l *replicationListener) Serve() {
conn, err := lst.Accept()
if err != nil {
log.Println("Replication accept:", err)
return
return err
}
// Figure out the other side device ID
@@ -231,19 +224,15 @@ func (l *replicationListener) Serve() {
continue
}
go l.handle(conn)
go l.handle(ctx, conn)
}
}
func (l *replicationListener) Stop() {
close(l.stop)
}
func (l *replicationListener) String() string {
return fmt.Sprintf("replicationListener(%q)", l.addr)
}
func (l *replicationListener) handle(conn net.Conn) {
func (l *replicationListener) handle(ctx context.Context, conn net.Conn) {
defer func() {
conn.SetWriteDeadline(time.Now().Add(time.Second))
conn.Close()
@@ -253,7 +242,7 @@ func (l *replicationListener) handle(conn net.Conn) {
for {
select {
case <-l.stop:
case <-ctx.Done():
return
default:
}
+4 -1
View File
@@ -3,6 +3,7 @@
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
@@ -194,7 +195,9 @@ func main() {
mapping := mapping{natSvc.NewMapping(nat.TCP, addr.IP, addr.Port)}
if natEnabled {
go natSvc.Serve()
ctx, cancel := context.WithCancel(context.Background())
go natSvc.Serve(ctx)
defer cancel()
found := make(chan struct{})
mapping.OnChanged(func(_ *nat.Mapping, _, _ []nat.Address) {
select {
+3 -2
View File
@@ -20,7 +20,8 @@ import (
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
@@ -62,7 +63,7 @@ func main() {
}
log.Println("Created client")
go relay.Serve()
go relay.Serve(ctx)
recv := make(chan protocol.SessionInvitation)
+30 -23
View File
@@ -8,6 +8,7 @@ package main
import (
"bytes"
"context"
"crypto/tls"
"flag"
"fmt"
@@ -41,6 +42,7 @@ import (
"github.com/syncthing/syncthing/lib/syncthing"
"github.com/syncthing/syncthing/lib/tlsutil"
"github.com/syncthing/syncthing/lib/upgrade"
"github.com/syncthing/syncthing/lib/util"
"github.com/pkg/errors"
)
@@ -321,7 +323,7 @@ func main() {
}
if err != nil {
l.Warnln("Command line options:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
if options.logFile == "default" || options.logFile == "" {
@@ -358,7 +360,7 @@ func main() {
)
if err != nil {
l.Warnln("Error reading device ID:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
fmt.Println(protocol.NewDeviceID(cert.Certificate[0]))
@@ -368,7 +370,7 @@ func main() {
if options.browserOnly {
if err := openGUI(protocol.EmptyDeviceID); err != nil {
l.Warnln("Failed to open web UI:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
return
}
@@ -376,7 +378,7 @@ func main() {
if options.generateDir != "" {
if err := generate(options.generateDir); err != nil {
l.Warnln("Failed to generate config and keys:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
return
}
@@ -384,14 +386,14 @@ func main() {
// Ensure that our home directory exists.
if err := ensureDir(locations.GetBaseDir(locations.ConfigBaseDir), 0700); err != nil {
l.Warnln("Failure on home directory:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
if options.upgradeTo != "" {
err := upgrade.ToURL(options.upgradeTo)
if err != nil {
l.Warnln("Error while Upgrading:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
l.Infoln("Upgraded from", options.upgradeTo)
return
@@ -422,13 +424,13 @@ func main() {
os.Exit(exitCodeForUpgrade(err))
}
l.Infof("Upgraded to %q", release.Tag)
os.Exit(syncthing.ExitUpgrade.AsInt())
os.Exit(util.ExitUpgrade.AsInt())
}
if options.resetDatabase {
if err := resetDB(); err != nil {
l.Warnln("Resetting database:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
l.Infoln("Successfully reset database - it will be rebuilt after next start.")
return
@@ -601,13 +603,14 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
}
evLogger := events.NewLogger()
go evLogger.Serve()
defer evLogger.Stop()
ctx, cancel := context.WithCancel(context.Background())
go evLogger.Serve(ctx)
defer cancel()
cfg, err := syncthing.LoadConfigAtStartup(locations.Get(locations.ConfigFile), cert, evLogger, runtimeOptions.allowNewerConfig, noDefaultFolder)
if err != nil {
l.Warnln("Failed to initialize config:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
// Candidate builds should auto upgrade. Make sure the option is set,
@@ -653,7 +656,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
}
} else {
l.Infof("Upgraded to %q, exiting now.", release.Tag)
os.Exit(syncthing.ExitUpgrade.AsInt())
os.Exit(util.ExitUpgrade.AsInt())
}
}
@@ -694,18 +697,18 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid()))
if err != nil {
l.Warnln("Creating profile:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
if err := pprof.StartCPUProfile(f); err != nil {
l.Warnln("Starting profile:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
}
go standbyMonitor(app, cfg)
if err := app.Start(); err != nil {
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
cleanConfigDirectory()
@@ -718,6 +721,10 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
status := app.Wait()
if status == util.ExitError {
l.Warnln("Syncthing stopped with error:", app.Error())
}
if runtimeOptions.cpuProfile {
pprof.StopCPUProfile()
}
@@ -733,7 +740,7 @@ func setupSignalHandling(app *syncthing.App) {
signal.Notify(restartSign, sigHup)
go func() {
<-restartSign
app.Stop(syncthing.ExitRestart)
app.Stop(util.ExitRestart)
}()
// Exit with "success" code (no restart) on INT/TERM
@@ -742,7 +749,7 @@ func setupSignalHandling(app *syncthing.App) {
signal.Notify(stopSign, os.Interrupt, sigTerm)
go func() {
<-stopSign
app.Stop(syncthing.ExitSuccess)
app.Stop(util.ExitSuccess)
}()
}
@@ -779,7 +786,7 @@ func auditWriter(auditFile string) io.Writer {
fd, err = os.OpenFile(auditFile, auditFlags, 0600)
if err != nil {
l.Warnln("Audit:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
auditDest = auditFile
}
@@ -829,7 +836,7 @@ func standbyMonitor(app *syncthing.App, cfg config.Wrapper) {
// things a moment to stabilize.
time.Sleep(restartDelay)
app.Stop(syncthing.ExitRestart)
app.Stop(util.ExitRestart)
return
}
now = time.Now()
@@ -899,7 +906,7 @@ func autoUpgrade(cfg config.Wrapper, app *syncthing.App, evLogger events.Logger)
sub.Unsubscribe()
l.Warnf("Automatically upgraded to version %q. Restarting in 1 minute.", rel.Tag)
time.Sleep(time.Minute)
app.Stop(syncthing.ExitUpgrade)
app.Stop(util.ExitUpgrade)
return
}
}
@@ -987,13 +994,13 @@ func setPauseState(cfg config.Wrapper, paused bool) {
}
if _, err := cfg.Replace(raw); err != nil {
l.Warnln("Cannot adjust paused state:", err)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
}
func exitCodeForUpgrade(err error) int {
if _, ok := err.(*errNoUpgrade); ok {
return syncthing.ExitNoUpgradeAvailable.AsInt()
return util.ExitNoUpgradeAvailable.AsInt()
}
return syncthing.ExitError.AsInt()
return util.ExitError.AsInt()
}
+5 -5
View File
@@ -26,7 +26,7 @@ import (
"github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/sync"
"github.com/syncthing/syncthing/lib/syncthing"
"github.com/syncthing/syncthing/lib/util"
)
var (
@@ -99,7 +99,7 @@ func monitorMain(runtimeOptions RuntimeOptions) {
if t := time.Since(restarts[0]); t < loopThreshold {
l.Warnf("%d restarts in %v; not retrying further", countRestarts, t)
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
copy(restarts[0:], restarts[1:])
@@ -169,7 +169,7 @@ func monitorMain(runtimeOptions RuntimeOptions) {
if err == nil {
// Successful exit indicates an intentional shutdown
os.Exit(syncthing.ExitSuccess.AsInt())
os.Exit(util.ExitSuccess.AsInt())
}
if exiterr, ok := err.(*exec.ExitError); ok {
@@ -177,7 +177,7 @@ func monitorMain(runtimeOptions RuntimeOptions) {
if stopped || runtimeOptions.noRestart {
os.Exit(exitCode)
}
if exitCode == syncthing.ExitUpgrade.AsInt() {
if exitCode == util.ExitUpgrade.AsInt() {
// Restart the monitor process to release the .old
// binary as part of the upgrade process.
l.Infoln("Restarting monitor...")
@@ -189,7 +189,7 @@ func monitorMain(runtimeOptions RuntimeOptions) {
}
if runtimeOptions.noRestart {
os.Exit(syncthing.ExitError.AsInt())
os.Exit(util.ExitError.AsInt())
}
l.Infoln("Syncthing exited:", err)