-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
489 lines (428 loc) · 13.2 KB
/
client.go
File metadata and controls
489 lines (428 loc) · 13.2 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Package gearman provides a thread-safe Gearman client implementation in Go.
//
// Gearman is a job queue system that allows you to distribute work across multiple
// machines or processes. This package provides a client that can submit jobs to
// a Gearman server and handle the responses.
//
// The client supports both foreground and background jobs, and provides methods
// for submitting jobs and receiving data and warnings from job execution.
//
// Example usage:
//
// client, err := gearman.NewClient("tcp4", "localhost:4730")
// if err != nil {
// panic(err)
// }
// defer client.Close()
//
// job, err := client.Submit("reverse", []byte("hello world!"), nil, nil)
// if err != nil {
// panic(err)
// }
//
// state := job.Run()
// fmt.Println("Job completed with state:", state)
package gearman
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log/slog"
"net"
"os"
"strings"
"sync"
"sync/atomic"
"time"
slogctx "github.com/veqryn/slog-context"
"github.com/wcn/gearman/v2/job"
"github.com/wcn/gearman/v2/packet"
"github.com/wcn/gearman/v2/scanner"
)
// noOpCloser is like an io.NopCloser, but for an io.Writer.
type noOpCloser struct {
w io.Writer
}
func (c *noOpCloser) Write(data []byte) (n int, err error) {
return c.w.Write(data)
}
func (c *noOpCloser) Close() error {
return nil
}
var discard = &noOpCloser{w: io.Discard}
type partialJob struct {
// data is used to write data back to the caller's provided io.Writer
data io.WriteCloser
// warnings is used to write warning messages back to the caller's provided io.Writer
warnings io.WriteCloser
}
// Client is a thread-safe Gearman client that can submit jobs to a Gearman server.
// The client maintains a connection to the server and handles packet routing
// to the appropriate jobs.
type Client struct {
network string
address string
// conn is the connection to the gearman server
conn net.Conn
// packets is the stream of incoming gearman packets from the server
packets chan *packet.Packet
// jobs is a router for sending packets to the correct job to interpret
jobs map[string]chan *packet.Packet
// partialJobs
partialJobs chan *partialJob
newJobs chan *job.Job
jobLock sync.RWMutex
// pings tracks ping identifiers so we can route them back to the caller
pings map[string]chan struct{}
pingLock sync.RWMutex
// connection state
connLock sync.RWMutex
connErr error // tracks the last connection error
closed bool // tracks if the client has been closed
started bool // prevents multiple Start() calls
// goroutine management (added in v2, backward compatible)
ctx context.Context
cancel context.CancelFunc
}
// Start connects to server and starts the goroutines for packet processing.
// When the provided context is cancelled, the client will automatically close,
func (c *Client) Start(ctx context.Context) error {
c.connLock.Lock()
defer c.connLock.Unlock()
if c.started {
return nil
}
conn, err := net.Dial(c.network, c.address)
if err != nil {
return fmt.Errorf("error while establishing a connection to gearman: %s", err)
}
c.conn = conn
c.ctx, c.cancel = context.WithCancel(ctx)
c.started = true
go c.read(c.ctx, scanner.New(c.conn))
go c.routePackets(c.ctx)
// Monitor context cancellation and auto-close
go func() {
<-ctx.Done()
c.Close()
}()
return nil
}
// Close terminates the connection to the server and cleans up resources.
func (c *Client) Close() error {
c.connLock.Lock()
defer c.connLock.Unlock()
if c.closed {
return nil
}
c.closed = true
if c.cancel != nil {
c.cancel()
}
// TODO: figure out when to close packet chan
return c.conn.Close()
}
func (c *Client) isReady() error {
c.connLock.RLock()
if c.closed {
c.connLock.RUnlock()
return fmt.Errorf("client is closed")
}
if c.connErr != nil {
c.connLock.RUnlock()
return fmt.Errorf("connection error: %w", c.connErr)
}
if !c.started {
c.connLock.RUnlock()
return fmt.Errorf("client has not been started - call Start(ctx) first")
}
c.connLock.RUnlock()
return nil
}
func (c *Client) submit(fn string, payload []byte, data, warnings io.WriteCloser, t packet.Type, timeout time.Duration) (*job.Job, error) {
if err := c.isReady(); err != nil {
return nil, err
}
// create and marshal the gearman packet
pack := &packet.Packet{
Code: packet.Req,
Type: t,
Arguments: [][]byte{[]byte(fn), {}, payload},
}
buf, err := pack.MarshalBinary()
if err != nil {
return nil, err
}
// write the packet to the gearman server
if _, err := io.Copy(c.conn, bytes.NewBuffer(buf)); err != nil {
// Mark connection as failed
c.connLock.Lock()
c.connErr = err
c.connLock.Unlock()
return nil, fmt.Errorf("failed to send packet: %w", err)
}
// block while the client waits for confirmation that a job has been created
select {
case c.partialJobs <- &partialJob{data: data, warnings: warnings}:
// Job submitted successfully
case <-time.After(timeout):
return nil, fmt.Errorf("timeout waiting for job confirmation")
}
select {
case j := <-c.newJobs:
return j, nil
case <-time.After(timeout):
return nil, fmt.Errorf("timeout waiting for job creation")
}
}
// SubmitWithTimeout sends a new job to the server with the specified function and payload and timeout.
// You must provide two WriteClosers for data and warnings to be written to.
func (c *Client) SubmitWithTimeout(fn string, payload []byte, data, warnings io.WriteCloser, timeout time.Duration) (*job.Job, error) {
return c.submit(fn, payload, data, warnings, packet.SubmitJob, timeout)
}
// Submit sends a new job to the server with the specified function and payload. You must provide
// two WriteClosers for data and warnings to be written to.
func (c *Client) Submit(fn string, payload []byte, data, warnings io.WriteCloser) (*job.Job, error) {
return c.submit(fn, payload, data, warnings, packet.SubmitJob, 30*time.Second)
}
// SubmitBackground submits a background job. There is no access to data, warnings, or completion
// state.
func (c *Client) SubmitBackground(fn string, payload []byte) error {
_, err := c.submit(fn, payload, discard, discard, packet.SubmitJobBg, 30*time.Second)
return err
}
// addJob adds the reference to a job and its packet stream to the internal map of packet streams.
func (c *Client) addJob(handle string, packets chan *packet.Packet) {
c.jobLock.Lock()
defer c.jobLock.Unlock()
c.jobs[handle] = packets
}
// getJob returns the reference to channel for a specific job based off of its handle.
func (c *Client) getJob(handle string) chan *packet.Packet {
c.jobLock.RLock()
defer c.jobLock.RUnlock()
return c.jobs[handle]
}
// deleteJob removes a job's packet stream from the internal map of ongoing jobs.
func (c *Client) deleteJob(handle string) {
c.jobLock.Lock()
defer c.jobLock.Unlock()
delete(c.jobs, handle)
}
// pingCounter is used to ensure uniqueness when multiple pings happen in the same nanosecond
var pingCounter atomic.Uint64
// generatePingToken creates a unique token for ping requests with "echo" prefix
// Uses PID + timestamp + counter to ensure uniqueness without error handling
func generatePingToken() string {
pid := os.Getpid()
now := time.Now().UnixNano()
return fmt.Sprintf("echo%d_%d_%d", pid, now, pingCounter.Add(1))
}
// Ping sends an ECHO_REQ packet to the server and waits for the corresponding ECHO_RES.
// It returns the echoed data or an error if the ping fails or times out.
func (c *Client) Ping(ctx context.Context) error {
if err := c.isReady(); err != nil {
return err
}
token := generatePingToken()
responseChan := make(chan struct{}, 1)
c.pingLock.Lock()
c.pings[token] = responseChan
c.pingLock.Unlock()
pack := &packet.Packet{
Code: packet.Req,
Type: packet.EchoReq,
Arguments: [][]byte{[]byte(token)},
}
buf, err := pack.MarshalBinary()
if err != nil {
c.pingLock.Lock()
delete(c.pings, token)
c.pingLock.Unlock()
return fmt.Errorf("failed to marshal ECHO_REQ packet: %w", err)
}
_, err = io.Copy(c.conn, bytes.NewBuffer(buf))
if err != nil {
c.pingLock.Lock()
delete(c.pings, token)
c.pingLock.Unlock()
// Mark connection as failed
c.connLock.Lock()
c.connErr = err
c.connLock.Unlock()
return fmt.Errorf("failed to send ECHO_REQ packet: %w", err)
}
select {
case <-responseChan:
return nil
case <-ctx.Done():
c.pingLock.Lock()
delete(c.pings, token)
c.pingLock.Unlock()
return ctx.Err()
}
}
// read attempts to read incoming packets from the gearman server to route them to the job
// they are intended for.
func (c *Client) read(ctx context.Context, scanner *bufio.Scanner) {
logger := slogctx.FromCtx(ctx)
defer func() {
if r := recover(); r != nil {
logger.Error("Gearman client panic recovered in read loop", slog.Any("panic", r))
}
}()
for scanner.Scan() {
select {
case <-ctx.Done():
logger.Debug("Read loop cancelled")
return
default:
}
pack := &packet.Packet{}
if err := pack.UnmarshalBinary(scanner.Bytes()); err != nil {
logger.Warn("Error parsing packet", slog.Any("error", err))
continue // Skip this packet and continue reading
}
// Use non-blocking send to prevent deadlock if channel is full
select {
case c.packets <- pack:
// Packet sent successfully
case <-ctx.Done():
logger.Debug("Read loop cancelled while sending packet")
return
default:
logger.Warn("Packet channel full, dropping packet")
}
}
if scanner.Err() != nil {
errMsg := scanner.Err().Error()
if strings.Contains(errMsg, "use of closed network connection") {
logger.Debug("Connection closed normally")
} else {
logger.Warn("Error scanning packets", slog.Any("error", scanner.Err()))
// Mark connection as failed
c.connLock.Lock()
c.connErr = scanner.Err()
c.connLock.Unlock()
c.failPendingJobs(ctx, scanner.Err())
}
}
}
// failPendingJobs fails all pending jobs when a scanner error occurs
func (c *Client) failPendingJobs(ctx context.Context, err error) {
logger := slogctx.FromCtx(ctx)
c.jobLock.Lock()
defer c.jobLock.Unlock()
for handle, packets := range c.jobs {
logger.Warn("Failing job due to scanner error",
slog.String("handle", handle),
slog.Any("error", err))
failPacket := &packet.Packet{
Type: packet.WorkFail,
Arguments: [][]byte{[]byte(handle)},
}
select {
case packets <- failPacket:
default:
logger.Warn("Could not send fail packet to job", slog.String("handle", handle))
}
}
}
// routePackets forwards incoming packets to the correct job.
func (c *Client) routePackets(ctx context.Context) {
logger := slogctx.FromCtx(ctx)
// operate on every packet that has been read
for {
select {
case pack := <-c.packets:
switch pack.Type {
case packet.EchoRes:
if len(pack.Arguments) == 0 {
logger.Warn("ECHO_RES packet received with no data")
continue
}
token := string(pack.Arguments[0])
// Look for a matching ping request
c.pingLock.Lock()
pingResponseChan, exists := c.pings[token]
if exists {
delete(c.pings, token)
}
c.pingLock.Unlock()
if exists {
// Send response back to waiting ping
select {
case pingResponseChan <- struct{}{}:
case <-ctx.Done():
return
}
} else {
logger.Warn("Received ECHO_RES for unknown token", slog.String("token", token))
}
continue
}
if len(pack.Arguments) == 0 {
logger.Warn("Packet read with no handle")
continue
}
handle := string(pack.Arguments[0])
switch pack.Type {
case packet.JobCreated:
// create a new channel to send packets for this job
packets := make(chan *packet.Packet)
// optimistically hope that the last job submitted is the same one that just started
pj := <-c.partialJobs
// hook up the job to its packet stream
j := job.NewWithContext(ctx, handle, pj.data, pj.warnings, packets)
// add the packet stream to the internal routing map
c.addJob(handle, packets)
// finally unblock the Submit() fn call
c.newJobs <- j
go func() {
defer close(packets)
defer c.deleteJob(handle)
j.Run()
}()
default:
// send the packet to the right job
pktStream := c.getJob(handle)
if pktStream != nil {
pktStream <- pack
} else {
logger.Warn("Packet read with unknown handle", slog.String("handle", handle))
}
}
case <-ctx.Done():
logger.Debug("Route packets loop cancelled")
return
}
}
}
// newClientWithoutStart creates a client but doesn't start background goroutines.
// This is used internally by SimpleClient.
func newClientWithoutStart(network, address string) *Client {
return &Client{
network: network,
address: address,
packets: make(chan *packet.Packet),
newJobs: make(chan *job.Job),
partialJobs: make(chan *partialJob),
jobs: make(map[string]chan *packet.Packet),
pings: make(map[string]chan struct{}),
}
}
// NewClient returns a new Gearman client pointing at the specified server.
// For v2 compatibility, this automatically starts the background goroutines.
func NewClient(network, address string) (*Client, error) {
c := newClientWithoutStart(network, address)
// Auto-start with background context for v2 compatibility
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
ctx := slogctx.With(context.Background(), logger)
err := c.Start(ctx)
if err != nil {
return nil, err
}
return c, nil
}