aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/net.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-01-04 21:57:57 +0900
committerOphestra <cat@gensokyo.uk>2026-01-05 00:01:23 +0900
commit4897b0259edeb256a3d5cf77148758536e366e64 (patch)
tree23c9e0ff7bbc0f49d233381e6c17b4cc357a939c /internal/pkg/net.go
parentd6e4f85864a1e9b2aadc2e7f47e481242d5e946a (diff)
internal/pkg: improve artifact interface
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>
Diffstat (limited to 'internal/pkg/net.go')
-rw-r--r--internal/pkg/net.go94
1 files changed, 30 insertions, 64 deletions
diff --git a/internal/pkg/net.go b/internal/pkg/net.go
index 09ad7fa0..fb902213 100644
--- a/internal/pkg/net.go
+++ b/internal/pkg/net.go
@@ -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