-
Notifications
You must be signed in to change notification settings - Fork 4
/
broker.go
30 lines (23 loc) · 723 Bytes
/
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
package broker
import (
"context"
"encoding/json"
)
// Broker broker interface
type Broker interface {
// Publish pub message to topic
Publish(ctx context.Context, topic string, msg interface{}, opts ...PubOption) error
// Subscribe sub message from topic + channel
Subscribe(ctx context.Context, topic string, channel string, subHandler SubHandler, opts ...SubOption) error
// Shutdown graceful shutdown broker
Shutdown(ctx context.Context) error
}
// SubHandler subscribe func
type SubHandler func(ctx context.Context, value []byte) error
// ParseMessage parse msg
func ParseMessage(msg interface{}) ([]byte, error) {
if s, ok := msg.(string); ok {
return []byte(s), nil
}
return json.Marshal(msg)
}