| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "sync/atomic" |
| 7 | ) |
| 8 | |
| 9 | // HostStreamRegistry tracks host-side provider streams (OpenAI/Anthropic/etc.) |
| 10 | // so generation drain can cancel in-flight HTTP reads without waiting for |
| 11 | // controller.Cancel of a still-published generation. |
| 12 | type HostStreamRegistry struct { |
| 13 | mu sync.Mutex |
| 14 | byGen map[uint64]map[uint64]context.CancelFunc |
| 15 | nextID atomic.Uint64 |
| 16 | gate *PublishGate |
| 17 | // drainHooked remembers which generations already registered a |
| 18 | // RegisterDrainCancel fan-in so we do not stack duplicate hooks. |
| 19 | drainHooked map[uint64]struct{} |
| 20 | } |
| 21 | |
| 22 | // DefaultHostStreams belongs to the compatibility runtime owner. |
| 23 | var DefaultHostStreams = DefaultRuntimeOwner.HostStreams |
| 24 | |
| 25 | // NewHostStreamRegistry returns an empty registry. |
| 26 | func NewHostStreamRegistry(gates ...*PublishGate) *HostStreamRegistry { |
| 27 | var gate *PublishGate |
| 28 | if len(gates) > 0 { |
| 29 | gate = gates[0] |
| 30 | } |
| 31 | return &HostStreamRegistry{ |
| 32 | byGen: make(map[uint64]map[uint64]context.CancelFunc), |
| 33 | drainHooked: make(map[uint64]struct{}), |
| 34 | gate: gate, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Track registers cancel for gen and returns untrack (safe to call once). |
| 39 | // When gen is 0, tracking is a no-op (no publish gate yet). |
| 40 | func (r *HostStreamRegistry) Track(gen uint64, cancel context.CancelFunc) (untrack func()) { |
| 41 | if r == nil || gen == 0 || cancel == nil { |
| 42 | return func() {} |
| 43 | } |
| 44 | id := r.nextID.Add(1) |
| 45 | r.mu.Lock() |
| 46 | if r.byGen[gen] == nil { |
| 47 | r.byGen[gen] = make(map[uint64]context.CancelFunc) |
| 48 | } |
| 49 | r.byGen[gen][id] = cancel |
| 50 | registerDrainHook := false |
| 51 | if _, hooked := r.drainHooked[gen]; !hooked { |
| 52 | r.drainHooked[gen] = struct{}{} |
| 53 | registerDrainHook = true |
| 54 | } |
| 55 | r.mu.Unlock() |
| 56 | if registerDrainHook { |
| 57 | // Fan-in: one drain cancel per generation cancels every tracked stream. |
| 58 | // Register outside r.mu because an already-expired generation fires the |
| 59 | // callback synchronously and re-enters CancelGeneration. |
| 60 | gate := r.gate |
| 61 | if gate == nil { |
| 62 | gate = DefaultPublishGate() |
| 63 | } |
| 64 | gate.RegisterDrainCancel(gen, func() { r.CancelGeneration(gen) }) |
| 65 | } |
| 66 | var once sync.Once |
| 67 | return func() { |
| 68 | once.Do(func() { |
| 69 | r.mu.Lock() |
| 70 | if m := r.byGen[gen]; m != nil { |
| 71 | delete(m, id) |
| 72 | if len(m) == 0 { |
| 73 | delete(r.byGen, gen) |
| 74 | } |
| 75 | } |
| 76 | r.mu.Unlock() |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // CancelGeneration cancels every host stream still tracked for gen. |
| 82 | func (r *HostStreamRegistry) CancelGeneration(gen uint64) { |
| 83 | if r == nil || gen == 0 { |
| 84 | return |
| 85 | } |
| 86 | r.mu.Lock() |
| 87 | m := r.byGen[gen] |
| 88 | delete(r.byGen, gen) |
| 89 | delete(r.drainHooked, gen) |
| 90 | r.mu.Unlock() |
| 91 | for _, c := range m { |
| 92 | if c != nil { |
| 93 | c() |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Count returns live tracked streams for gen (tests). |
| 99 | func (r *HostStreamRegistry) Count(gen uint64) int { |
| 100 | if r == nil { |
| 101 | return 0 |
| 102 | } |
| 103 | r.mu.Lock() |
| 104 | defer r.mu.Unlock() |
| 105 | return len(r.byGen[gen]) |
| 106 | } |
| 107 | |
| 108 | // TrackHostStream is a convenience over DefaultHostStreams.Track. |
| 109 | func TrackHostStream(gen uint64, cancel context.CancelFunc) (untrack func()) { |
| 110 | return RuntimeOwnerOrDefault(nil).HostStreams.Track(gen, cancel) |
| 111 | } |
| 112 |