-
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.
#44 Implemented the round robin routing strategy
- Loading branch information
1 parent
d6fa702
commit 9e34cd4
Showing
1 changed file
with
45 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package routing | ||
|
||
import ( | ||
"glide/pkg/providers" | ||
"sync/atomic" | ||
) | ||
|
||
const ( | ||
RoundRobin Strategy = "round-robin" | ||
) | ||
|
||
// RoundRobinRouting routes request to the next model in the list in cycle | ||
type RoundRobinRouting struct { | ||
idx atomic.Uint64 | ||
models []*providers.LangModel | ||
} | ||
|
||
func NewRoundRobinRouting(models []*providers.LangModel) *RoundRobinRouting { | ||
return &RoundRobinRouting{ | ||
models: models, | ||
} | ||
} | ||
|
||
func (r *RoundRobinRouting) Iterator() LangModelIterator { | ||
return r | ||
} | ||
|
||
func (r *RoundRobinRouting) Next() (*providers.LangModel, error) { | ||
modelLen := len(r.models) | ||
|
||
// in order to avoid infinite loop in case of no healthy model is available, | ||
// we need to track whether we made a whole cycle around the model slice looking for a healthy model | ||
for i := 0; i < modelLen; i++ { | ||
idx := r.idx.Add(1) | ||
model := r.models[idx%uint64(modelLen)] | ||
|
||
if !model.Healthy() { | ||
continue | ||
} | ||
|
||
return model, nil | ||
} | ||
|
||
return nil, ErrNoHealthyModels | ||
} |