Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: add dynamic configuration for download variables #3960

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions server/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import (
"github.com/ollama/ollama/format"
)

const maxRetries = 6
// env-tweakable constants
var maxRetries = getEnvInt("OLLAMA_DOWNLOAD_RETRIES", 6)
var numDownloadParts = getEnvInt("OLLAMA_DOWNLOAD_PARALLEL", 64)
var minDownloadPartSize = getEnvInt("OLLAMA_DOWNLOAD_MIN_SIZE", int64(100*format.MegaByte))
var maxDownloadPartSize = getEnvInt("OLLAMA_DOWNLOAD_MAX_SIZE", int64(1000*format.MegaByte))

var errMaxRetriesExceeded = errors.New("max retries exceeded")
var errPartStalled = errors.New("part stalled")
Expand Down Expand Up @@ -58,12 +62,6 @@ type blobDownloadPart struct {
*blobDownload `json:"-"`
}

const (
numDownloadParts = 64
minDownloadPartSize int64 = 100 * format.MegaByte
maxDownloadPartSize int64 = 1000 * format.MegaByte
)

func (p *blobDownloadPart) Name() string {
return strings.Join([]string{
p.blobDownload.Name, "partial", strconv.Itoa(p.N),
Expand Down Expand Up @@ -111,7 +109,7 @@ func (b *blobDownload) Prepare(ctx context.Context, requestURL *url.URL, opts *r

b.Total, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)

size := b.Total / numDownloadParts
size := b.Total / int64(numDownloadParts)
switch {
case size < minDownloadPartSize:
size = minDownloadPartSize
Expand Down
15 changes: 10 additions & 5 deletions server/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/ollama/ollama/version"
)

var maxDigestRetries = getEnvInt("OLLAMA_DOWNLOAD_DIGEST_RETRIES", 3)

type registryOptions struct {
Insecure bool
Username string
Expand Down Expand Up @@ -1266,10 +1268,13 @@ func verifyBlob(digest string) error {
}
defer f.Close()

fileDigest, _ := GetSHA256Digest(f)
if digest != fileDigest {
return fmt.Errorf("%w: want %s, got %s", errDigestMismatch, digest, fileDigest)
for i := 0; i < maxDigestRetries; i++ {
fileDigest, _ := GetSHA256Digest(f)
if digest == fileDigest {
return nil
}
err = fmt.Errorf("%w: want %s, got %s", errDigestMismatch, digest, fileDigest)
slog.Warn(err.Error())
}

return nil
return err
}
27 changes: 27 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package server

import (
"os"
"strconv"
"unsafe"
)

type Int interface {
int | int8 | int16 | int32 | int64
}

func parseInt[T Int](v string, fallback T) T {
i, err := strconv.ParseInt(v, 10, int(unsafe.Sizeof(fallback)))
if err != nil {
return fallback
}
return T(i)
}

func getEnvInt[T Int](key string, fallback T) T {
v, ok := os.LookupEnv(key)
if !ok {
return fallback
}
return parseInt(v, fallback)
}