Skip to content

Commit

Permalink
#44 Implemented the round robin routing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jan 13, 2024
1 parent d6fa702 commit 9e34cd4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/routers/routing/round_robin.go
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
}

0 comments on commit 9e34cd4

Please sign in to comment.