forked from lukasjarosch/genki
/
broker.go
46 lines (39 loc) · 1.01 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 broker
import (
"context"
"sync"
)
// Message is the core structure of the broker. It abstract's away the concrete broker message
// implementations (e.g. amqp.Delivery).
type Message struct {
Context context.Context
Body []byte
}
// Broker is the interface which defines the client connection(s) to a concrete broker like amqp
type Broker interface {
Publisher
Subscriber
Initialize() error
Disconnect() error
Consume(group *sync.WaitGroup)
HasConsumer() bool
}
// Subscriber can subscribe to exchanges with a routingKey
type Subscriber interface {
Subscribe(exchange, routingKey string, handler Handler) error
}
// Publisher can publish messages with a routingKey to the message-broker
type Publisher interface {
Publish(exchange, routingKey string, message *Message) error
EnsureExchange(exchange string)
}
type Handler func(event Event)
// Event abstraction
type Event interface {
Message() *Message
Ack()
Nack(requeue bool)
QueueName() string
RoutingKey() string
SetContext(ctx context.Context)
}