|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "trpc.group/trpc-go/trpc-a2a-go/internal/sse" |
| 9 | + "trpc.group/trpc-go/trpc-a2a-go/log" |
| 10 | + "trpc.group/trpc-go/trpc-a2a-go/protocol" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + defaultSSEBatchSize = 20 |
| 15 | + defaultSSEFlushInterval = 1 * time.Second |
| 16 | +) |
| 17 | + |
| 18 | +// sseTunnel optimizes SSE streaming by batching events before sending |
| 19 | +type sseTunnel struct { |
| 20 | + w http.ResponseWriter |
| 21 | + flusher http.Flusher |
| 22 | + rpcID string |
| 23 | + batchSize int |
| 24 | + flushInterval time.Duration |
| 25 | + batch []protocol.StreamingMessageEvent |
| 26 | +} |
| 27 | + |
| 28 | +// newSSETunnel creates a new SSE tunnel with default settings |
| 29 | +func newSSETunnel(w http.ResponseWriter, flusher http.Flusher, rpcID string) *sseTunnel { |
| 30 | + return &sseTunnel{ |
| 31 | + w: w, |
| 32 | + flusher: flusher, |
| 33 | + rpcID: rpcID, |
| 34 | + batchSize: defaultSSEBatchSize, |
| 35 | + flushInterval: defaultSSEFlushInterval, |
| 36 | + batch: make([]protocol.StreamingMessageEvent, 0, defaultSSEBatchSize), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// start runs the SSE tunnel with event batching optimization |
| 41 | +func (t *sseTunnel) start(ctx context.Context, eventsChan <-chan protocol.StreamingMessageEvent, clientClosed <-chan struct{}) { |
| 42 | + ticker := time.NewTicker(t.flushInterval) |
| 43 | + defer ticker.Stop() |
| 44 | + |
| 45 | + for { |
| 46 | + select { |
| 47 | + case event, ok := <-eventsChan: |
| 48 | + if !ok { |
| 49 | + // Channel closed, flush any remaining events and exit |
| 50 | + if len(t.batch) > 0 { |
| 51 | + t.flushBatch() |
| 52 | + } |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Add event to batch |
| 57 | + t.batch = append(t.batch, event) |
| 58 | + |
| 59 | + // Flush if batch is full |
| 60 | + if len(t.batch) >= t.batchSize { |
| 61 | + ticker.Reset(t.flushInterval) |
| 62 | + if !t.flushBatch() { |
| 63 | + return // Error occurred, exit |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + case <-ticker.C: |
| 68 | + // Periodic flush for any accumulated events |
| 69 | + if len(t.batch) > 0 { |
| 70 | + if !t.flushBatch() { |
| 71 | + return // Error occurred, exit |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + case <-clientClosed: |
| 76 | + // Client disconnected |
| 77 | + log.Infof("SSE client disconnected for request ID: %s. Closing stream.", t.rpcID) |
| 78 | + return |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// flushBatch sends all events in the current batch as a single write operation |
| 84 | +func (t *sseTunnel) flushBatch() bool { |
| 85 | + if len(t.batch) == 0 { |
| 86 | + return true |
| 87 | + } |
| 88 | + |
| 89 | + // Convert to batch format for efficient processing |
| 90 | + events := make([]sse.EventBatch, 0, len(t.batch)) |
| 91 | + |
| 92 | + // Process all events in the batch |
| 93 | + for _, event := range t.batch { |
| 94 | + eventType, err := t.getEventType(&event) |
| 95 | + if err != nil { |
| 96 | + if err == errUnknownEvent { |
| 97 | + log.Warnf("Unknown event type received for request ID: %s: %T. Skipping.", t.rpcID, event) |
| 98 | + continue |
| 99 | + } |
| 100 | + log.Errorf("Error determining event type for request ID: %s: %v", t.rpcID, err) |
| 101 | + return false |
| 102 | + } |
| 103 | + |
| 104 | + // Add to batch events |
| 105 | + events = append(events, sse.EventBatch{ |
| 106 | + EventType: eventType, |
| 107 | + ID: t.rpcID, |
| 108 | + Data: &event, |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + // Write the entire batch using optimized batch function |
| 113 | + if err := sse.FormatJSONRPCEventBatch(t.w, events); err != nil { |
| 114 | + log.Errorf("Error writing SSE batch for request ID: %s (client likely disconnected): %v", t.rpcID, err) |
| 115 | + return false |
| 116 | + } |
| 117 | + |
| 118 | + // Clear the batch and flush to client |
| 119 | + t.batch = t.batch[:0] |
| 120 | + t.flusher.Flush() |
| 121 | + |
| 122 | + return true |
| 123 | +} |
| 124 | + |
| 125 | +// getEventType determines the SSE event type from a StreamingMessageEvent |
| 126 | +func (t *sseTunnel) getEventType(event *protocol.StreamingMessageEvent) (string, error) { |
| 127 | + actualEvent := event.Result |
| 128 | + |
| 129 | + switch actualEvent.(type) { |
| 130 | + case *protocol.TaskStatusUpdateEvent: |
| 131 | + return protocol.EventStatusUpdate, nil |
| 132 | + case *protocol.TaskArtifactUpdateEvent: |
| 133 | + return protocol.EventArtifactUpdate, nil |
| 134 | + case *protocol.Message: |
| 135 | + return protocol.EventMessage, nil |
| 136 | + case *protocol.Task: |
| 137 | + return protocol.EventTask, nil |
| 138 | + default: |
| 139 | + return "", errUnknownEvent |
| 140 | + } |
| 141 | +} |
0 commit comments