github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/sendwindow/sendwindow.go (about) 1 // Package sendwindow carries the tool-result send retry window from 2 // lib/environments.EnvironmentWorker (which learns the work-item lease TTL from 3 // each heartbeat) to anthropic.SessionToolRunner (which bounds its send retries 4 // by it) without putting the plumbing on either package's public API. 5 package sendwindow 6 7 import ( 8 "context" 9 "sync/atomic" 10 "time" 11 ) 12 13 // Window is a duration that one goroutine updates while another reads it. The 14 // zero value, and a nil *Window, report 0, meaning "use the default". 15 type Window struct{ nanos atomic.Int64 } 16 17 // Set stores d. 18 func (w *Window) Set(d time.Duration) { w.nanos.Store(int64(d)) } 19 20 // Get returns the last Set value, or 0. 21 func (w *Window) Get() time.Duration { 22 if w == nil { 23 return 0 24 } 25 return time.Duration(w.nanos.Load()) 26 } 27 28 type ctxKey struct{} 29 30 // NewContext returns a child of ctx carrying w. A SessionToolRunner constructed 31 // on the returned context re-reads w before every send retry. 32 func NewContext(ctx context.Context, w *Window) context.Context { 33 return context.WithValue(ctx, ctxKey{}, w) 34 } 35 36 // FromContext returns the Window carried by ctx, or nil. 37 func FromContext(ctx context.Context) *Window { 38 w, _ := ctx.Value(ctxKey{}).(*Window) 39 return w 40 }