-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#45: Added weight field and WRR routing strategy
- Loading branch information
1 parent
01f46d2
commit b37972d
Showing
9 changed files
with
131 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package routing | ||
|
||
import ( | ||
"sync" | ||
|
||
"glide/pkg/providers" | ||
) | ||
|
||
const ( | ||
WeightedRoundRobin Strategy = "weighed-round-robin" | ||
) | ||
|
||
type Weighter struct { | ||
model providers.Model | ||
currentWeight int | ||
} | ||
|
||
func (w *Weighter) Current() int { | ||
return w.currentWeight | ||
} | ||
|
||
func (w *Weighter) Weight() int { | ||
return w.model.Weight() | ||
} | ||
|
||
func (w *Weighter) Incr() { | ||
w.currentWeight += w.Weight() | ||
} | ||
|
||
func (w *Weighter) Decr(totalWeight int) { | ||
w.currentWeight -= totalWeight | ||
} | ||
|
||
type WRoundRobinRouting struct { | ||
mu sync.Mutex | ||
weights []Weighter | ||
} | ||
|
||
func NewWeightedRoundRobin(models []providers.Model) *WRoundRobinRouting { | ||
weights := make([]Weighter, 0, len(models)) | ||
|
||
return &WRoundRobinRouting{ | ||
weights: weights, | ||
} | ||
} | ||
|
||
func (r *WRoundRobinRouting) Iterator() LangModelIterator { | ||
return r | ||
} | ||
|
||
func (r *WRoundRobinRouting) Next() (providers.Model, error) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
totalWeight := 0 | ||
var maxWeighter *Weighter | ||
|
||
for _, weighter := range r.weights { | ||
if !weighter.model.Healthy() { | ||
continue | ||
} | ||
|
||
weighter.Incr() | ||
totalWeight += weighter.Weight() | ||
|
||
if maxWeighter == nil { | ||
maxWeighter = &weighter | ||
continue | ||
} | ||
|
||
if weighter.Current() > maxWeighter.Current() { | ||
maxWeighter = &weighter | ||
} | ||
} | ||
|
||
if maxWeighter != nil { | ||
maxWeighter.Decr(totalWeight) | ||
|
||
return maxWeighter.model, nil | ||
} | ||
|
||
return nil, ErrNoHealthyModels | ||
} |