From 9e34cd4d5dd77e9b03c12d214d3fe82bbc1a9b0b Mon Sep 17 00:00:00 2001 From: Roman Glushko Date: Sat, 13 Jan 2024 23:56:17 +0200 Subject: [PATCH] #44 Implemented the round robin routing strategy --- pkg/routers/routing/round_robin.go | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pkg/routers/routing/round_robin.go diff --git a/pkg/routers/routing/round_robin.go b/pkg/routers/routing/round_robin.go new file mode 100644 index 00000000..a36644a1 --- /dev/null +++ b/pkg/routers/routing/round_robin.go @@ -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 +}