Skip to content

Commit 82272aa

Browse files
committed
Convert from scan to event based for router dialing
1 parent d732c53 commit 82272aa

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

common/ctrlchan/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,5 @@ func (self *ListenerCtrlChannel) HandleUnderlayAccepted(_ channel.MultiChannel,
423423

424424
func (self *ListenerCtrlChannel) IsConnected() bool {
425425
// when the listener underlay becomes disconnected, it closed
426-
return true
426+
return !self.IsClosed()
427427
}

controller/config/config.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ const (
9898
DefaultBackgroundQueueDropWhenFull = false
9999
DefaultBackgroundQueueThreshold = 50 * time.Millisecond
100100

101-
DefaultCtrlDialerEnabled = false
102-
DefaultCtrlDialerScanInterval = 30 * time.Second
103-
DefaultCtrlDialerDialDelay = 30 * time.Second
101+
DefaultCtrlDialerEnabled = false
102+
DefaultCtrlDialerDialDelay = 30 * time.Second
104103
)
105104

106105
type Config struct {
@@ -176,10 +175,9 @@ type CtrlOptions struct {
176175
}
177176

178177
type CtrlDialerConfig struct {
179-
Enabled bool
180-
Groups []string
181-
ScanInterval time.Duration
182-
DialDelay time.Duration
178+
Enabled bool
179+
Groups []string
180+
DialDelay time.Duration
183181
}
184182

185183
func (config *Config) Configure(sub config.Subconfig) error {
@@ -656,10 +654,9 @@ func LoadConfig(path string) (*Config, error) {
656654
}
657655

658656
controllerConfig.Ctrl.Dialer = CtrlDialerConfig{
659-
Enabled: DefaultCtrlDialerEnabled,
660-
Groups: []string{"default"},
661-
ScanInterval: DefaultCtrlDialerScanInterval,
662-
DialDelay: DefaultCtrlDialerDialDelay,
657+
Enabled: DefaultCtrlDialerEnabled,
658+
Groups: []string{"default"},
659+
DialDelay: DefaultCtrlDialerDialDelay,
663660
}
664661

665662
if value, found := submap["dialer"]; found {
@@ -677,15 +674,6 @@ func LoadConfig(path string) (*Config, error) {
677674
}
678675
}
679676
}
680-
if v, found := dialerMap["scanInterval"]; found {
681-
if s, ok := v.(string); ok {
682-
if d, err := time.ParseDuration(s); err == nil {
683-
controllerConfig.Ctrl.Dialer.ScanInterval = d
684-
} else {
685-
return nil, fmt.Errorf("invalid ctrl.dialer.scanInterval: %w", err)
686-
}
687-
}
688-
}
689677
if v, found := dialerMap["dialDelay"]; found {
690678
if s, ok := v.(string); ok {
691679
if d, err := time.ParseDuration(s); err == nil {

controller/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ func (c *Controller) Run() error {
484484
headers,
485485
c.shutdownC,
486486
)
487+
c.network.AddRouterPresenceHandler(ctrlDialer)
488+
c.network.Router.Store.AddEntityIdListener(ctrlDialer.RouterUpdated, boltz.EntityUpdated)
487489
go ctrlDialer.Run()
488490
}
489491

controller/handler_ctrl/dialer.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,30 @@ func (self *CtrlDialer) Run() {
7070
defer self.ctxCancel()
7171

7272
log := pfxlog.Logger().WithField("component", "ctrlDialer")
73-
log.WithField("scanInterval", self.config.ScanInterval).
74-
WithField("dialDelay", self.config.DialDelay).
73+
log.WithField("dialDelay", self.config.DialDelay).
7574
WithField("groups", self.config.Groups).
7675
Info("starting ctrl channel dialer")
7776

78-
ticker := time.NewTicker(self.config.ScanInterval)
79-
defer ticker.Stop()
80-
81-
for {
82-
select {
83-
case <-ticker.C:
84-
self.scan()
85-
case <-self.closeNotify:
86-
log.Info("stopping ctrl channel dialer")
87-
return
88-
}
77+
if self.config.DialDelay > 0 {
78+
time.Sleep(self.config.DialDelay)
8979
}
80+
81+
self.scan()
82+
83+
<-self.closeNotify
84+
log.Info("stopping ctrl channel dialer")
85+
}
86+
87+
func (self *CtrlDialer) RouterConnected(_ *model.Router) {
88+
// no-op: router connected via another path, nothing to do
89+
}
90+
91+
func (self *CtrlDialer) RouterDisconnected(r *model.Router) {
92+
self.checkDialersFor(r.Id, r)
93+
}
94+
95+
func (self *CtrlDialer) RouterUpdated(id string) {
96+
self.checkDialersFor(id, nil)
9097
}
9198

9299
func (self *CtrlDialer) scan() {

0 commit comments

Comments
 (0)