-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathresponse_queue.go
More file actions
165 lines (140 loc) · 4.72 KB
/
response_queue.go
File metadata and controls
165 lines (140 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package wire
import (
"bytes"
"context"
)
// ResponseEventKind represents the type of event in the ResponseQueue
type ResponseEventKind uint8
const (
// ResponseParseComplete represents a ParseComplete ack
ResponseParseComplete ResponseEventKind = iota + 1
// ResponseBindComplete represents a BindComplete ack
ResponseBindComplete
// ResponseStmtDescribe represents a composite ParameterDescription + RowDescription
// for a statement (from Describe Statement)
ResponseStmtDescribe
// ResponsePortalDescribe represents a RowDescription for a portal (from Describe Portal)
ResponsePortalDescribe
// ResponseExecute represents an Execute with its complete result set
// (DataRows, CommandComplete)
ResponseExecute
// ResponseCloseComplete represents a CloseComplete ack
ResponseCloseComplete
)
// ResponseEvent represents an event in the response stream
// Use the constructor functions (NewParseCompleteEvent, etc.) to create events
type ResponseEvent struct {
Kind ResponseEventKind
// For ResponseStmtDescribe: holds parameter OIDs and column definitions
Parameters []uint32
Columns Columns
// For ResponsePortalDescribe and ResponseExecute: format codes for result columns
Formats []FormatCode
// For ResponseExecute: tracks completion and results
ResultChannel chan *executeResult // channel to receive results
Result *executeResult // cached result once received
}
// executeResult holds the raw wire bytes or error produced by an async
// portal execution in the parallel pipeline.
type executeResult struct {
buf *bytes.Buffer
err error
}
// NewParseCompleteEvent creates a ParseComplete response event
func NewParseCompleteEvent() *ResponseEvent {
return &ResponseEvent{
Kind: ResponseParseComplete,
}
}
// NewBindCompleteEvent creates a BindComplete response event
func NewBindCompleteEvent() *ResponseEvent {
return &ResponseEvent{
Kind: ResponseBindComplete,
}
}
// NewStmtDescribeEvent creates a statement Describe response event
func NewStmtDescribeEvent(parameters []uint32, columns Columns) *ResponseEvent {
return &ResponseEvent{
Kind: ResponseStmtDescribe,
Parameters: parameters,
Columns: columns,
}
}
// NewPortalDescribeEvent creates a portal Describe response event
func NewPortalDescribeEvent(columns Columns, formats []FormatCode) *ResponseEvent {
return &ResponseEvent{
Kind: ResponsePortalDescribe,
Columns: columns,
Formats: formats,
}
}
// NewCloseCompleteEvent creates a CloseComplete response event
func NewCloseCompleteEvent() *ResponseEvent {
return &ResponseEvent{
Kind: ResponseCloseComplete,
}
}
// NewExecuteEvent creates an Execute response event
func NewExecuteEvent(resultChan chan *executeResult) *ResponseEvent {
return &ResponseEvent{
Kind: ResponseExecute,
ResultChannel: resultChan,
}
}
// ResponseQueue maintains all events in arrival order for a cycle
type ResponseQueue struct {
events []*ResponseEvent
}
// NewResponseQueue creates a new empty ResponseQueue
func NewResponseQueue() *ResponseQueue {
return &ResponseQueue{
events: make([]*ResponseEvent, 0),
}
}
// Enqueue adds an event to the queue
func (q *ResponseQueue) Enqueue(event *ResponseEvent) {
q.events = append(q.events, event)
}
// DrainSync drains all events, waiting for all results to be received
// It returns early if an error is encountered or the context is cancelled
// Only returns events up to but not including an error event
func (q *ResponseQueue) DrainSync(ctx context.Context) ([]*ResponseEvent, error) {
processedEvents := make([]*ResponseEvent, 0, len(q.events))
for _, event := range q.events {
if event.Kind == ResponseExecute {
if event.ResultChannel != nil {
select {
case res := <-event.ResultChannel:
event.Result = res
// Check if the result contains an error
if res != nil && res.err != nil {
// Return events processed so far,not including the error event
// Events after this one won't be sent on the wire
return processedEvents, res.err
}
case <-ctx.Done():
// Context cancelled - return events processed up to this point
// The current event doesn't have a result, but it's included
// so the caller knows where processing stopped
return processedEvents, ctx.Err()
}
}
}
processedEvents = append(processedEvents, event)
}
return processedEvents, nil
}
// DrainAll returns all events in arrival order and clears the queue
func (q *ResponseQueue) DrainAll() []*ResponseEvent {
result := q.events
q.events = make([]*ResponseEvent, 0)
return result
}
// Clear resets the queue for a new cycle
func (q *ResponseQueue) Clear() {
q.events = make([]*ResponseEvent, 0)
}
// Len returns the number of events in the queue
func (q *ResponseQueue) Len() int {
return len(q.events)
}