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
}