GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — workflow/deferred.go at main · ubgo/workflow

Expand file tree

/

deferred.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
261 lines (236 loc) · 8.36 KB
/

deferred.go

Copy path

File metadata and controls

261 lines (236 loc) · 8.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package workflow
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"sync"
)
// ErrDeferredClosed is returned by an enqueue made through a Deferred
// that was already flushed or discarded, such as a goroutine of a request
// handler enqueueing after the response was written. Nothing is sent.
//
//ds:def id=errdeferredclosed-mcycghsh owner=@khanakia stability=stable
var ErrDeferredClosed = errors.New("workflow: deferred enqueues already flushed or discarded")
// Deferred holds enqueues until the work that made them is known to have
// succeeded: Flush sends them in order, Discard drops them. It is how a
// request handler enqueues "send the welcome email" without sending it
// when the request then fails. Put one in a ctx with WithDeferred (DeferHTTP
// does this per request) and enqueue through a client wrapped with
// DeferEnqueue.
//
// It is not a transaction: a Flush after a database commit can still fail
// or the process can die between the two. When the job must be enqueued
// if and only if a database transaction commits, use the SQL driver's
// EnqueueTx or its transactional outbox instead.
//
// A Deferred is safe for concurrent use. Use NewDeferred; it is single
// use: after Flush or Discard every Add returns ErrDeferredClosed.
//
//ds:def id=deferred-d9t494vm owner=@khanakia stability=stable
type Deferred struct {
mu sync.Mutex
held []heldJob
closed bool
}
type heldJob struct {
enqueue EnqueueFunc
job Job
}
// NewDeferred returns an empty, open Deferred.
func NewDeferred() *Deferred { return &Deferred{} }
// Add holds job, to be sent with enqueue by Flush. It returns
// ErrDeferredClosed once the Deferred was flushed or discarded. The job is
// copied, so the caller may reuse it.
//
//ds:def id=deferred-add-jy58gna5 owner=@khanakia stability=stable
func (d *Deferred) Add(enqueue EnqueueFunc, job Job) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return ErrDeferredClosed
}
d.held = append(d.held, heldJob{enqueue: enqueue, job: CloneJob(job)})
return nil
}
// Len is the number of jobs held.
func (d *Deferred) Len() int {
d.mu.Lock()
defer d.mu.Unlock()
return len(d.held)
}
// take closes d and returns what it held; a second call returns nothing.
func (d *Deferred) take() []heldJob {
d.mu.Lock()
defer d.mu.Unlock()
held := d.held
d.held, d.closed = nil, true
return held
}
// Flush sends every held job, in the order held, and closes d. It sends
// all of them even when one fails, and returns the refs of the ones sent
// (the zero RunRef for a failure, so refs[i] is the i-th held job) with
// every failure joined. A second Flush, or a Flush after Discard, sends
// nothing.
//
//ds:def id=deferred-flush-22p939zy owner=@khanakia stability=stable
func (d *Deferred) Flush(ctx context.Context) ([]RunRef, error) {
held := d.take()
refs := make([]RunRef, len(held))
var errs []error
for i, h := range held {
ref, err := h.enqueue(ctx, h.job)
if err != nil {
errs = append(errs, fmt.Errorf("deferred job %d for %q: %w", i, h.job.Workflow, err))
continue
}
refs[i] = ref
}
return refs, errors.Join(errs...)
}
// Discard drops every held job, closes d and returns how many it dropped.
//
//ds:def id=deferred-discard-u886nc2u owner=@khanakia stability=stable
func (d *Deferred) Discard() int { return len(d.take()) }
// deferredKey is the unexported context key for a Deferred.
type deferredKey struct{}
// WithDeferred returns a ctx carrying d, so enqueues made with it through
// a DeferEnqueue client are held by d.
func WithDeferred(ctx context.Context, d *Deferred) context.Context {
return context.WithValue(ctx, deferredKey{}, d)
}
// DeferredFrom returns the Deferred on ctx, and false when there is none.
//
//ds:def id=deferredfrom-vw9bzra7 owner=@khanakia stability=stable
func DeferredFrom(ctx context.Context) (*Deferred, bool) {
d, ok := ctx.Value(deferredKey{}).(*Deferred)
return d, ok
}
// DeferEnqueue is enqueue middleware that holds a job in the ctx's
// Deferred instead of sending it, returning the zero RunRef and nil: the
// run does not exist yet, so there is no ref to give. Without a Deferred
// on the ctx it sends the job at once, so the same client works inside and
// outside requests. Validation and capability checks happen when the job
// is flushed, by the client underneath; a caller that needs them up
// front, or needs the RunRef, enqueues without a Deferred on its ctx.
// Middleware listed after it in the chain runs at flush time.
//
//ds:def id=deferenqueue-n94f3gz2 owner=@khanakia stability=stable
func DeferEnqueue() EnqueueMiddleware {
return func(next EnqueueFunc) EnqueueFunc {
return func(ctx context.Context, job Job) (RunRef, error) {
d, ok := DeferredFrom(ctx)
if !ok {
return next(ctx, job)
}
return RunRef{}, d.Add(next, job)
}
}
}
// DeferOptions tunes DeferHTTP. The zero value is the default.
//
//ds:def id=deferoptions-6fvx7vmb owner=@khanakia stability=stable
type DeferOptions struct {
// Succeeded decides from the response status whether the request
// succeeded, so its held jobs are flushed rather than discarded. Nil
// means DeferSucceeded (any status below 400). A handler that writes
// no status counts as 200.
Succeeded func(status int) bool
// OnError receives a flush that failed for some jobs. The response is
// already written, so it cannot reach the client. Nil logs it with
// Logger.
OnError func(r *http.Request, err error)
// Logger is used by the default OnError. Nil means slog.Default().
Logger *slog.Logger
}
// DeferSucceeded is DeferOptions.Succeeded's default: a status below
// 400.
//
//ds:def id=defersucceeded-ca6zpqq6 owner=@khanakia stability=stable
func DeferSucceeded(status int) bool { return status < http.StatusBadRequest }
// DeferHTTP wraps next so every request gets its own Deferred on its
// context: jobs enqueued during the request through a DeferEnqueue client
// are sent after next returns, when opts.Succeeded accepts the response
// status, and dropped when it does not or when next panics (the panic
// goes on up after the drop). The flush runs on the request's context
// without its cancellation, so a client that hung up after a successful
// response does not cancel the jobs.
//
//ds:def id=deferhttp-46csut5g owner=@khanakia stability=stable
func DeferHTTP(next http.Handler, opts DeferOptions) http.Handler {
succeeded := opts.Succeeded
if succeeded == nil {
succeeded = DeferSucceeded
}
onError := opts.OnError
if onError == nil {
logger := opts.Logger
if logger == nil {
logger = slog.Default()
}
onError = func(r *http.Request, err error) {
logger.ErrorContext(r.Context(), "workflow: deferred enqueue failed", "method", r.Method, "path", r.URL.Path, "error", err)
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d := NewDeferred()
sw := &statusWriter{ResponseWriter: w}
completed := false
defer func() {
if !completed { // next panicked (or called runtime.Goexit)
d.Discard()
}
}()
next.ServeHTTP(sw, r.WithContext(WithDeferred(r.Context(), d)))
completed = true
if !succeeded(sw.Status()) {
d.Discard()
return
}
if _, err := d.Flush(context.WithoutCancel(r.Context())); err != nil {
onError(r, err)
}
})
}
// statusWriter records the status a handler wrote.
type statusWriter struct {
http.ResponseWriter
status int
}
func (s *statusWriter) WriteHeader(code int) {
if s.status == 0 {
s.status = code
}
s.ResponseWriter.WriteHeader(code)
}
func (s *statusWriter) Write(b []byte) (int, error) {
if s.status == 0 {
s.status = http.StatusOK
}
return s.ResponseWriter.Write(b)
}
// Flush passes a streaming handler's flush through, so wrapping a
// handler in DeferHTTP does not stop it streaming.
//
//ds:def id=statuswriter-flush-ab4p85x2 owner=@khanakia stability=stable
func (s *statusWriter) Flush() {
if s.status == 0 {
s.status = http.StatusOK
}
_ = http.NewResponseController(s.ResponseWriter).Flush() // an unflushable writer has nothing to flush
}
// Status is the status written, 200 when none was.
//
//ds:def id=statuswriter-status-gmeu2u8v owner=@khanakia stability=stable
func (s *statusWriter) Status() int {
if s.status == 0 {
return http.StatusOK
}
return s.status
}
// Unwrap lets http.ResponseController reach the underlying writer's
// Flush, Hijack and deadlines.
//
//ds:def id=statuswriter-unwrap-d5epth5q owner=@khanakia stability=stable
func (s *statusWriter) Unwrap() http.ResponseWriter { return s.ResponseWriter }
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.