(ctx context.Context, id string)
| 45 | } |
| 46 | |
| 47 | func (b *modelStreamBridge) read(ctx context.Context, id string) (handlers.ModelExecutionChunk, bool, error) { |
| 48 | if b == nil { |
| 49 | return handlers.ModelExecutionChunk{}, true, fmt.Errorf("model stream bridge is unavailable") |
| 50 | } |
| 51 | if id == "" { |
| 52 | return handlers.ModelExecutionChunk{}, true, fmt.Errorf("model stream id is required") |
| 53 | } |
| 54 | b.mu.Lock() |
| 55 | entry, ok := b.streams[id] |
| 56 | b.mu.Unlock() |
| 57 | if !ok || entry.chunks == nil { |
| 58 | return handlers.ModelExecutionChunk{}, true, nil |
| 59 | } |
| 60 | if ctx == nil { |
| 61 | ctx = context.Background() |
| 62 | } |
| 63 | select { |
| 64 | case <-ctx.Done(): |
| 65 | b.close(id) |
| 66 | return handlers.ModelExecutionChunk{}, true, ctx.Err() |
| 67 | case chunk, okRead := <-entry.chunks: |
| 68 | if !okRead { |
| 69 | b.close(id) |
| 70 | return handlers.ModelExecutionChunk{}, true, nil |
| 71 | } |
| 72 | if chunk.Err != nil { |
| 73 | b.close(id) |
| 74 | return chunk, true, nil |
| 75 | } |
| 76 | return chunk, false, nil |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func (b *modelStreamBridge) close(id string) { |
| 81 | if b == nil || id == "" { |
no test coverage detected