internal/pkg: improve artifact interface
All checks were successful
Test / Create distribution (push) Successful in 43s
Test / Sandbox (push) Successful in 2m35s
Test / Hakurei (push) Successful in 3m36s
Test / ShareFS (push) Successful in 3m41s
Test / Hpkg (push) Successful in 4m19s
Test / Sandbox (race detector) (push) Successful in 4m52s
Test / Hakurei (race detector) (push) Successful in 5m52s
Test / Flake checks (push) Successful in 1m53s

This moves all cache I/O code to Cache. Artifact now only contains methods for constructing their actual contents.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-04 21:57:57 +09:00
parent d6e4f85864
commit 4897b0259e
6 changed files with 1013 additions and 773 deletions

View File

@@ -3,11 +3,10 @@ package pkg
import (
"context"
"crypto/sha512"
"errors"
"io"
"net/http"
"os"
"sync"
"syscall"
"hakurei.app/container/check"
)
@@ -17,38 +16,34 @@ type httpArtifact struct {
// Caller-supplied request.
req *http.Request
// Caller-supplied checksum of the response body, also used as the
// identifier. This is validated during curing.
id ID
// Caller-supplied checksum of the response body. This is validated during
// curing and the first call to Data.
checksum Checksum
// doFunc is the Do method of [http.Client] supplied by the caller.
doFunc func(req *http.Request) (*http.Response, error)
// Instance of [Cache] to submit the cured artifact to.
c *Cache
// Response body read to EOF.
data []byte
// Populated when submitting to or loading from [Cache].
pathname *check.Absolute
// Synchronises access to pathname and data.
// Synchronises access to data.
mu sync.Mutex
}
// NewHTTP returns a new [File] backed by the supplied client and request. If
// c is nil, [http.DefaultClient] is used instead.
func (c *Cache) NewHTTP(hc *http.Client, req *http.Request, checksum Checksum) File {
if hc == nil {
hc = http.DefaultClient
func NewHTTP(c *http.Client, req *http.Request, checksum Checksum) File {
if c == nil {
c = http.DefaultClient
}
return &httpArtifact{req: req, id: checksum, doFunc: hc.Do, c: c}
return &httpArtifact{req: req, checksum: checksum, doFunc: c.Do}
}
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
func (c *Cache) NewHTTPGet(
func NewHTTPGet(
ctx context.Context,
hc *http.Client,
c *http.Client,
url string,
checksum Checksum,
) (File, error) {
@@ -56,14 +51,25 @@ func (c *Cache) NewHTTPGet(
if err != nil {
return nil, err
}
return c.NewHTTP(hc, req, checksum), nil
return NewHTTP(c, req, checksum), nil
}
// Kind returns the hardcoded [Kind] constant.
func (a *httpArtifact) Kind() Kind { return KindHTTP }
// ID returns the caller-supplied hash of the response body.
func (a *httpArtifact) ID() ID { return a.id }
func (a *httpArtifact) ID() ID { return a.checksum }
// Params is unreachable.
func (a *httpArtifact) Params() []byte {
panic("not implemented")
}
// Dependencies returns a nil slice.
func (a *httpArtifact) Dependencies() []Artifact { return nil }
// Checksum returns the address to the caller-supplied checksum.
func (a *httpArtifact) Checksum() *Checksum { return &a.checksum }
// ResponseStatusError is returned for a response returned by an [http.Client]
// with a status code other than [http.StatusOK].
@@ -95,44 +101,13 @@ func (a *httpArtifact) do() (data []byte, err error) {
return
}
// Hash cures the [Artifact] and returns its hash. The return value is always
// identical to that of the ID method.
func (a *httpArtifact) Hash() (Checksum, error) { _, err := a.Pathname(); return a.id, err }
// Pathname cures the [Artifact] and returns its pathname in the [Cache].
func (a *httpArtifact) Pathname() (pathname *check.Absolute, err error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.pathname != nil {
return a.pathname, nil
}
if a.data != nil {
pathname, err = a.c.StoreFile(
a.id, a.data,
(*Checksum)(&a.id),
true,
)
if err == nil {
a.pathname = pathname
}
return
} else {
a.pathname, a.data, _, err = a.c.LoadOrStoreFile(
a.id, a.do,
(*Checksum)(&a.id),
true,
)
if err != nil {
a.pathname, a.data = nil, nil
}
return a.pathname, err
}
// Cure returns syscall.ENOTSUP. Callers should use Data instead.
func (a *httpArtifact) Cure(*check.Absolute, CacheDataFunc) error {
return syscall.ENOTSUP
}
// Data completes the http request and returns the resulting response body read
// to EOF. Data does not write to the underlying [Cache].
// to EOF. Data does not interact with the filesystem.
func (a *httpArtifact) Data() (data []byte, err error) {
a.mu.Lock()
defer a.mu.Unlock()
@@ -142,23 +117,14 @@ func (a *httpArtifact) Data() (data []byte, err error) {
return a.data, nil
}
if a.pathname, a.data, err = a.c.LoadFile(a.id); err == nil {
return a.data, nil
} else {
a.pathname, a.data = nil, nil
if !errors.Is(err, os.ErrNotExist) {
return
}
}
if data, err = a.do(); err != nil {
return
}
h := sha512.New384()
h.Write(data)
if got := (Checksum)(h.Sum(nil)); got != a.id {
return nil, &ChecksumMismatchError{got, a.id}
if got := (Checksum)(h.Sum(nil)); got != a.checksum {
return nil, &ChecksumMismatchError{got, a.checksum}
}
a.data = data
return