all: Make config.Wrapper an actual suture.Service (fixes #7451) (#7452)

This commit is contained in:
Jakob Borg 2021-03-11 14:51:00 +01:00 committed by GitHub
parent df08984a58
commit cdef503db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 89 additions and 31 deletions

View File

@ -186,6 +186,7 @@ func main() {
} }
wrapper := config.Wrap("config", config.New(id), id, events.NoopLogger) wrapper := config.Wrap("config", config.New(id), id, events.NoopLogger)
go wrapper.Serve(context.TODO())
wrapper.Modify(func(cfg *config.Configuration) { wrapper.Modify(func(cfg *config.Configuration) {
cfg.Options.NATLeaseM = natLease cfg.Options.NATLeaseM = natLease
cfg.Options.NATRenewalM = natRenewal cfg.Options.NATRenewalM = natRenewal

View File

@ -603,9 +603,7 @@ func syncthingMain(options serveOptions) {
l.Warnln("Failed to initialize config:", err) l.Warnln("Failed to initialize config:", err)
os.Exit(svcutil.ExitError.AsInt()) os.Exit(svcutil.ExitError.AsInt())
} }
if cfgService, ok := cfgWrapper.(suture.Service); ok { earlyService.Add(cfgWrapper)
earlyService.Add(cfgService)
}
// Candidate builds should auto upgrade. Make sure the option is set, // Candidate builds should auto upgrade. Make sure the option is set,
// unless we are in a build where it's disabled or the STNOUPGRADE // unless we are in a build where it's disabled or the STNOUPGRADE

View File

@ -1262,11 +1262,9 @@ func TestConfigChanges(t *testing.T) {
defer os.Remove(tmpFile.Name()) defer os.Remove(tmpFile.Name())
w := config.Wrap(tmpFile.Name(), cfg, protocol.LocalDeviceID, events.NoopLogger) w := config.Wrap(tmpFile.Name(), cfg, protocol.LocalDeviceID, events.NoopLogger)
tmpFile.Close() tmpFile.Close()
if cfgService, ok := w.(suture.Service); ok { cfgCtx, cfgCancel := context.WithCancel(context.Background())
cfgCtx, cfgCancel := context.WithCancel(context.Background()) go w.Serve(cfgCtx)
go cfgService.Serve(cfgCtx) defer cfgCancel()
defer cfgCancel()
}
baseURL, cancel, err := startHTTP(w) baseURL, cancel, err := startHTTP(w)
if err != nil { if err != nil {
t.Fatal("Unexpected error from getting base URL:", err) t.Fatal("Unexpected error from getting base URL:", err)

View File

@ -23,7 +23,6 @@ import (
"testing" "testing"
"github.com/d4l3k/messagediff" "github.com/d4l3k/messagediff"
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs" "github.com/syncthing/syncthing/lib/fs"
@ -1301,16 +1300,10 @@ func startWrapper(wrapper Wrapper) *testWrapper {
Wrapper: wrapper, Wrapper: wrapper,
done: make(chan struct{}), 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()) ctx, cancel := context.WithCancel(context.Background())
tw.cancel = cancel tw.cancel = cancel
go func() { go func() {
s.Serve(ctx) wrapper.Serve(ctx)
close(tw.done) close(tw.done)
}() }()
return tw return tw

View File

@ -2,6 +2,7 @@
package mocks package mocks
import ( import (
"context"
"sync" "sync"
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
@ -258,6 +259,17 @@ type Wrapper struct {
saveReturnsOnCall map[int]struct { saveReturnsOnCall map[int]struct {
result1 error 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 SubscribeStub func(config.Committer) config.Configuration
subscribeMutex sync.RWMutex subscribeMutex sync.RWMutex
subscribeArgsForCall []struct { subscribeArgsForCall []struct {
@ -1577,6 +1589,67 @@ func (fake *Wrapper) SaveReturnsOnCall(i int, result1 error) {
}{result1} }{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 { func (fake *Wrapper) Subscribe(arg1 config.Committer) config.Configuration {
fake.subscribeMutex.Lock() fake.subscribeMutex.Lock()
ret, specificReturn := fake.subscribeReturnsOnCall[len(fake.subscribeArgsForCall)] ret, specificReturn := fake.subscribeReturnsOnCall[len(fake.subscribeArgsForCall)]
@ -1719,6 +1792,8 @@ func (fake *Wrapper) Invocations() map[string][][]interface{} {
defer fake.requiresRestartMutex.RUnlock() defer fake.requiresRestartMutex.RUnlock()
fake.saveMutex.RLock() fake.saveMutex.RLock()
defer fake.saveMutex.RUnlock() defer fake.saveMutex.RUnlock()
fake.serveMutex.RLock()
defer fake.serveMutex.RUnlock()
fake.subscribeMutex.RLock() fake.subscribeMutex.RLock()
defer fake.subscribeMutex.RUnlock() defer fake.subscribeMutex.RUnlock()
fake.unsubscribeMutex.RLock() fake.unsubscribeMutex.RLock()

View File

@ -20,6 +20,7 @@ import (
"github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/sync" "github.com/syncthing/syncthing/lib/sync"
"github.com/thejerf/suture/v4"
) )
const ( const (
@ -111,6 +112,8 @@ type Wrapper interface {
Subscribe(c Committer) Configuration Subscribe(c Committer) Configuration
Unsubscribe(c Committer) Unsubscribe(c Committer)
suture.Service
} }
type wrapper struct { type wrapper struct {

View File

@ -17,7 +17,6 @@ import (
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/protocol"
"github.com/thejerf/suture/v4"
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
@ -45,12 +44,8 @@ func initConfig() (config.Wrapper, context.CancelFunc) {
dev3Conf = newDeviceConfiguration(wrapper, device3, "device3") dev3Conf = newDeviceConfiguration(wrapper, device3, "device3")
dev4Conf = newDeviceConfiguration(wrapper, device4, "device4") dev4Conf = newDeviceConfiguration(wrapper, device4, "device4")
var cancel context.CancelFunc = func() {} ctx, cancel := context.WithCancel(context.Background())
if wrapperService, ok := wrapper.(suture.Service); ok { go wrapper.Serve(ctx)
var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())
go wrapperService.Serve(ctx)
}
dev2Conf.MaxRecvKbps = rand.Int() % 100000 dev2Conf.MaxRecvKbps = rand.Int() % 100000
dev2Conf.MaxSendKbps = rand.Int() % 100000 dev2Conf.MaxSendKbps = rand.Int() % 100000

View File

@ -13,8 +13,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db" "github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend" "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) wrapper := config.Wrap(tmpFile.Name(), cfg, myID, events.NoopLogger)
tmpFile.Close() tmpFile.Close()
if cfgService, ok := wrapper.(suture.Service); ok { ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.Background()) go wrapper.Serve(ctx)
go cfgService.Serve(ctx) return wrapper, cancel
return wrapper, cancel
}
return wrapper, func() {}
} }
func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) { func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {