MCPcopy Create free account
hub / github.com/tbphp/gpt-load / selectNext

Method selectNext

internal/services/subgroup_manager.go:145–198  ·  view source on GitHub ↗

selectNext uses weighted round-robin algorithm to select a sub-group with active keys

()

Source from the content-addressed store, hash-verified

143
144// selectNext uses weighted round-robin algorithm to select a sub-group with active keys
145func (s *selector) selectNext() string {
146 s.mu.Lock()
147 defer s.mu.Unlock()
148
149 if len(s.subGroups) == 0 {
150 return ""
151 }
152
153 if len(s.subGroups) == 1 {
154 if s.hasActiveKeys(s.subGroups[0].subGroupID) {
155 return s.subGroups[0].name
156 }
157 logrus.WithFields(logrus.Fields{
158 "group_id": s.subGroups[0].subGroupID,
159 "group_name": s.subGroups[0].name,
160 }).Debug("Single sub-group has no active keys")
161 return ""
162 }
163
164 attempted := make(map[uint]bool)
165 for len(attempted) < len(s.subGroups) {
166 item := s.selectByWeight()
167 if item == nil {
168 break
169 }
170
171 if attempted[item.subGroupID] {
172 continue
173 }
174 attempted[item.subGroupID] = true
175
176 if s.hasActiveKeys(item.subGroupID) {
177 logrus.WithFields(logrus.Fields{
178 "aggregate_group": s.groupName,
179 "selected_group": item.name,
180 "attempts": len(attempted),
181 }).Debug("Selected sub-group with active keys")
182 return item.name
183 }
184
185 logrus.WithFields(logrus.Fields{
186 "group_id": item.subGroupID,
187 "group_name": item.name,
188 "attempts": len(attempted),
189 }).Debug("Sub-group has no active keys, trying next")
190 }
191
192 logrus.WithFields(logrus.Fields{
193 "aggregate_group": s.groupName,
194 "total_sub_groups": len(s.subGroups),
195 }).Warn("No sub-groups with active keys available")
196
197 return ""
198}
199
200// selectByWeight implements smooth weighted round-robin algorithm
201func (s *selector) selectByWeight() *subGroupItem {

Callers 1

SelectSubGroupMethod · 0.80

Calls 2

hasActiveKeysMethod · 0.95
selectByWeightMethod · 0.95

Tested by

no test coverage detected