Invert initialization dependence on relay/conns

This makes it so we can initialize the relay management and then give
that to the connection management, instead of the other way around.

This is important to me in the discovery revamp I'm doing, as otherwise
I get a circular dependency when constructing stuff, with relaying
depending on connection, connection depending on discovery, and
discovery depending on relaying.

With this fixed, discovery will depend on relaying, and connection will
depend on both discovery and relaying.
This commit is contained in:
Jakob Borg 2015-09-14 10:21:55 +02:00
parent 95fc253d6b
commit 596a49c112
3 changed files with 35 additions and 12 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/model" "github.com/syncthing/syncthing/lib/model"
"github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/relay"
"github.com/thejerf/suture" "github.com/thejerf/suture"
) )
@ -44,6 +45,7 @@ type connectionSvc struct {
tlsCfg *tls.Config tlsCfg *tls.Config
discoverer *discover.Discoverer discoverer *discover.Discoverer
conns chan model.IntermediateConnection conns chan model.IntermediateConnection
relaySvc *relay.Svc
lastRelayCheck map[protocol.DeviceID]time.Time lastRelayCheck map[protocol.DeviceID]time.Time
@ -52,7 +54,7 @@ type connectionSvc struct {
relaysEnabled bool relaysEnabled bool
} }
func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Model, tlsCfg *tls.Config, discoverer *discover.Discoverer) *connectionSvc { func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Model, tlsCfg *tls.Config, discoverer *discover.Discoverer, relaySvc *relay.Svc) *connectionSvc {
svc := &connectionSvc{ svc := &connectionSvc{
Supervisor: suture.NewSimple("connectionSvc"), Supervisor: suture.NewSimple("connectionSvc"),
cfg: cfg, cfg: cfg,
@ -60,6 +62,7 @@ func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Mo
model: mdl, model: mdl,
tlsCfg: tlsCfg, tlsCfg: tlsCfg,
discoverer: discoverer, discoverer: discoverer,
relaySvc: relaySvc,
conns: make(chan model.IntermediateConnection), conns: make(chan model.IntermediateConnection),
connType: make(map[protocol.DeviceID]model.ConnectionType), connType: make(map[protocol.DeviceID]model.ConnectionType),
@ -104,6 +107,10 @@ func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Mo
} }
svc.Add(serviceFunc(svc.handle)) svc.Add(serviceFunc(svc.handle))
if svc.relaySvc != nil {
svc.Add(serviceFunc(svc.acceptRelayConns))
}
return svc return svc
} }
@ -385,6 +392,12 @@ func (s *connectionSvc) connect() {
} }
} }
func (s *connectionSvc) acceptRelayConns() {
for {
s.conns <- s.relaySvc.Accept()
}
}
func (s *connectionSvc) shouldLimit(addr net.Addr) bool { func (s *connectionSvc) shouldLimit(addr net.Addr) bool {
if s.cfg.Options().LimitBandwidthInLan { if s.cfg.Options().LimitBandwidthInLan {
return true return true

View File

@ -711,16 +711,19 @@ func syncthingMain() {
setupGUI(mainSvc, cfg, m, apiSub, discoverer) setupGUI(mainSvc, cfg, m, apiSub, discoverer)
// Start relay management
var relaySvc *relay.Svc
if opts.RelaysEnabled && (opts.GlobalAnnEnabled || opts.RelayWithoutGlobalAnn) {
relaySvc = relay.NewSvc(cfg, tlsCfg)
mainSvc.Add(relaySvc)
}
// Start connection management // Start connection management
connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg, discoverer) connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg, discoverer, relaySvc)
mainSvc.Add(connectionSvc) mainSvc.Add(connectionSvc)
if opts.RelaysEnabled && (opts.GlobalAnnEnabled || opts.RelayWithoutGlobalAnn) {
relaySvc = relay.NewSvc(cfg, tlsCfg, connectionSvc.conns)
connectionSvc.Add(relaySvc)
}
if cpuProfile { if cpuProfile {
f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid())) f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid()))
if err != nil { if err != nil {

View File

@ -25,7 +25,9 @@ import (
"github.com/thejerf/suture" "github.com/thejerf/suture"
) )
func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) *Svc { func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config) *Svc {
conns := make(chan model.IntermediateConnection)
svc := &Svc{ svc := &Svc{
Supervisor: suture.New("Svc", suture.Spec{ Supervisor: suture.New("Svc", suture.Spec{
Log: func(log string) { Log: func(log string) {
@ -40,11 +42,11 @@ func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config, conns chan<- model.Intermed
cfg: cfg, cfg: cfg,
tlsCfg: tlsCfg, tlsCfg: tlsCfg,
tokens: make(map[string]suture.ServiceToken), tokens: make(map[string]suture.ServiceToken),
clients: make(map[string]*client.ProtocolClient), clients: make(map[string]*client.ProtocolClient),
mut: sync.NewRWMutex(), mut: sync.NewRWMutex(),
invitations: make(chan protocol.SessionInvitation), invitations: make(chan protocol.SessionInvitation),
conns: conns,
} }
rcfg := cfg.Raw() rcfg := cfg.Raw()
@ -72,6 +74,7 @@ type Svc struct {
clients map[string]*client.ProtocolClient clients map[string]*client.ProtocolClient
mut sync.RWMutex mut sync.RWMutex
invitations chan protocol.SessionInvitation invitations chan protocol.SessionInvitation
conns chan model.IntermediateConnection
} }
func (s *Svc) VerifyConfiguration(from, to config.Configuration) error { func (s *Svc) VerifyConfiguration(from, to config.Configuration) error {
@ -207,6 +210,10 @@ func (s *Svc) ClientStatus() map[string]bool {
return status return status
} }
func (s *Svc) Accept() model.IntermediateConnection {
return <-s.conns
}
type invitationReceiver struct { type invitationReceiver struct {
invitations chan protocol.SessionInvitation invitations chan protocol.SessionInvitation
tlsCfg *tls.Config tlsCfg *tls.Config