-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_pool.go
40 lines (32 loc) · 868 Bytes
/
worker_pool.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
package letsrest
type CanSetResponse interface {
SetResponse(id string, response *Response, err error) error
}
type requestWithStore struct {
request *Request
r CanSetResponse
}
type WorkerPool interface {
AddRequest(*Request, CanSetResponse)
}
func NewWorkerPool(requester Requester) *ChanWorkerPool {
pool := &ChanWorkerPool{
requester: requester,
requestCh: make(chan *requestWithStore, 1000),
}
go pool.ListenForTasks()
return pool
}
type ChanWorkerPool struct {
requester Requester
requestCh chan *requestWithStore
}
func (wp *ChanWorkerPool) AddRequest(request *Request, r CanSetResponse) {
wp.requestCh <- &requestWithStore{request: request, r: r}
}
func (wp *ChanWorkerPool) ListenForTasks() {
for rs := range wp.requestCh {
resp, err := wp.requester.Do(rs.request.RequestData)
rs.r.SetResponse(rs.request.ID, resp, err)
}
}