@@ -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
564563type 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+
574578func (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
0 commit comments