-
Notifications
You must be signed in to change notification settings - Fork 34
/
broker.go
46 lines (43 loc) · 1.17 KB
/
broker.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
package engine
import (
"github.com/pkg/errors"
"github.com/runabol/tork/conf"
"github.com/runabol/tork/mq"
)
func (e *Engine) initBroker() error {
bt := conf.StringDefault("broker.type", mq.BROKER_INMEMORY)
broker, err := e.createBroker(bt)
if err != nil {
return err
}
for _, cb := range e.onBrokerInit {
if err := cb(broker); err != nil {
return err
}
}
e.broker = broker
return nil
}
func (e *Engine) createBroker(btype string) (mq.Broker, error) {
p, ok := e.mqProviders[btype]
if ok {
return p()
}
switch btype {
case "inmemory":
return mq.NewInMemoryBroker(), nil
case "rabbitmq":
rb, err := mq.NewRabbitMQBroker(
conf.StringDefault("broker.rabbitmq.url", "amqp://guest:guest@localhost:5672/"),
mq.WithConsumerTimeoutMS(conf.DurationDefault("broker.rabbitmq.consumer.timeout", mq.RABBITMQ_DEFAULT_CONSUMER_TIMEOUT)),
mq.WithManagementURL(conf.String("broker.rabbitmq.management.url")),
mq.WithDurableQueues(conf.Bool("broker.rabbitmq.durable.queues")),
)
if err != nil {
return nil, errors.Wrapf(err, "unable to connect to RabbitMQ")
}
return rb, nil
default:
return nil, errors.Errorf("invalid broker type: %s", btype)
}
}