diff --git a/cmd/strelaysrv/main.go b/cmd/strelaysrv/main.go index 0d914e23f..09876079e 100644 --- a/cmd/strelaysrv/main.go +++ b/cmd/strelaysrv/main.go @@ -186,6 +186,7 @@ func main() { } wrapper := config.Wrap("config", config.New(id), id, events.NoopLogger) + go wrapper.Serve(context.TODO()) wrapper.Modify(func(cfg *config.Configuration) { cfg.Options.NATLeaseM = natLease cfg.Options.NATRenewalM = natRenewal diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index f63ec3cc6..d29ab2723 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -603,9 +603,7 @@ func syncthingMain(options serveOptions) { l.Warnln("Failed to initialize config:", err) os.Exit(svcutil.ExitError.AsInt()) } - if cfgService, ok := cfgWrapper.(suture.Service); ok { - earlyService.Add(cfgService) - } + earlyService.Add(cfgWrapper) // Candidate builds should auto upgrade. Make sure the option is set, // unless we are in a build where it's disabled or the STNOUPGRADE diff --git a/lib/api/api_test.go b/lib/api/api_test.go index 2aab8308f..c20296a7d 100644 --- a/lib/api/api_test.go +++ b/lib/api/api_test.go @@ -1262,11 +1262,9 @@ func TestConfigChanges(t *testing.T) { defer os.Remove(tmpFile.Name()) w := config.Wrap(tmpFile.Name(), cfg, protocol.LocalDeviceID, events.NoopLogger) tmpFile.Close() - if cfgService, ok := w.(suture.Service); ok { - cfgCtx, cfgCancel := context.WithCancel(context.Background()) - go cfgService.Serve(cfgCtx) - defer cfgCancel() - } + cfgCtx, cfgCancel := context.WithCancel(context.Background()) + go w.Serve(cfgCtx) + defer cfgCancel() baseURL, cancel, err := startHTTP(w) if err != nil { t.Fatal("Unexpected error from getting base URL:", err) diff --git a/lib/config/config_test.go b/lib/config/config_test.go index 1228e7174..d253cdcff 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -23,7 +23,6 @@ import ( "testing" "github.com/d4l3k/messagediff" - "github.com/thejerf/suture/v4" "github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/fs" @@ -1301,16 +1300,10 @@ func startWrapper(wrapper Wrapper) *testWrapper { Wrapper: wrapper, done: make(chan struct{}), } - s, ok := wrapper.(suture.Service) - if !ok { - tw.cancel = func() {} - close(tw.done) - return tw - } ctx, cancel := context.WithCancel(context.Background()) tw.cancel = cancel go func() { - s.Serve(ctx) + wrapper.Serve(ctx) close(tw.done) }() return tw diff --git a/lib/config/mocks/mocked_wrapper.go b/lib/config/mocks/mocked_wrapper.go index 910f439f0..8cc4029ad 100644 --- a/lib/config/mocks/mocked_wrapper.go +++ b/lib/config/mocks/mocked_wrapper.go @@ -2,6 +2,7 @@ package mocks import ( + "context" "sync" "github.com/syncthing/syncthing/lib/config" @@ -258,6 +259,17 @@ type Wrapper struct { saveReturnsOnCall map[int]struct { result1 error } + ServeStub func(context.Context) error + serveMutex sync.RWMutex + serveArgsForCall []struct { + arg1 context.Context + } + serveReturns struct { + result1 error + } + serveReturnsOnCall map[int]struct { + result1 error + } SubscribeStub func(config.Committer) config.Configuration subscribeMutex sync.RWMutex subscribeArgsForCall []struct { @@ -1577,6 +1589,67 @@ func (fake *Wrapper) SaveReturnsOnCall(i int, result1 error) { }{result1} } +func (fake *Wrapper) Serve(arg1 context.Context) error { + fake.serveMutex.Lock() + ret, specificReturn := fake.serveReturnsOnCall[len(fake.serveArgsForCall)] + fake.serveArgsForCall = append(fake.serveArgsForCall, struct { + arg1 context.Context + }{arg1}) + stub := fake.ServeStub + fakeReturns := fake.serveReturns + fake.recordInvocation("Serve", []interface{}{arg1}) + fake.serveMutex.Unlock() + if stub != nil { + return stub(arg1) + } + if specificReturn { + return ret.result1 + } + return fakeReturns.result1 +} + +func (fake *Wrapper) ServeCallCount() int { + fake.serveMutex.RLock() + defer fake.serveMutex.RUnlock() + return len(fake.serveArgsForCall) +} + +func (fake *Wrapper) ServeCalls(stub func(context.Context) error) { + fake.serveMutex.Lock() + defer fake.serveMutex.Unlock() + fake.ServeStub = stub +} + +func (fake *Wrapper) ServeArgsForCall(i int) context.Context { + fake.serveMutex.RLock() + defer fake.serveMutex.RUnlock() + argsForCall := fake.serveArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *Wrapper) ServeReturns(result1 error) { + fake.serveMutex.Lock() + defer fake.serveMutex.Unlock() + fake.ServeStub = nil + fake.serveReturns = struct { + result1 error + }{result1} +} + +func (fake *Wrapper) ServeReturnsOnCall(i int, result1 error) { + fake.serveMutex.Lock() + defer fake.serveMutex.Unlock() + fake.ServeStub = nil + if fake.serveReturnsOnCall == nil { + fake.serveReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.serveReturnsOnCall[i] = struct { + result1 error + }{result1} +} + func (fake *Wrapper) Subscribe(arg1 config.Committer) config.Configuration { fake.subscribeMutex.Lock() ret, specificReturn := fake.subscribeReturnsOnCall[len(fake.subscribeArgsForCall)] @@ -1719,6 +1792,8 @@ func (fake *Wrapper) Invocations() map[string][][]interface{} { defer fake.requiresRestartMutex.RUnlock() fake.saveMutex.RLock() defer fake.saveMutex.RUnlock() + fake.serveMutex.RLock() + defer fake.serveMutex.RUnlock() fake.subscribeMutex.RLock() defer fake.subscribeMutex.RUnlock() fake.unsubscribeMutex.RLock() diff --git a/lib/config/wrapper.go b/lib/config/wrapper.go index b17468173..356e15d95 100644 --- a/lib/config/wrapper.go +++ b/lib/config/wrapper.go @@ -20,6 +20,7 @@ import ( "github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/sync" + "github.com/thejerf/suture/v4" ) const ( @@ -111,6 +112,8 @@ type Wrapper interface { Subscribe(c Committer) Configuration Unsubscribe(c Committer) + + suture.Service } type wrapper struct { diff --git a/lib/connections/limiter_test.go b/lib/connections/limiter_test.go index dafdbb905..caf6bf201 100644 --- a/lib/connections/limiter_test.go +++ b/lib/connections/limiter_test.go @@ -17,7 +17,6 @@ import ( "github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/protocol" - "github.com/thejerf/suture/v4" "golang.org/x/time/rate" ) @@ -45,12 +44,8 @@ func initConfig() (config.Wrapper, context.CancelFunc) { dev3Conf = newDeviceConfiguration(wrapper, device3, "device3") dev4Conf = newDeviceConfiguration(wrapper, device4, "device4") - var cancel context.CancelFunc = func() {} - if wrapperService, ok := wrapper.(suture.Service); ok { - var ctx context.Context - ctx, cancel = context.WithCancel(context.Background()) - go wrapperService.Serve(ctx) - } + ctx, cancel := context.WithCancel(context.Background()) + go wrapper.Serve(ctx) dev2Conf.MaxRecvKbps = rand.Int() % 100000 dev2Conf.MaxSendKbps = rand.Int() % 100000 diff --git a/lib/model/testutils_test.go b/lib/model/testutils_test.go index c9c2fff4a..edd4aaeca 100644 --- a/lib/model/testutils_test.go +++ b/lib/model/testutils_test.go @@ -13,8 +13,6 @@ import ( "testing" "time" - "github.com/thejerf/suture/v4" - "github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/db" "github.com/syncthing/syncthing/lib/db/backend" @@ -84,12 +82,9 @@ func createTmpWrapper(cfg config.Configuration) (config.Wrapper, context.CancelF } wrapper := config.Wrap(tmpFile.Name(), cfg, myID, events.NoopLogger) tmpFile.Close() - if cfgService, ok := wrapper.(suture.Service); ok { - ctx, cancel := context.WithCancel(context.Background()) - go cfgService.Serve(ctx) - return wrapper, cancel - } - return wrapper, func() {} + ctx, cancel := context.WithCancel(context.Background()) + go wrapper.Serve(ctx) + return wrapper, cancel } func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {