/
boilerplate.go
64 lines (53 loc) · 1.52 KB
/
boilerplate.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
package psub
import (
"time"
"cloud.google.com/go/pubsub"
"github.com/func25/slicesol/slicesol"
)
type boilerplate struct {
client *pubsub.Client
// not using function receiver for custom purpose
SubConfig func(topicID string) pubsub.SubscriptionConfig
SubInfo func(topicID string, subIDs []string) SubsInfo
UpsertCmd func(topicID string, subIDs []string) UpsertCmd
}
func NewBoiler(c *pubsub.Client) boilerplate {
b := &boilerplate{
client: c,
}
b.SubConfig = boilerSubConfig(b)
b.SubInfo = boilerSubInfo(b)
b.UpsertCmd = boilerUpsertCmd(b)
return *b
}
func (b *boilerplate) GetClient() *pubsub.Client {
return b.client
}
func boilerSubConfig(b *boilerplate) func(topicID string) pubsub.SubscriptionConfig {
return func(topicID string) pubsub.SubscriptionConfig {
return pubsub.SubscriptionConfig{
Topic: b.client.Topic(topicID),
RetryPolicy: &pubsub.RetryPolicy{
MinimumBackoff: 1 * time.Second,
MaximumBackoff: 10 * time.Second,
},
AckDeadline: 30 * time.Second,
}
}
}
func boilerSubInfo(b *boilerplate) func(topicID string, subIDs []string) SubsInfo {
return func(topicID string, subIDs []string) SubsInfo {
return SubsInfo{
Subs: slicesol.Map(subIDs, func(s string) Sub { return Sub{ID: s} }),
DefaultConfig: b.SubConfig(topicID),
}
}
}
func boilerUpsertCmd(b *boilerplate) func(topicID string, subIDs []string) UpsertCmd {
return func(topicID string, subIDs []string) UpsertCmd {
return UpsertCmd{
TopicID: topicID,
UpsertSubs: b.SubInfo(topicID, subIDs),
}
}
}