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
+3 -4
View File
@@ -91,8 +91,7 @@ func (t *quicListener) serve(ctx context.Context) error {
svc, conn := stun.New(t.cfg, t, packetConn)
defer func() { _ = conn.Close() }()
go svc.Serve()
defer svc.Stop()
go svc.Serve(ctx)
registry.Register(t.uri.Scheme, conn)
defer registry.Unregister(t.uri.Scheme, conn)
@@ -115,7 +114,7 @@ func (t *quicListener) serve(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
}
@@ -206,7 +205,7 @@ func (f *quicListenerFactory) New(uri *url.URL, cfg config.Wrapper, tlsCfg *tls.
conns: conns,
factory: f,
}
l.ServiceWithError = util.AsServiceWithError(l.serve, l.String())
l.ServiceWithError = util.AsService(l.serve, l.String())
l.nat.Store(stun.NATUnknown)
return l
}
+3 -4
View File
@@ -53,8 +53,7 @@ func (t *relayListener) serve(ctx context.Context) error {
t.mut.Lock()
t.client = clnt
go clnt.Serve()
defer clnt.Stop()
go clnt.Serve(ctx)
t.mut.Unlock()
// Start with nil, so that we send a addresses changed notification as soon as we connect somewhere.
@@ -120,7 +119,7 @@ func (t *relayListener) serve(ctx context.Context) error {
}
case <-ctx.Done():
return nil
return ctx.Err()
}
}
}
@@ -185,7 +184,7 @@ func (f *relayListenerFactory) New(uri *url.URL, cfg config.Wrapper, tlsCfg *tls
conns: conns,
factory: f,
}
t.ServiceWithError = util.AsServiceWithError(t.serve, t.String())
t.ServiceWithError = util.AsService(t.serve, t.String())
return t
}
+20 -20
View File
@@ -31,7 +31,7 @@ import (
_ "github.com/syncthing/syncthing/lib/upnp"
"github.com/pkg/errors"
"github.com/thejerf/suture"
"github.com/thejerf/suture/v4"
"golang.org/x/time/rate"
)
@@ -132,13 +132,12 @@ type service struct {
}
func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, bepProtocolName string, tlsDefaultCommonName string, evLogger events.Logger) Service {
spec := util.Spec()
spec.EventHook = func(e suture.Event) {
l.Infoln(e)
}
service := &service{
Supervisor: suture.New("connections.Service", suture.Spec{
Log: func(line string) {
l.Infoln(line)
},
PassThroughPanics: true,
}),
Supervisor: suture.New("connections.Service", spec),
connectionStatusHandler: newConnectionStatusHandler(),
cfg: cfg,
@@ -162,8 +161,8 @@ func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *t
// due to config are done by removing and adding services, so are
// not subject to these limitations.
listenerSupervisor: suture.New("c.S.listenerSupervisor", suture.Spec{
Log: func(line string) {
l.Infoln(line)
EventHook: func(e suture.Event) {
l.Infoln(e)
},
FailureThreshold: 2,
FailureBackoff: 600 * time.Second,
@@ -189,21 +188,20 @@ func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *t
service.Add(service.listenerSupervisor)
service.Add(service.natService)
util.OnSupervisorDone(service.Supervisor, func() {
service.cfg.Unsubscribe(service.limiter)
service.cfg.Unsubscribe(service)
})
return service
}
func (s *service) Stop() {
s.cfg.Unsubscribe(s.limiter)
s.cfg.Unsubscribe(s)
s.Supervisor.Stop()
}
func (s *service) handle(ctx context.Context) {
func (s *service) handle(ctx context.Context) error {
var c internalConn
for {
select {
case <-ctx.Done():
return
return ctx.Err()
case c = <-s.conns:
}
@@ -338,9 +336,10 @@ func (s *service) handle(ctx context.Context) {
s.model.AddConnection(modelConn, hello)
continue
}
return nil
}
func (s *service) connect(ctx context.Context) {
func (s *service) connect(ctx context.Context) error {
nextDial := make(map[string]time.Time)
// Used as delay for the first few connection attempts, increases
@@ -371,7 +370,7 @@ func (s *service) connect(ctx context.Context) {
for _, deviceCfg := range cfg.Devices {
select {
case <-ctx.Done():
return
return ctx.Err()
default:
}
@@ -503,9 +502,10 @@ func (s *service) connect(ctx context.Context) {
select {
case <-time.After(sleep):
case <-ctx.Done():
return
return ctx.Err()
}
}
return nil
}
func (s *service) isLANHost(host string) bool {
+3 -2
View File
@@ -18,6 +18,8 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/nat"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/thejerf/suture/v4"
)
// Connection is what we expose to the outside. It is a protocol.Connection
@@ -181,8 +183,7 @@ type ListenerAddresses struct {
}
type genericListener interface {
Serve()
Stop()
suture.Service
URI() *url.URL
// A given address can potentially be mutated by the listener.
// For example we bind to tcp://0.0.0.0, but that for example might return
+1 -1
View File
@@ -207,7 +207,7 @@ func (f *tcpListenerFactory) New(uri *url.URL, cfg config.Wrapper, tlsCfg *tls.C
natService: natService,
factory: f,
}
l.ServiceWithError = util.AsServiceWithError(l.serve, l.String())
l.ServiceWithError = util.AsService(l.serve, l.String())
return l
}