Runner: a get session's object reader reads on a context that ends with the session, not with the call that opened it

Builds: design 47 G §490, §518, §620 (a get over one chunk is a session read chunk by chunk)
This commit is contained in:
2026-11-15 01:45:37 +01:00
parent 45fbc813df
commit 9e8bf6dc0c
2 changed files with 49 additions and 10 deletions
+31 -6
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"sort"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/buscalls"
@@ -177,15 +178,18 @@ func (c *consumers) objectsGet(module string, r buscalls.ObjectsNameRequest) (an
if st == nil {
return nil, fmt.Errorf("the runner has no bus to read %s of %s from", r.Name, module)
}
ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout)
defer cancel()
ctx, cancel, opened := openingContext()
info, rd, found, err := st.ObjectsOpen(ctx, module, r)
opened()
if err != nil || !found {
cancel()
return nil, err
}
if info.Size > buscalls.ChunkBytes {
return c.openGet(module, info, rd, sessionDigest(info.Digest), false)
// the reader reads on ctx after this call returns: it ends with the session, not with this call
return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(info.Digest), false)
}
defer cancel()
defer rd.Close()
data, err := io.ReadAll(rd)
if err != nil {
@@ -208,11 +212,32 @@ func (c *consumers) sentGet(module string, r buscalls.ObjectsSentGetRequest) (an
if st == nil {
return nil, fmt.Errorf("the runner has no bus to read %s from", r.Object)
}
ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout)
defer cancel()
ctx, cancel, opened := openingContext()
info, rd, err := st.SentOpen(ctx, r)
opened()
if err != nil {
cancel()
return nil, err
}
return c.openGet(module, info, rd, sessionDigest(r.Digest), true)
return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(r.Digest), true)
}
// openingContext is the context an object is opened on: it is bound at bus.StateTimeout while opening, and once opened (the
// returned function) is bound no more: a reader of an object that outlives the call reads on it until its session ends.
func openingContext() (context.Context, context.CancelFunc, func()) {
ctx, cancel := context.WithCancel(context.Background())
t := time.AfterFunc(bus.StateTimeout, cancel)
return ctx, cancel, func() { t.Stop() }
}
// cancelOnClose ends the context an object's reader reads on when the reader is closed: at the end of its get session.
type cancelOnClose struct {
io.ReadCloser
cancel context.CancelFunc
}
func (c cancelOnClose) Close() error {
err := c.ReadCloser.Close()
c.cancel()
return err
}
+18 -4
View File
@@ -37,12 +37,26 @@ func (f *fakeObjects) ObjectsPutStream(ctx context.Context, _ string, r buscalls
return buscalls.ObjectInfo{Name: r.Name, Size: uint64(len(data)), Description: description}, nil
}
func (f *fakeObjects) ObjectsOpen(context.Context, string, buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) {
return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), true, nil
// ctxReader reads as the bus's object reader does: on the context it was opened on, failing once that is cancelled.
type ctxReader struct {
ctx context.Context
r io.Reader
}
func (f *fakeObjects) SentOpen(context.Context, buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) {
return buscalls.ObjectInfo{Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), nil
func (c ctxReader) Read(p []byte) (int, error) {
if err := c.ctx.Err(); err != nil {
return 0, err
}
return c.r.Read(p)
}
func (c ctxReader) Close() error { return nil }
func (f *fakeObjects) ObjectsOpen(ctx context.Context, _ string, _ buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) {
return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, true, nil
}
func (f *fakeObjects) SentOpen(ctx context.Context, _ buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) {
return buscalls.ObjectInfo{Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, nil
}
func aSession(t *testing.T, f *fakeObjects) *moduleBus {