Skip to content

Commit 4123bde

Browse files
committed
Allow specifying a minimum number of underlays for a channel, regardless of underlay type. Fixes #226
1 parent a3221ad commit 4123bde

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

multi.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ func NewMultiChannel(config *MultiChannelConfig) (MultiChannel, error) {
139139
}
140140

141141
impl.startMultiplex(config.Underlay)
142-
go impl.underlayHandler.Start(impl)
143-
144142
impl.underlayHandler.HandleUnderlayAccepted(impl, config.Underlay)
143+
go impl.underlayHandler.Start(impl)
145144

146145
return impl, nil
147146
}
@@ -563,6 +562,7 @@ type underlayConstraint struct {
563562

564563
type UnderlayConstraints struct {
565564
types map[string]underlayConstraint
565+
minTotal uint32
566566
applyInProgress atomic.Bool
567567
lastDial concurrenz.AtomicValue[time.Time]
568568
}
@@ -571,6 +571,10 @@ func (self *UnderlayConstraints) LastDialTime() time.Time {
571571
return self.lastDial.Load()
572572
}
573573

574+
func (self *UnderlayConstraints) SetMinTotal(minTotal uint32) {
575+
self.minTotal = minTotal
576+
}
577+
574578
func (self *UnderlayConstraints) AddConstraint(underlayType string, numDesired int, minAllowed int) {
575579
if self.types == nil {
576580
self.types = make(map[string]underlayConstraint)
@@ -599,6 +603,24 @@ func (self *UnderlayConstraints) countsShowValidState(ch MultiChannel, counts ma
599603
return false
600604
}
601605
}
606+
607+
totalCount := 0
608+
for _, count := range counts {
609+
totalCount += count
610+
}
611+
612+
if uint32(totalCount) < self.minTotal {
613+
pfxlog.Logger().WithField("conn", ch.LogicalName()).
614+
WithField("channelId", ch.ConnectionId()).
615+
WithField("label", ch.Label()).
616+
WithField("underlays", counts).
617+
Info("not enough total open underlays, closing multi-underlay channel")
618+
if err := ch.Close(); err != nil {
619+
pfxlog.Logger().WithError(err).Error("error closing underlay")
620+
}
621+
return false
622+
}
623+
602624
return true
603625
}
604626

multi_listener.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package channel
1818

1919
import (
20-
"github.com/michaelquigley/pfxlog"
2120
"sync"
21+
22+
"github.com/michaelquigley/pfxlog"
2223
)
2324

2425
type MultiChannelFactory func(underlay Underlay, closeCallback func()) (MultiChannel, error)
@@ -47,6 +48,7 @@ func (self *MultiListener) AcceptUnderlay(underlay Underlay) {
4748
}
4849

4950
chId := underlay.ConnectionId()
51+
isFirst, _ := Headers(underlay.Headers()).GetBoolHeader(IsFirstGroupConnection)
5052

5153
self.lock.Lock()
5254
mc, ok := self.channels[chId]
@@ -58,7 +60,7 @@ func (self *MultiListener) AcceptUnderlay(underlay Underlay) {
5860
log.WithError(err).Error("error accepting underlay")
5961
}
6062
} else {
61-
if isFirst, _ := Headers(underlay.Headers()).GetBoolHeader(IsFirstGroupConnection); !isFirst {
63+
if !isFirst {
6264
log.Info("no existing channel found for underlay, but isFirstGroupConnection not set, closing connection")
6365
if err := underlay.Close(); err != nil {
6466
log.Info("error closing underlay")

0 commit comments

Comments
 (0)