-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.go
97 lines (79 loc) · 2.09 KB
/
receiver.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
package pubsub
import (
"context"
"errors"
"sync/atomic"
"time"
"cloud.google.com/go/pubsub"
mq "github.com/uudashr/go-mq"
"google.golang.org/api/option"
)
const (
stateCreated int32 = iota
stateConnecting
stateListening
stateStopping
stateStopped
)
// Receiver receives message.
type Receiver struct {
projectID string
subscriptionID string
connectTimeout time.Duration
opts []option.ClientOption
recvCtx context.Context
stopRecv context.CancelFunc
state int32
}
// Listen to the incoming message.
func (r *Receiver) Listen(h mq.Handler) (retErr error) {
if !atomic.CompareAndSwapInt32(&r.state, stateCreated, stateConnecting) {
return errors.New("pubsub: not in created state")
}
defer atomic.StoreInt32(&r.state, stateStopped)
client, err := r.newClient()
if err != nil {
return err
}
defer func() {
if err = client.Close(); err != nil && retErr != nil {
retErr = err
}
}()
atomic.StoreInt32(&r.state, stateListening)
subscription := client.Subscription(r.subscriptionID)
defer r.stopRecv()
err = subscription.Receive(r.recvCtx, func(ctx context.Context, msg *pubsub.Message) {
wrap := &message{msg: msg}
h.Handle(wrap)
})
if err != context.Canceled {
return err
}
return nil
}
func (r *Receiver) newClient() (*pubsub.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.connectTimeout)
defer cancel()
return pubsub.NewClient(ctx, r.projectID, r.opts...)
}
// Stop the receiver.
func (r *Receiver) Stop() error {
if !atomic.CompareAndSwapInt32(&r.state, stateListening, stateStopping) {
return errors.New("pubsub: cannot stop non-listening receiver")
}
r.stopRecv()
return nil
}
// NewReceiver construct new Receiver.
func NewReceiver(projectID, subscriptionID string, connectTimeout time.Duration, opts ...option.ClientOption) (*Receiver, error) {
recvCtx, stopRecv := context.WithCancel(context.Background())
return &Receiver{
projectID: projectID,
subscriptionID: subscriptionID,
connectTimeout: connectTimeout,
opts: opts,
recvCtx: recvCtx,
stopRecv: stopRecv,
}, nil
}