lib: Add util.Service as suture.Service template (fixes #5801) (#5806)

This commit is contained in:
Simon Frei
2019-07-09 11:40:30 +02:00
committed by GitHub
parent d0ab65a178
commit ba056578ec
21 changed files with 340 additions and 420 deletions

View File

@@ -13,7 +13,10 @@ import (
"github.com/AudriusButkevicius/pfilter"
"github.com/ccding/go-stun/stun"
"github.com/thejerf/suture"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/util"
)
const stunRetryInterval = 5 * time.Minute
@@ -56,6 +59,8 @@ type Subscriber interface {
}
type Service struct {
suture.Service
name string
cfg config.Wrapper
subscriber Subscriber
@@ -66,8 +71,6 @@ type Service struct {
natType NATType
addr *Host
stop chan struct{}
}
func New(cfg config.Wrapper, subscriber Subscriber, conn net.PacketConn) (*Service, net.PacketConn) {
@@ -88,7 +91,7 @@ func New(cfg config.Wrapper, subscriber Subscriber, conn net.PacketConn) (*Servi
client.SetSoftwareName("") // Explicitly unset this, seems to freak some servers out.
// Return the service and the other conn to the client
return &Service{
s := &Service{
name: "Stun@" + conn.LocalAddr().Network() + "://" + conn.LocalAddr().String(),
cfg: cfg,
@@ -100,16 +103,17 @@ func New(cfg config.Wrapper, subscriber Subscriber, conn net.PacketConn) (*Servi
natType: NATUnknown,
addr: nil,
stop: make(chan struct{}),
}, otherDataConn
}
s.Service = util.AsService(s.serve)
return s, otherDataConn
}
func (s *Service) Stop() {
close(s.stop)
s.Service.Stop()
_ = s.stunConn.Close()
}
func (s *Service) Serve() {
func (s *Service) serve(stop chan struct{}) {
for {
disabled:
s.setNATType(NATUnknown)
@@ -117,7 +121,7 @@ func (s *Service) Serve() {
if s.cfg.Options().IsStunDisabled() {
select {
case <-s.stop:
case <-stop:
return
case <-time.After(time.Second):
continue
@@ -130,12 +134,12 @@ func (s *Service) Serve() {
// This blocks until we hit an exit condition or there are issues with the STUN server.
// This returns a boolean signifying if a different STUN server should be tried (oppose to the whole thing
// shutting down and this winding itself down.
if !s.runStunForServer(addr) {
if !s.runStunForServer(addr, stop) {
// Check exit conditions.
// Have we been asked to stop?
select {
case <-s.stop:
case <-stop:
return
default:
}
@@ -163,7 +167,7 @@ func (s *Service) Serve() {
}
}
func (s *Service) runStunForServer(addr string) (tryNext bool) {
func (s *Service) runStunForServer(addr string, stop chan struct{}) (tryNext bool) {
l.Debugf("Running stun for %s via %s", s, addr)
// Resolve the address, so that in case the server advertises two
@@ -201,10 +205,10 @@ func (s *Service) runStunForServer(addr string) (tryNext bool) {
return false
}
return s.stunKeepAlive(addr, extAddr)
return s.stunKeepAlive(addr, extAddr, stop)
}
func (s *Service) stunKeepAlive(addr string, extAddr *Host) (tryNext bool) {
func (s *Service) stunKeepAlive(addr string, extAddr *Host, stop chan struct{}) (tryNext bool) {
var err error
nextSleep := time.Duration(s.cfg.Options().StunKeepaliveStartS) * time.Second
@@ -247,7 +251,7 @@ func (s *Service) stunKeepAlive(addr string, extAddr *Host) (tryNext bool) {
select {
case <-time.After(sleepFor):
case <-s.stop:
case <-stop:
l.Debugf("%s stopping, aborting stun", s)
return false
}