@@ -23,6 +23,10 @@ const (
2323 forceAfter = 15 * time .Minute
2424)
2525
26+ var (
27+ ErrIsClosed = errors .New ("manager is closed" )
28+ )
29+
2630type ConnectionManager interface {
2731 Connections () []trafficontrol.TrackerMetadata
2832}
@@ -76,7 +80,7 @@ func (m *MutableGroupManager) OutboundGroups() []adapter.MutableOutboundGroup {
7680 return slices .Collect (maps .Values (m .groups ))
7781}
7882
79- // CreateOutboundForGroup creates an outbound for the specified group.
83+ // CreateOutboundForGroup creates an outbound and adds it to the specified group.
8084func (m * MutableGroupManager ) CreateOutboundForGroup (
8185 ctx context.Context ,
8286 router A.Router ,
@@ -87,7 +91,7 @@ func (m *MutableGroupManager) CreateOutboundForGroup(
8791 return m .createForGroup (ctx , m .outboundMgr , router , logger , group , tag , typ , options )
8892}
8993
90- // CreateEndpointForGroup creates an endpoint for the specified group.
94+ // CreateEndpointForGroup creates an endpoint and adds it to the specified group.
9195func (m * MutableGroupManager ) CreateEndpointForGroup (
9296 ctx context.Context ,
9397 router A.Router ,
@@ -116,20 +120,38 @@ func (m *MutableGroupManager) createForGroup(
116120 return errors .New ("manager is closed" )
117121 }
118122
119- groupObj , found := m .groups [group ]
123+ outGroup , found := m .groups [group ]
120124 if ! found {
121125 return fmt .Errorf ("group %s not found" , group )
122126 }
123127
124128 if err := mgr .Create (ctx , router , logger , tag , typ , options ); err != nil {
125129 return err
126130 }
127- n , err := groupObj .Add ([]string {tag })
131+ return m .addToGroup (outGroup , tag )
132+ }
133+
134+ func (m * MutableGroupManager ) AddToGroup (group , tag string ) error {
135+ m .mu .Lock ()
136+ defer m .mu .Unlock ()
137+ if m .closed .Load () {
138+ return errors .New ("manager is closed" )
139+ }
140+
141+ outGroup , found := m .groups [group ]
142+ if ! found {
143+ return fmt .Errorf ("group %s not found" , group )
144+ }
145+ return m .addToGroup (outGroup , tag )
146+ }
147+
148+ func (m * MutableGroupManager ) addToGroup (outGroup adapter.MutableOutboundGroup , tag string ) error {
149+ n , err := outGroup .Add ([]string {tag })
128150 if err != nil || n == 0 {
129151 if err == nil {
130152 err = errors .New ("unknown" )
131153 }
132- return fmt .Errorf ("failed to add %s to %s: %w" , tag , group , err )
154+ return fmt .Errorf ("failed to add %s to %s: %w" , tag , outGroup . Tag () , err )
133155 }
134156 // remove from removal queue in case it was scheduled for removal
135157 m .removalQueue .dequeue (tag )
@@ -141,7 +163,7 @@ func (m *MutableGroupManager) RemoveFromGroup(group, tag string) error {
141163 m .mu .Lock ()
142164 defer m .mu .Unlock ()
143165 if m .closed .Load () {
144- return errors . New ( "manager is closed" )
166+ return ErrIsClosed
145167 }
146168 groupObj , found := m .groups [group ]
147169 if ! found {
0 commit comments