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
+1 -1
View File
@@ -10,7 +10,7 @@ import (
stdsync "sync"
"time"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/lib/protocol"
)
+10 -14
View File
@@ -18,6 +18,14 @@ import (
"github.com/syncthing/syncthing/lib/protocol"
)
func setupCache() *manager {
cfg := config.New(protocol.LocalDeviceID)
cfg.Options.LocalAnnEnabled = false
cfg.Options.GlobalAnnEnabled = false
return NewManager(protocol.LocalDeviceID, config.Wrap("", cfg, events.NoopLogger), tls.Certificate{}, events.NoopLogger, nil).(*manager)
}
func TestCacheUnique(t *testing.T) {
addresses0 := []string{"tcp://192.0.2.44:22000", "tcp://192.0.2.42:22000"}
addresses1 := []string{"tcp://192.0.2.43:22000", "tcp://192.0.2.42:22000"}
@@ -33,13 +41,7 @@ func TestCacheUnique(t *testing.T) {
"tcp://192.0.2.44:22000",
}
cfg := config.New(protocol.LocalDeviceID)
cfg.Options.LocalAnnEnabled = false
cfg.Options.GlobalAnnEnabled = false
c := NewManager(protocol.LocalDeviceID, config.Wrap("", cfg, events.NoopLogger), tls.Certificate{}, events.NoopLogger, nil).(*manager)
c.ServeBackground()
defer c.Stop()
c := setupCache()
// Add a fake discovery service and verify we get its answers through the
// cache.
@@ -93,13 +95,7 @@ func (f *fakeDiscovery) Cache() map[protocol.DeviceID]CacheEntry {
}
func TestCacheSlowLookup(t *testing.T) {
cfg := config.New(protocol.LocalDeviceID)
cfg.Options.LocalAnnEnabled = false
cfg.Options.GlobalAnnEnabled = false
c := NewManager(protocol.LocalDeviceID, config.Wrap("", cfg, events.NoopLogger), tls.Certificate{}, events.NoopLogger, nil).(*manager)
c.ServeBackground()
defer c.Stop()
c := setupCache()
// Add a slow discovery service.
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"time"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
)
// A Finder provides lookup services of some kind.
+3 -8
View File
@@ -21,16 +21,12 @@ import (
stdsync "sync"
"time"
"github.com/thejerf/suture"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/util"
)
type globalClient struct {
suture.Service
server string
addrList AddressLister
announceClient httpClient
@@ -133,7 +129,6 @@ func NewGlobal(server string, cert tls.Certificate, addrList AddressLister, evLo
noLookup: opts.noLookup,
evLogger: evLogger,
}
cl.Service = util.AsService(cl.serve, cl.String())
if !opts.noAnnounce {
// If we are supposed to annonce, it's an error until we've done so.
cl.setError(errors.New("not announced"))
@@ -193,12 +188,12 @@ func (c *globalClient) String() string {
return "global@" + c.server
}
func (c *globalClient) serve(ctx context.Context) {
func (c *globalClient) Serve(ctx context.Context) error {
if c.noAnnounce {
// We're configured to not do announcements, only lookups. To maintain
// the same interface, we just pause here if Serve() is run.
<-ctx.Done()
return
return ctx.Err()
}
timer := time.NewTimer(5 * time.Second)
@@ -231,7 +226,7 @@ func (c *globalClient) serve(ctx context.Context) {
c.sendAnnouncement(ctx, timer)
case <-ctx.Done():
return
return ctx.Err()
}
}
}
+6 -4
View File
@@ -200,8 +200,9 @@ func TestGlobalAnnounce(t *testing.T) {
t.Fatal(err)
}
go disco.Serve()
defer disco.Stop()
ctx, cancel := context.WithCancel(context.Background())
go disco.Serve(ctx)
defer cancel()
// The discovery thing should attempt an announcement immediately. We wait
// for it to succeed, a while.
@@ -223,8 +224,9 @@ func testLookup(url string) ([]string, error) {
if err != nil {
return nil, err
}
go disco.Serve()
defer disco.Stop()
ctx, cancel := context.WithCancel(context.Background())
go disco.Serve(ctx)
defer cancel()
return disco.Lookup(context.Background(), protocol.LocalDeviceID)
}
+6 -8
View File
@@ -25,7 +25,7 @@ import (
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/rand"
"github.com/syncthing/syncthing/lib/util"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
)
type localClient struct {
@@ -52,9 +52,7 @@ const (
func NewLocal(id protocol.DeviceID, addr string, addrList AddressLister, evLogger events.Logger) (FinderService, error) {
c := &localClient{
Supervisor: suture.New("local", suture.Spec{
PassThroughPanics: true,
}),
Supervisor: suture.New("local", util.Spec()),
myID: id,
addrList: addrList,
evLogger: evLogger,
@@ -137,7 +135,7 @@ func (c *localClient) announcementPkt(instanceID int64, msg []byte) ([]byte, boo
return msg, true
}
func (c *localClient) sendLocalAnnouncements(ctx context.Context) {
func (c *localClient) sendLocalAnnouncements(ctx context.Context) error {
var msg []byte
var ok bool
instanceID := rand.Int63()
@@ -150,18 +148,18 @@ func (c *localClient) sendLocalAnnouncements(ctx context.Context) {
case <-c.localBcastTick:
case <-c.forcedBcastTick:
case <-ctx.Done():
return
return ctx.Err()
}
}
}
func (c *localClient) recvAnnouncements(ctx context.Context) {
func (c *localClient) recvAnnouncements(ctx context.Context) error {
b := c.beacon
warnedAbout := make(map[string]bool)
for {
select {
case <-ctx.Done():
return
return ctx.Err()
default:
}
+4 -2
View File
@@ -8,6 +8,7 @@ package discover
import (
"bytes"
"context"
"net"
"testing"
@@ -20,8 +21,9 @@ func TestLocalInstanceID(t *testing.T) {
if err != nil {
t.Fatal(err)
}
go c.Serve()
defer c.Stop()
ctx, cancel := context.WithCancel(context.Background())
go c.Serve(ctx)
defer cancel()
lc := c.(*localClient)
+9 -8
View File
@@ -13,7 +13,7 @@ import (
"sort"
"time"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/events"
@@ -46,10 +46,8 @@ type manager struct {
}
func NewManager(myID protocol.DeviceID, cfg config.Wrapper, cert tls.Certificate, evLogger events.Logger, lister AddressLister) Manager {
return &manager{
Supervisor: suture.New("discover.Manager", suture.Spec{
PassThroughPanics: true,
}),
m := &manager{
Supervisor: suture.New("discover.Manager", util.Spec()),
myID: myID,
cfg: cfg,
cert: cert,
@@ -59,13 +57,16 @@ func NewManager(myID protocol.DeviceID, cfg config.Wrapper, cert tls.Certificate
finders: make(map[string]cachedFinder),
mut: sync.NewRWMutex(),
}
m.Add(util.AsService(m.serve, m.String()))
return m
}
func (m *manager) Serve() {
func (m *manager) serve(ctx context.Context) error {
m.cfg.Subscribe(m)
defer m.cfg.Unsubscribe(m)
m.CommitConfiguration(config.Configuration{}, m.cfg.RawCopy())
m.Supervisor.Serve()
<-ctx.Done()
m.cfg.Unsubscribe(m)
return nil
}
func (m *manager) addLocked(identity string, finder Finder, cacheTime, negCacheTime time.Duration) {