-
Notifications
You must be signed in to change notification settings - Fork 11
/
queue.go
122 lines (99 loc) · 2.78 KB
/
queue.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
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
package middleware
import (
"context"
"errors"
"net/http"
"runtime"
"time"
"github.com/webx-top/echo"
"golang.org/x/sync/semaphore"
)
type (
// QueueConfig defines the configuration for the queue
QueueConfig struct {
Skipper echo.Skipper
QueueSize int
Workers int
QueueTimeout time.Duration
WorkerTimeout time.Duration
}
)
// errors
var (
// ErrQueueTimeout is thrown when the request waits more than QueueTimeout to be queued
ErrQueueTimeout = echo.NewHTTPError(http.StatusTooManyRequests, "queue limit reached")
// ErrWorkerTimeout is thrown when the request waits more than WorkerTimeout for a worker to start processing it
ErrWorkerTimeout = echo.NewHTTPError(http.StatusRequestTimeout, "request took too long to process")
)
// DefaultQueueConfig defines default values for QueueConfig
var DefaultQueueConfig = QueueConfig{
Skipper: echo.DefaultSkipper,
QueueSize: 100,
Workers: runtime.GOMAXPROCS(0),
QueueTimeout: 30 * time.Second,
WorkerTimeout: 10 * time.Second,
}
/*
Queue returns a queue middleware
e := echo.New()
e.GET("/queue", func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}, middleware.Queue())
*/
func Queue() echo.MiddlewareFunc {
config := DefaultQueueConfig
return QueueWithConfig(config)
}
/*
QueueWithConfig returns a queue middleware
e := echo.New()
config := middleware.QueueConfig{
Skipper: DefaultSkipper,
QueueSize: 2,
Workers: 1,
QueueTimeout: 20 * time.Second,
WorkerTimeout: 10 * time.Second,
}
e.GET("/queue", func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}, middleware.QueueWithConfig(config))
*/
func QueueWithConfig(config QueueConfig) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = DefaultQueueConfig.Skipper
}
queueSemaphore := semaphore.NewWeighted(int64(config.QueueSize))
workersSemaphore := semaphore.NewWeighted(int64(config.Workers))
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
if config.Skipper(c) {
return next.Handle(c)
}
ctxQueue, queueCancel := context.WithTimeout(c.StdContext(), config.QueueTimeout)
if err := queueSemaphore.Acquire(ctxQueue, 1); err != nil {
queueCancel()
if errors.Is(err, context.DeadlineExceeded) {
return ErrQueueTimeout
}
return err
}
ctxWorker, workerCancel := context.WithTimeout(ctxQueue, config.WorkerTimeout)
defer func() {
workerCancel()
queueCancel()
}()
if err := workersSemaphore.Acquire(ctxWorker, 1); err != nil {
queueSemaphore.Release(1)
if errors.Is(err, context.DeadlineExceeded) {
return ErrWorkerTimeout
}
return err
}
defer func() {
workersSemaphore.Release(1)
queueSemaphore.Release(1)
}()
return next.Handle(c)
})
}
}