lib/model: Return index from deviceActivity.leastBusy

This way, we don't need a second loop over the Availabilities to remove
the selected item.
This commit is contained in:
greatroar
2021-11-26 12:07:43 +01:00
committed by Jakob Borg
parent b84ee4d240
commit 6a9716e8a1
3 changed files with 26 additions and 35 deletions
+7 -8
View File
@@ -26,20 +26,19 @@ func newDeviceActivity() *deviceActivity {
}
}
func (m *deviceActivity) leastBusy(availability []Availability) (Availability, bool) {
// Returns the index of the least busy device, or -1 if all are too busy.
func (m *deviceActivity) leastBusy(availability []Availability) int {
m.mut.Lock()
low := 2<<30 - 1
found := false
var selected Availability
for _, info := range availability {
if usage := m.act[info.ID]; usage < low {
best := -1
for i := range availability {
if usage := m.act[availability[i].ID]; usage < low {
low = usage
selected = info
found = true
best = i
}
}
m.mut.Unlock()
return selected, found
return best
}
func (m *deviceActivity) using(availability Availability) {