-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
84 lines (71 loc) · 1.49 KB
/
main.go
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
package main
import (
"context"
"fmt"
"math/rand"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/go-playground/errors/v5"
"github.com/go-playground/relay-client-go"
"github.com/go-playground/relay-client-go/producer"
)
var (
enqueued uint64
)
func init() {
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 1024
http.DefaultTransport.(*http.Transport).MaxConnsPerHost = 1024
}
type payload struct {
Key string `json:"key"`
}
func main() {
ctx := context.Background()
client, err := relay.New[payload, struct{}](relay.Config{
BaseURL: "http://127.0.0.1:8080",
})
if err != nil {
panic(err)
}
queue := "test-queue"
p, err := producer.New(producer.Config[payload, struct{}, *relay.Client[payload, struct{}]]{
Enqueuer: client,
})
if err != nil {
panic(err)
}
wg := new(sync.WaitGroup)
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
err := p.Enqueue(ctx, relay.Job[payload, struct{}]{
ID: strconv.Itoa(rand.Intn(1_000_000_000)),
Queue: queue,
Timeout: 30,
MaxRetries: 0,
Payload: payload{Key: "I'm a little teapot"},
})
if err != nil {
var e relay.ErrJobExits
if errors.As(err, &e) {
continue
}
panic("enqueue" + err.Error())
}
atomic.AddUint64(&enqueued, 1)
}
}()
}
// report counts
for {
time.Sleep(time.Second)
oldEnqueued := atomic.SwapUint64(&enqueued, 0)
fmt.Println("Enqueued:", oldEnqueued)
}
wg.Wait()
}