-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsubscribe.go
170 lines (144 loc) · 4.29 KB
/
subscribe.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package pubsub
import (
"context"
"encoding/json"
"reflect"
"time"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var wait = make(chan bool)
// Subscribe starts a run loop with a Subscriber that listens to topics and
// waits for a syscall.SIGINT or syscall.SIGTERM
func Subscribe(s Subscriber) {
s.Setup(clients[0])
<-wait
}
func Shutdown() {
logrus.Infof("pubsub: Gracefully shutting down pubsub subscribers")
wait <- true
for _, c := range clients {
c.Provider.Shutdown()
}
}
// HandlerOptions defines the options for a subscriber handler
type HandlerOptions struct {
// The topic to subscribe to
Topic string
// The name of this subscriber/function
Name string
// The name of this subscriber/function's service
ServiceName string
// The function to invoke
Handler Handler
// A message deadline/timeout
Deadline time.Duration
// Concurrency sets the maximum number of msgs to be run concurrently
// default: 20
Concurrency int
// Auto Ack the message automatically if return err == nil
AutoAck bool
// Decode JSON objects from pubsub instead of protobuf
JSON bool
// StartFromBeginning starts a new subscriber from
// the beginning of messages available, if supported
StartFromBeginning bool
// Unique subscriber means that all subscribers will receive all messages
Unique bool
}
// On takes HandlerOptions and subscribes to a topic, waiting for a protobuf message
// calling the function when a message is received
func (c Client) On(opts HandlerOptions) {
if opts.Topic == "" {
panic("lile pubsub: topic must be set")
}
if opts.Name == "" {
panic("lile pubsub: name must be set")
}
if opts.ServiceName == "" {
opts.ServiceName = c.ServiceName
}
if opts.Handler == nil {
panic("lile pubsub: handler cannot be nil")
}
// Set some default options
if opts.Deadline == 0 {
opts.Deadline = 10 * time.Second
}
// Set some default concurrency
if opts.Concurrency == 0 {
opts.Concurrency = 20
}
// Reflection is slow, but this is done only once on subscriber setup
hndlr := reflect.TypeOf(opts.Handler)
if hndlr.Kind() != reflect.Func {
panic("lile pubsub: handler needs to be a func")
}
if hndlr.NumIn() != 3 {
panic(`lile pubsub: handler should be of format
func(ctx context.Context, obj *proto.Message, msg *Msg) error
but didn't receive enough args`)
}
if hndlr.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() {
panic(`lile pubsub: handler should be of format
func(ctx context.Context, obj *proto.Message, msg *Msg) error
but first arg was not context.Context`)
}
if !opts.JSON {
if !hndlr.In(1).Implements(reflect.TypeOf((*proto.Message)(nil)).Elem()) {
panic(`lile pubsub: handler should be of format
func(ctx context.Context, obj *proto.Message, msg *Msg) error
but second arg does not implement proto.Message interface`)
}
}
if hndlr.In(2) != reflect.TypeOf(&Msg{}) {
panic(`lile pubsub: handler should be of format
func(ctx context.Context, obj *proto.Message, msg *Msg) error
but third arg was not pubsub.Msg`)
}
if !hndlr.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
panic(`lile pubsub: handler should be of format
func(ctx context.Context, obj *proto.Message, msg *Msg) error
but output type is not error`)
}
fn := reflect.ValueOf(opts.Handler)
cb := func(ctx context.Context, m Msg) error {
var err error
obj := reflect.New(hndlr.In(1).Elem()).Interface()
if opts.JSON {
err = json.Unmarshal(m.Data, obj)
} else {
err = proto.Unmarshal(m.Data, obj.(proto.Message))
}
if err != nil {
return errors.Wrap(err, "lile pubsub: could not unmarshal message")
}
rtrn := fn.Call([]reflect.Value{
reflect.ValueOf(ctx),
reflect.ValueOf(obj),
reflect.ValueOf(&m),
})
if len(rtrn) == 0 {
return nil
}
erri := rtrn[0].Interface()
if erri != nil {
err = erri.(error)
}
return err
}
mw := chainSubscriberMiddleware(c.Middleware...)
c.Provider.Subscribe(opts, mw(opts, cb))
}
func chainSubscriberMiddleware(mw ...Middleware) func(opts HandlerOptions, next MsgHandler) MsgHandler {
return func(opts HandlerOptions, final MsgHandler) MsgHandler {
return func(ctx context.Context, m Msg) error {
last := final
for i := len(mw) - 1; i >= 0; i-- {
last = mw[i].SubscribeInterceptor(opts, last)
}
return last(ctx, m)
}
}
}