forked from jeroenrinzema/psql-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
330 lines (279 loc) · 7.72 KB
/
cache.go
File metadata and controls
330 lines (279 loc) · 7.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package wire
import (
"context"
"errors"
"fmt"
"iter"
"sync"
"github.com/jeroenrinzema/psql-wire/pkg/buffer"
"github.com/jeroenrinzema/psql-wire/pkg/types"
)
// Limit represents the maximum number of rows to be written.
// Zero denotes "no limit".
type Limit uint32
const NoLimit Limit = 0
type Statement struct {
fn PreparedStatementFn
parameters []uint32
columns Columns
}
func DefaultStatementCacheFn() StatementCache {
return &DefaultStatementCache{}
}
type DefaultStatementCache struct {
statements map[string]*Statement
mu sync.RWMutex
}
// Set attempts to bind the given statement to the given name. Any
// previously defined statement is overridden.
func (cache *DefaultStatementCache) Set(ctx context.Context, name string, stmt *PreparedStatement) error {
cache.mu.Lock()
defer cache.mu.Unlock()
if cache.statements == nil {
cache.statements = map[string]*Statement{}
}
cache.statements[name] = &Statement{
fn: stmt.fn,
parameters: stmt.parameters,
columns: stmt.columns,
}
return nil
}
// Get attempts to get the prepared statement for the given name. An error
// is returned when no statement has been found.
func (cache *DefaultStatementCache) Get(ctx context.Context, name string) (*Statement, error) {
cache.mu.RLock()
defer cache.mu.RUnlock()
if cache.statements == nil {
return nil, nil
}
stmt, has := cache.statements[name]
if !has {
return nil, nil
}
return stmt, nil
}
func (cache *DefaultStatementCache) Delete(ctx context.Context, name string) error {
cache.mu.Lock()
defer cache.mu.Unlock()
delete(cache.statements, name)
return nil
}
func (cache *DefaultStatementCache) Close() {
cache.mu.Lock()
defer cache.mu.Unlock()
clear(cache.statements)
}
type Portal struct {
statement *Statement
parameters []Parameter
formats []FormatCode
// The iterator state (created by iter.Pull)
next func() (struct{}, bool)
stop func()
// Filled in by dataWriter.Complete when the handler finishes. Used to
// return the tag when re-executing a completed portal.
tag string
err error
// Set to true when execution of the portal has finished.
done bool
// pending is closed when the most recently launched async goroutine
// finishes. A new goroutine for the same portal waits on this channel
// before starting, so same-portal executes serialize while different
// portals run in parallel.
pending chan struct{}
}
// Close tears down the portal from outside the execution goroutine (e.g.
// explicit Close message or portal re-bind). If an async goroutine is still
// running, cleanup is deferred to a background goroutine that waits for it.
func (p *Portal) Close() {
if p.pending != nil {
pending := p.pending
go func() {
<-pending
p.close()
}()
return
}
p.close()
}
// close tears down the iterator inline. Used from within execute where we
// are already inside the execution context and don't need to wait on pending.
func (p *Portal) close() {
if p.stop != nil {
p.stop()
p.next = nil
p.stop = nil
}
}
func portalSuspended(writer *buffer.Writer) error {
writer.Start(types.ServerPortalSuspended)
return writer.End()
}
func (p *Portal) execute(ctx context.Context, limit Limit, reader *buffer.Reader, writer *buffer.Writer) error {
if p.done {
// Re-executing an already completed portal simply returns the tag or
// error again.
if p.err != nil {
return p.err
}
return commandComplete(writer, p.tag)
}
if p.next == nil {
// This is the first execute call on this portal. So let's start the
// execution. Otherwise we continue from where we left off.
session, _ := GetSession(ctx)
// Create a simple push-style iterator (iter.Seq) around the
// statement.fn.
seq := func(yield func(struct{}) bool) {
dw := &dataWriter{
ctx: ctx,
session: session,
columns: p.statement.columns,
formats: p.formats,
reader: reader,
client: writer,
yield: yield,
tag: &p.tag,
}
err := p.statement.fn(ctx, dw, p.parameters)
if err != nil && !errors.Is(err, ErrSuspendedHandlerClosed) {
p.err = err
}
}
// Then we convert that push-style iterator into a pull-style iterator,
// so we can suspend the iterator when we reach the row limit.
p.next, p.stop = iter.Pull(seq)
}
var count Limit
for {
if limit != NoLimit && count >= limit {
// We've reached the row limit. Suspend the portal and let the
// client know, so it can either issue a new Execute to continue or
// close the portal.
return portalSuspended(writer)
}
// Run the handler until it has produced the next row or finishes. The
// dataWriter inside the handler will call yield to "teleport" back
// here.
_, ok := p.next()
if !ok {
// The handler has finished. CommandComplete was already written
// by dataWriter.Complete.
p.close()
p.done = true
return p.err
}
count++
}
}
func DefaultPortalCacheFn() PortalCache {
return &DefaultPortalCache{}
}
type DefaultPortalCache struct {
portals map[string]*Portal
executing *Portal
closePending bool
mu sync.RWMutex
}
func (cache *DefaultPortalCache) Bind(ctx context.Context, name string, stmt *Statement, parameters []Parameter, formats []FormatCode) error {
cache.mu.Lock()
defer cache.mu.Unlock()
if cache.portals == nil {
cache.portals = map[string]*Portal{}
}
if existing, ok := cache.portals[name]; ok {
existing.Close()
}
cache.portals[name] = &Portal{
statement: stmt,
parameters: parameters,
formats: formats,
}
return nil
}
func (cache *DefaultPortalCache) Get(ctx context.Context, name string) (*Portal, error) {
cache.mu.Lock()
defer cache.mu.Unlock()
if cache.portals == nil {
return nil, nil
}
portal, has := cache.portals[name]
if !has {
return nil, nil
}
return portal, nil
}
func (cache *DefaultPortalCache) Execute(ctx context.Context, name string, limit Limit, reader *buffer.Reader, writer *buffer.Writer) (err error) {
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("unexpected panic: %s", r)
}
}()
// Look up the portal under the lock, then release before executing.
// The handler may call back into the cache so we must make sure we don't
// hold the lock for the execute duration. e.g. a handler for DEALLOCATE
// may call back into the cache to delete a portal.
cache.mu.RLock()
if cache.portals == nil {
cache.mu.RUnlock()
return nil
}
portal, has := cache.portals[name]
cache.mu.RUnlock()
if !has {
return nil
}
cache.mu.Lock()
cache.executing = portal
cache.mu.Unlock()
err = portal.execute(ctx, limit, reader, writer)
cache.mu.Lock()
cache.executing = nil
needsClose := cache.closePending
cache.closePending = false
cache.mu.Unlock()
if needsClose {
portal.close()
}
return err
}
// closePortal closes the portal immediately, unless it is the currently
// executing portal, in which case it marks the portal for deferred closing
// after the current Execute call returns.
func (cache *DefaultPortalCache) closePortal(portal *Portal) {
if portal == cache.executing {
cache.closePending = true
} else {
portal.Close()
}
}
func (cache *DefaultPortalCache) Delete(ctx context.Context, name string) error {
cache.mu.Lock()
defer cache.mu.Unlock()
if portal, ok := cache.portals[name]; ok {
cache.closePortal(portal)
}
delete(cache.portals, name)
return nil
}
func (cache *DefaultPortalCache) DeleteByStatement(ctx context.Context, stmt *Statement) error {
cache.mu.Lock()
defer cache.mu.Unlock()
for name, portal := range cache.portals {
if portal.statement == stmt {
cache.closePortal(portal)
delete(cache.portals, name)
}
}
return nil
}
func (cache *DefaultPortalCache) Close() {
cache.mu.Lock()
defer cache.mu.Unlock()
for _, portal := range cache.portals {
cache.closePortal(portal)
}
clear(cache.portals)
}