/
pubsub.go
106 lines (88 loc) · 1.73 KB
/
pubsub.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
package endpoint
import (
"context"
"fmt"
"sync"
"time"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
)
const pubsubExpiresAfter = time.Second * 30
// SQSConn is an endpoint connection
type PubSubConn struct {
mu sync.Mutex
ep Endpoint
svc *pubsub.Client
topic *pubsub.Topic
ex bool
t time.Time
}
func (conn *PubSubConn) close() {
if conn.svc != nil {
conn.svc.Close()
conn.svc = nil
}
}
// Send sends a message
func (conn *PubSubConn) Send(msg string) error {
conn.mu.Lock()
defer conn.mu.Unlock()
if conn.ex {
return errExpired
}
ctx := context.Background()
conn.t = time.Now()
if conn.svc == nil {
var creds option.ClientOption
var svc *pubsub.Client
var err error
credPath := conn.ep.PubSub.CredPath
if credPath != "" {
creds = option.WithCredentialsFile(credPath)
svc, err = pubsub.NewClient(ctx, conn.ep.PubSub.Project, creds)
} else {
svc, err = pubsub.NewClient(ctx, conn.ep.PubSub.Project)
}
if err != nil {
fmt.Println(err)
return err
}
topic := svc.Topic(conn.ep.PubSub.Topic)
conn.svc = svc
conn.topic = topic
}
// Send message
res := conn.topic.Publish(ctx, &pubsub.Message{
Data: []byte(msg),
})
_, err := res.Get(ctx)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func (conn *PubSubConn) Expired() bool {
conn.mu.Lock()
defer conn.mu.Unlock()
if !conn.ex {
if time.Since(conn.t) > pubsubExpiresAfter {
conn.close()
conn.ex = true
}
}
return conn.ex
}
// ExpireNow forces the connection to expire
func (conn *PubSubConn) ExpireNow() {
conn.mu.Lock()
defer conn.mu.Unlock()
conn.close()
conn.ex = true
}
func newPubSubConn(ep Endpoint) *PubSubConn {
return &PubSubConn{
ep: ep,
t: time.Now(),
}
}