mirror of
https://github.com/dogkeeper886/ollama37.git
synced 2025-12-11 00:07:07 +00:00
Previously, using a Registry required a DiskCache to be passed in for use in various methods. This was a bit cumbersome, as the DiskCache is required for most operations, and the DefaultCache is used in most of those cases. This change makes the DiskCache an optional field on the Registry struct. This also changes DefaultCache to initialize on first use. This is to not burden clients with the cost of creating a new cache per use, or having to hold onto a cache for the lifetime of the Registry. Also, slip in some minor docs updates for Trace.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package ollama
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// Trace is a set of functions that are called to report progress during blob
|
|
// downloads and uploads.
|
|
//
|
|
// Use [WithTrace] to attach a Trace to a context for use with [Registry.Push]
|
|
// and [Registry.Pull].
|
|
type Trace struct {
|
|
// Update is called during [Registry.Push] and [Registry.Pull] to
|
|
// report the progress of blob uploads and downloads.
|
|
//
|
|
// It is called once at the beginning of the download with a zero n and
|
|
// then once per read operation with the number of bytes read so far,
|
|
// and an error if any.
|
|
//
|
|
// A function assigned must be safe for concurrent use. The function is
|
|
// called synchronously and so should not block or take long to run.
|
|
Update func(_ *Layer, n int64, _ error)
|
|
}
|
|
|
|
func (t *Trace) update(l *Layer, n int64, err error) {
|
|
if t.Update != nil {
|
|
t.Update(l, n, err)
|
|
}
|
|
}
|
|
|
|
type traceKey struct{}
|
|
|
|
// WithTrace returns a context derived from ctx that uses t to report trace
|
|
// events.
|
|
func WithTrace(ctx context.Context, t *Trace) context.Context {
|
|
return context.WithValue(ctx, traceKey{}, t)
|
|
}
|
|
|
|
var emptyTrace = &Trace{}
|
|
|
|
// traceFromContext returns the Trace associated with ctx, or an empty Trace if
|
|
// none is found.
|
|
//
|
|
// It never returns nil.
|
|
func traceFromContext(ctx context.Context) *Trace {
|
|
t, _ := ctx.Value(traceKey{}).(*Trace)
|
|
if t == nil {
|
|
return emptyTrace
|
|
}
|
|
return t
|
|
}
|