Skip to content

Commit

Permalink
use upstream Ollama Heartbeat method for availability
Browse files Browse the repository at this point in the history
  • Loading branch information
presbrey committed Aug 25, 2024
1 parent 0e64128 commit 3a55184
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ Note: When an Ollama instance goes offline, OllamaFarm automatically selects the
```go
type Options struct {
Client *http.Client
Heartbeat time.Duration
ModelsTTL time.Duration
PingTTL time.Duration
}
```
* `Properties`: Defines the properties of an Ollama client. All fields are optional.
Expand Down
6 changes: 3 additions & 3 deletions farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func NewWithOptions(options *Options) *Farm {
if options.Client == nil {
options.Client = http.DefaultClient
}
if options.Heartbeat == 0 {
options.Heartbeat = 5 * time.Second
}
if options.ModelsTTL == 0 {
options.ModelsTTL = 30 * time.Second
}
if options.PingTTL == 0 {
options.PingTTL = 5 * time.Second
}
return &Farm{
ollamas: make(map[string]*Ollama),
options: options,
Expand Down
4 changes: 3 additions & 1 deletion farm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func setupTestServers() []*httptest.Server {
} else if r.URL.Path == "/api/version" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"version":"0.3.6"}`))
} else if r.URL.Path == "/" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
Expand All @@ -56,7 +58,7 @@ func TestFarmMethods(t *testing.T) {
}()

farm := ollamafarm.New()
farm2 := ollamafarm.NewWithOptions(&ollamafarm.Options{nil, time.Second, time.Second})
farm2 := ollamafarm.NewWithOptions(&ollamafarm.Options{Heartbeat: time.Second, ModelsTTL: time.Second})

// Register clients
for i, server := range servers {
Expand Down
9 changes: 4 additions & 5 deletions ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ollama *Ollama) updateModels() {

func (ollama *Ollama) updateOnline() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err := ollama.client.Version(ctx)
err := ollama.client.Heartbeat(ctx)
cancel()

ollama.farm.mu.Lock()
Expand All @@ -66,20 +66,19 @@ func (ollama *Ollama) updateOnline() {
// updateOllama fetches and updates the model list and checks the client's status.
func (ollama *Ollama) updateTickers() {
ollama.farm.mu.Lock()
heartbeatTicker := time.NewTicker(ollama.farm.options.Heartbeat)
modelTicker := time.NewTicker(ollama.farm.options.ModelsTTL)
versionTicker := time.NewTicker(ollama.farm.options.PingTTL)
ollama.farm.mu.Unlock()

ollama.updateModels()
ollama.updateOnline()

for {
select {
case <-heartbeatTicker.C:
ollama.updateOnline()
case <-modelTicker.C:
ollama.updateModels()

case <-versionTicker.C:
ollama.updateOnline()
}
}
}
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Ollama struct {
type Options struct {
// Client is the HTTP client used to make requests.
Client *http.Client
// Heartbeat is the time-to-live for online/offline detection. Default: 5 seconds
Heartbeat time.Duration
// ModelsTTL is the time-to-live for the models cache. Default: 30 seconds
ModelsTTL time.Duration
// PingTTL is the time-to-live for the online/offline ping. Default: 5 seconds
PingTTL time.Duration
}

// Properties defines the properties of an Ollama client.
Expand Down

0 comments on commit 3a55184

Please sign in to comment.