server/internal/client/ollama: handle some network errors gracefully (#10317)

This commit is contained in:
Blake Mizerany
2025-04-17 12:43:09 -07:00
committed by GitHub
parent 09bb2e30f6
commit 1d99451ad7
4 changed files with 115 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ package registry
import (
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
@@ -11,6 +12,7 @@ import (
"log/slog"
"net/http"
"slices"
"strings"
"sync"
"time"
@@ -330,9 +332,8 @@ func (s *Local) handlePull(w http.ResponseWriter, r *http.Request) error {
return err
}
err := s.Client.Pull(ctx, p.model())
var oe *ollama.Error
if errors.As(err, &oe) && oe.Temporary() {
continue // retry
if canRetry(err) {
continue
}
return err
}
@@ -390,3 +391,20 @@ func decodeUserJSON[T any](r io.Reader) (T, error) {
}
return zero, err
}
func canRetry(err error) bool {
if err == nil {
return false
}
var oe *ollama.Error
if errors.As(err, &oe) {
return oe.Temporary()
}
s := err.Error()
return cmp.Or(
errors.Is(err, context.DeadlineExceeded),
strings.Contains(s, "unreachable"),
strings.Contains(s, "no route to host"),
strings.Contains(s, "connection reset by peer"),
)
}