-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
70 lines (55 loc) · 1.34 KB
/
client.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
package psub
import (
"context"
"fmt"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
)
type PsubConnection struct {
*pubsub.Client
topics map[string]*pubsub.Topic // topics that have custom publishSetting
isLog bool
newClientFunc func() (*pubsub.Client, error)
}
type Subscriber struct {
ID string
Sub *pubsub.Subscription
CancelFunc context.CancelFunc
cfg *SubscribeOption
}
var Connection *PsubConnection // for singleton usage
func Connect(ctx context.Context, projectID string, opts ...option.ClientOption) (*PsubConnection, error) {
var err error
f := func() (*pubsub.Client, error) { return pubsub.NewClient(context.Background(), projectID, opts...) }
c, err := f()
if err != nil {
return nil, err
}
if Connection == nil {
Connection = &PsubConnection{
Client: c,
newClientFunc: f,
topics: make(map[string]*pubsub.Topic),
}
}
return &PsubConnection{
Client: c,
topics: make(map[string]*pubsub.Topic),
newClientFunc: f,
}, nil
}
func newClient(client *pubsub.Client) *PsubConnection {
newClient := &PsubConnection{
Client: client,
topics: make(map[string]*pubsub.Topic),
}
return newClient
}
func (c *PsubConnection) SetLog(isLog bool) {
c.isLog = isLog
}
func (c *PsubConnection) Log(a ...any) {
if c.isLog {
fmt.Println(a...)
}
}