all: Implement suture v4-api (#6947)
This commit is contained in:
+20
-29
@@ -12,7 +12,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/thejerf/suture"
|
||||
"github.com/thejerf/suture/v4"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
)
|
||||
@@ -44,24 +44,25 @@ type cast struct {
|
||||
// caller needs to set reader and writer with the addReader and addWriter
|
||||
// methods to get a functional implementation of Interface.
|
||||
func newCast(name string) *cast {
|
||||
return &cast{
|
||||
Supervisor: suture.New(name, suture.Spec{
|
||||
// Don't retry too frenetically: an error to open a socket or
|
||||
// whatever is usually something that is either permanent or takes
|
||||
// a while to get solved...
|
||||
FailureThreshold: 2,
|
||||
FailureBackoff: 60 * time.Second,
|
||||
// Only log restarts in debug mode.
|
||||
Log: func(line string) {
|
||||
l.Debugln(line)
|
||||
},
|
||||
PassThroughPanics: true,
|
||||
}),
|
||||
name: name,
|
||||
inbox: make(chan []byte),
|
||||
outbox: make(chan recv, 16),
|
||||
stopped: make(chan struct{}),
|
||||
spec := util.Spec()
|
||||
// Don't retry too frenetically: an error to open a socket or
|
||||
// whatever is usually something that is either permanent or takes
|
||||
// a while to get solved...
|
||||
spec.FailureThreshold = 2
|
||||
spec.FailureBackoff = 60 * time.Second
|
||||
// Only log restarts in debug mode.
|
||||
spec.EventHook = func(e suture.Event) {
|
||||
l.Debugln(e)
|
||||
}
|
||||
c := &cast{
|
||||
Supervisor: suture.New(name, spec),
|
||||
name: name,
|
||||
inbox: make(chan []byte),
|
||||
outbox: make(chan recv, 16),
|
||||
stopped: make(chan struct{}),
|
||||
}
|
||||
util.OnSupervisorDone(c.Supervisor, func() { close(c.stopped) })
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *cast) addReader(svc func(context.Context) error) {
|
||||
@@ -75,17 +76,7 @@ func (c *cast) addWriter(svc func(ctx context.Context) error) {
|
||||
}
|
||||
|
||||
func (c *cast) createService(svc func(context.Context) error, suffix string) util.ServiceWithError {
|
||||
return util.AsServiceWithError(func(ctx context.Context) error {
|
||||
l.Debugln("Starting", c.name, suffix)
|
||||
err := svc(ctx)
|
||||
l.Debugf("Stopped %v %v: %v", c.name, suffix, err)
|
||||
return err
|
||||
}, fmt.Sprintf("%s/%s", c, suffix))
|
||||
}
|
||||
|
||||
func (c *cast) Stop() {
|
||||
c.Supervisor.Stop()
|
||||
close(c.stopped)
|
||||
return util.AsService(svc, fmt.Sprintf("%s/%s", c, suffix))
|
||||
}
|
||||
|
||||
func (c *cast) String() string {
|
||||
|
||||
@@ -41,7 +41,7 @@ func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error {
|
||||
select {
|
||||
case bs = <-inbox:
|
||||
case <-doneCtx.Done():
|
||||
return nil
|
||||
return doneCtx.Err()
|
||||
}
|
||||
|
||||
intfs, err := net.Interfaces()
|
||||
@@ -138,7 +138,7 @@ func readBroadcasts(ctx context.Context, outbox chan<- recv, port int) error {
|
||||
select {
|
||||
case outbox <- recv{c, addr}:
|
||||
case <-doneCtx.Done():
|
||||
return nil
|
||||
return doneCtx.Err()
|
||||
default:
|
||||
l.Debugln("dropping message")
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func writeMulticasts(ctx context.Context, inbox <-chan []byte, addr string) erro
|
||||
select {
|
||||
case bs = <-inbox:
|
||||
case <-doneCtx.Done():
|
||||
return nil
|
||||
return doneCtx.Err()
|
||||
}
|
||||
|
||||
intfs, err := net.Interfaces()
|
||||
@@ -87,7 +87,7 @@ func writeMulticasts(ctx context.Context, inbox <-chan []byte, addr string) erro
|
||||
|
||||
select {
|
||||
case <-doneCtx.Done():
|
||||
return nil
|
||||
return doneCtx.Err()
|
||||
default:
|
||||
}
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func readMulticasts(ctx context.Context, outbox chan<- recv, addr string) error
|
||||
for {
|
||||
select {
|
||||
case <-doneCtx.Done():
|
||||
return nil
|
||||
return doneCtx.Err()
|
||||
default:
|
||||
}
|
||||
n, _, addr, err := pconn.ReadFrom(bs)
|
||||
|
||||
Reference in New Issue
Block a user