-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbroker.go
More file actions
63 lines (56 loc) · 1.44 KB
/
Copy pathbroker.go
File metadata and controls
63 lines (56 loc) · 1.44 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
package linda
import (
"errors"
neturl "net/url"
"time"
"strconv"
)
var (
UnknownBroker = errors.New("unknown broker scheme")
)
// Broker is message transport[MQ]
// it provides a unified API, support multi drivers
type Broker interface {
Connect(rawUrl string, timeout time.Duration) error
Close() error
MigrateExpiredJobs(queue string)
Reserve(queue string, timeout int64) (string, error)
Delete(queue, id string) error
Release(queue, id string, delay int64) error
Push(queue, id string) error
Later(queue, id string, delay int64) error
}
var brokerMaps = make(map[string]Broker)
// RegisterBroker is used to register brokers with scheme name
// You can use your own broker driver
func RegisterBroker(scheme string, broker Broker) {
if broker == nil {
panic("Register broker is nil")
}
brokerMaps[scheme] = broker
}
// NewBroker will get an instance of broker with url string
// if there is no matched scheme, return error
// now broker only support redis
func NewBroker(rawUrl string) (Broker, error) {
url, err := neturl.Parse(rawUrl)
if err != nil {
return nil, err
}
scheme := url.Scheme
timeout, err := strconv.Atoi(url.Query().Get("timeout"))
if err != nil {
timeout = 1000
}
if b, ok := brokerMaps[scheme]; ok {
err := b.Connect(rawUrl, time.Duration(timeout)*time.Millisecond)
if err != nil {
return nil, err
}
return b, nil
}
return nil, UnknownBroker
}
func init() {
RegisterBroker("redis", &RedisBroker{})
}