Documentation
¶
Overview ¶
Package fetch is what the providers share to get their native libraries and models onto disk: a download verified by size and SHA-256 before it appears under its final name, a file that is downloaded only when it is missing or not the pinned size, an unverified download that reports the digest a new pin needs, the extraction of the libraries out of a release tarball (extract.go), and the staged install of such a tarball into a directory (install.go). It knows nothing about which libraries or models those are.
A download is written to "<dest>.partial" next to its destination and renamed into place once it checks out. The partial file is held under a lock (lock.go), so goroutines and processes after the same file take turns, and it survives an interrupted run: the next one asks the server for the rest with a Range request and verifies the whole against the pinned digest. That is so where the system has file locks (unix). Where it has none, processes are not kept apart: each writes a partial file of its own ("<dest>.partial-<pid>", see lock_other.go), so none writes through another, and nothing is resumed.
The directories all this happens in are the caller's, and may hold anything. The package writes only under names derived from the destination ("<dest>.partial", "<dir>.lock", "<dir>.sense-download", "<dir>.sense-staging", "<dir>.sense-retired") and removes only what it wrote or what carries its marker: nothing is swept for its name or age.
Index ¶
- Constants
- func ExtractTarGz(archive, dir string, want func(path string) bool, limit int64) (int, error)
- func ResolveDir(dir string) (string, error)
- func WithDefaultTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)
- type Asset
- type DirInstall
- type Downloader
- func (d Downloader) EnsureFile(ctx context.Context, what string, a Asset, dest string) error
- func (d Downloader) File(ctx context.Context, what string, a Asset, dest string) error
- func (d Downloader) InstallDir(ctx context.Context, in DirInstall) (installed bool, err error)
- func (d Downloader) Unpinned(ctx context.Context, url, dest string) (string, int64, error)
Constants ¶
const DepsURL = "https://github.com/go-bumbu/sense-deps/releases/download/"
DepsURL is where the providers' pinned native libraries are downloaded from: the releases of go-bumbu/sense-deps, which keeps the exact upstream builds (ONNX Runtime, llama.cpp) so a release that disappears upstream does not break a first use. A file's URL is DepsURL + "<release tag>/<file>".
Variables ¶
This section is empty.
Functions ¶
func ExtractTarGz ¶
ExtractTarGz copies the entries of the gzipped tarball at archive that want accepts into dir, flattened to their base names (release archives nest them under a version-named directory), and returns how many it wrote. want sees each entry's slash-separated path inside the archive. A symlink is recreated only when it points at a bare name in the same directory that want accepts too; anything else is rejected rather than written. Each file is capped at limit bytes.
func ResolveDir ¶
ResolveDir returns dir as an absolute path with its symbolic links resolved, as far as the path exists: the part that is not there yet is kept as it is, behind the real path of the part that is, and a link whose target is not there yet is followed to that target. It is the name to install into and to compare directories by. A managed directory that is a link (to another disk, say) must be filled where it points: the renames of an install would otherwise replace the link by a directory and leave the old files behind it; and one directory reached under two names must compare equal to itself.
func WithDefaultTimeout ¶
WithDefaultTimeout bounds ctx by d unless the caller already gave it a deadline, which then stands: a default is for callers that set none, and must not cut short the time a caller allowed for a large download.
Types ¶
type Asset ¶
type Asset struct {
URL string
// SHA256 is the digest in hex, in either case.
SHA256 string
Size int64 // exact size when known, 0 to skip the check
}
Asset is one downloadable file with its expected digest.
type DirInstall ¶
type DirInstall struct {
// Dir is the directory to fill.
Dir string
// What names the install in the download's log line and in errors
// ("llama.cpp b11429").
What string
// Archive is the gzipped release tarball.
Archive Asset
// Extract unpacks the verified archive into dir, a new directory that
// holds nothing but this package's marker file (".sense-install").
Extract func(archive, dir string) error
// RecordFile is the name of the file written next to the libraries with
// Record as its content, so a later run can tell what Dir holds. Its
// presence is also what marks a directory as installed by the caller: a
// directory with other content and no such file is never replaced.
RecordFile string
Record []byte
// Ready reports whether Dir already holds what is wanted. It is asked
// once the directory's lock is held, so an install that another
// goroutine or process finished in the meantime is found instead of
// repeated. An error stops everything: the caller refusing to replace a
// directory it is using, say. Nil means never ready.
Ready func() (bool, error)
// Use, when set, runs once Dir holds what is wanted, with the lock still
// held: loading the libraries there keeps another process from replacing
// them halfway through the load.
Use func() error
// Loaded, when set, reports whether this process already runs what is in
// Dir: it has loaded the libraries from there. Such a process does not
// take the lock when Ready also says the directory holds what is wanted.
// It has nothing to install and nothing to load, and would otherwise wait
// at every open for as long as another process downloads a build into
// the directory. Ready and Use then run without the lock.
Loaded func() bool
}
DirInstall describes the install of a release tarball's libraries into a directory this package's caller owns. What differs between providers is here: which archive, how its layout is unpacked, what the record of the install says, and when the directory is good as it is.
type Downloader ¶
type Downloader struct {
// Client makes the requests; nil means http.DefaultClient.
Client *http.Client
// UserAgent names the downloads to the server.
UserAgent string
// Limit caps a single download in bytes; an asset pinned to a larger
// exact size is capped at that size instead.
Limit int64
// Logger gets a line before each download starts (a download can take
// minutes) and one when a missing file was fetched; nil discards them.
Logger *slog.Logger
}
Downloader is how one provider's downloads are made. The zero value works: http.DefaultClient, no user agent, no cap beyond an asset's own size, and no log lines.
func (Downloader) EnsureFile ¶
EnsureFile makes sure dest holds a, downloading it (see File) when dest is absent or, for an asset with a known size, has another size: a file cut short by a crash or a full disk is fetched again instead of being handed to a model loader. A file of the right size is taken as it is, without reading it. what names the file in log lines and errors ("model org/name"). An asset without a URL cannot be fetched, so a missing or wrong file is then an error naming it.
func (Downloader) File ¶
File downloads a to dest, verifying size and SHA-256 before the file appears under its final name, and replaces whatever was there. The bytes of an earlier, interrupted attempt are kept and only the rest is asked for. what names the download in the log line written before it starts.
func (Downloader) InstallDir ¶
func (d Downloader) InstallDir(ctx context.Context, in DirInstall) (installed bool, err error)
InstallDir makes in.Dir hold what in describes and runs in.Use on it. Installs into one directory are serialized, between goroutines and (where the platform has file locks) between processes, by a lock file next to the directory, and whether an install is needed is decided under that lock. The wait for the lock ends with ctx, as an error. Only a process that already runs what the directory holds does not wait at all (see DirInstall.Loaded). The download and the extraction happen next to the final directory, which is renamed into place at the end: an interrupted install never leaves a half-filled in.Dir. A directory that holds anything this package's caller did not install is not touched; that is an error.
Nothing is ever removed for its name alone. A call that finds the directory ready writes nothing but the lock file. One that installs works under "<Dir>.sense-download", "<Dir>.sense-staging" and "<Dir>.sense-retired", and clears what an install that was killed left under the last two only when it is recognizably that (see clearLeftover); anything else under those names stops the install with an error.
in.Dir is resolved first (see ResolveDir), so a directory that is a symbolic link is installed where the link points, next to which the lock and the staging then are, and the link stays a link. A caller that names the directory elsewhere (in Ready, in Use, to compare it with the one it loaded from) resolves it the same way.
installed reports whether the directory was filled by this call.