-
Notifications
You must be signed in to change notification settings - Fork 3
/
factories.go
155 lines (141 loc) · 4.41 KB
/
factories.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
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
package gocelery
import (
"errors"
"fmt"
neturl "net/url"
"strconv"
"strings"
backendiface "github.com/itzujun/gocelery/backends/iface"
redisbackend "github.com/itzujun/gocelery/backends/redis"
amqpbroker "github.com/itzujun/gocelery/brokers/amqp"
eagerbroker "github.com/itzujun/gocelery/brokers/eager"
brokeriface "github.com/itzujun/gocelery/brokers/iface"
redisbroker "github.com/itzujun/gocelery/brokers/redis"
"github.com/itzujun/gocelery/config"
)
func BrokerFactory(cnf *config.Config) (brokeriface.Broker, error) {
if strings.HasPrefix(cnf.Broker, "amqp://") {
return amqpbroker.New(cnf), nil
}
if strings.HasPrefix(cnf.Broker, "amqps://") {
return amqpbroker.New(cnf), nil
}
if strings.HasPrefix(cnf.Broker, "redis://") {
parts := strings.Split(cnf.Broker, "redis://")
if len(parts) != 2 {
return nil, fmt.Errorf(
"redis broker connection string should be in format redis://host:port, instead got %s",
cnf.Broker,
)
}
redisHost, redisPassword, redisDB, err := ParseRedisURL(cnf.Broker)
if err != nil {
return nil, err
}
return redisbroker.New(cnf, redisHost, redisPassword, "", redisDB), nil
}
if strings.HasPrefix(cnf.Broker, "redis+socket://") {
redisSocket, redisPassword, redisDB, err := ParseRedisSocketURL(cnf.Broker)
if err != nil {
return nil, err
}
return redisbroker.New(cnf, "", redisPassword, redisSocket, redisDB), nil
}
if strings.HasPrefix(cnf.Broker, "eager") {
return eagerbroker.New(), nil
}
return nil, fmt.Errorf("factory failed with broker URL: %v", cnf.Broker)
}
func BackendFactory(cnf *config.Config) (backendiface.Backend, error) {
if strings.HasPrefix(cnf.ResultBackend, "redis://") {
redisHost, redisPassword, redisDB, err := ParseRedisURL(cnf.ResultBackend)
if err != nil {
return nil, err
}
return redisbackend.New(cnf, redisHost, redisPassword, "", redisDB), nil
}
return nil, fmt.Errorf("factory failed with result backend: %v", cnf.ResultBackend)
}
func ParseRedisURL(url string) (host, password string, db int, err error) {
var u *neturl.URL
u, err = neturl.Parse(url)
if err != nil {
return
}
if u.Scheme != "redis" {
err = errors.New("no redis scheme found")
return
}
if u.User != nil {
var exists bool
password, exists = u.User.Password()
if !exists {
password = u.User.Username()
}
}
host = u.Host
parts := strings.Split(u.Path, "/")
if len(parts) == 1 {
db = 0 //default redis db
} else {
db, err = strconv.Atoi(parts[1])
if err != nil {
db, err = 0, nil //ignore err here
}
}
return
}
func ParseRedisSocketURL(url string) (path, password string, db int, err error) {
parts := strings.Split(url, "redis+socket://")
if parts[0] != "" {
err = errors.New("no redis scheme found")
return
}
if len(parts) != 2 {
err = fmt.Errorf("redis socket connection string should be in format redis+socket://password@/path/to/file.sock:/db, instead got %s", url)
return
}
remainder := parts[1]
parts = strings.SplitN(remainder, "@", 2)
if len(parts) == 2 {
password = parts[0]
remainder = parts[1]
} else {
remainder = parts[0]
}
parts = strings.SplitN(remainder, ":", 2)
path = parts[0]
if path == "" {
err = fmt.Errorf("redis socket connection string should be in format redis+socket://password@/path/to/file.sock:/db, instead got %s", url)
return
}
if len(parts) == 2 {
remainder = parts[1]
}
parts = strings.SplitN(remainder, "/", 2)
if len(parts) == 2 {
db, _ = strconv.Atoi(parts[1])
}
return
}
func ParseGCPPubSubURL(url string) (string, string, error) {
parts := strings.Split(url, "gcppubsub://")
if parts[0] != "" {
return "", "", errors.New("no gcppubsub scheme found")
}
if len(parts) != 2 {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
remainder := parts[1]
parts = strings.Split(remainder, "/")
if len(parts) == 2 {
if len(parts[0]) == 0 {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
if len(parts[1]) == 0 {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
return parts[0], parts[1], nil
}
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}