-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtunnel.go
70 lines (52 loc) · 1.63 KB
/
tunnel.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 gcppubsub
import (
"context"
"cloud.google.com/go/pubsub"
"github.com/pkg/errors"
"github.com/batchcorp/plumber-schemas/build/go/protos/opts"
"github.com/batchcorp/plumber-schemas/build/go/protos/records"
"github.com/batchcorp/plumber/tunnel"
"github.com/batchcorp/plumber/validate"
)
func (g *GCPPubSub) Tunnel(ctx context.Context, tunnelOpts *opts.TunnelOptions, tunnelSvc tunnel.ITunnel, errorCh chan<- *records.ErrorRecord) error {
if err := validateTunnelOptions(tunnelOpts); err != nil {
return errors.New("unable to validate write options")
}
if err := tunnelSvc.Start(ctx, "GCP PubSub", errorCh); err != nil {
return errors.Wrap(err, "unable to create tunnel")
}
t := g.client.Topic(tunnelOpts.GcpPubsub.Args.TopicId)
outboundCh := tunnelSvc.Read()
for {
select {
case outbound := <-outboundCh:
result := t.Publish(ctx, &pubsub.Message{
Data: outbound.Blob,
})
if _, err := result.Get(ctx); err != nil {
g.log.Errorf("Unable to replay message: %s", err)
continue
}
g.log.Debugf("Replayed message to GCP Pubsub topic '%s' for replay '%s'", tunnelOpts.GcpPubsub.Args.TopicId, outbound.ReplayId)
case <-ctx.Done():
g.log.Debug("context cancelled")
return nil
}
}
return nil
}
func validateTunnelOptions(tunnelOpts *opts.TunnelOptions) error {
if tunnelOpts == nil {
return validate.ErrEmptyTunnelOpts
}
if tunnelOpts.GcpPubsub == nil {
return validate.ErrEmptyBackendGroup
}
if tunnelOpts.GcpPubsub.Args == nil {
return validate.ErrEmptyBackendArgs
}
if tunnelOpts.GcpPubsub.Args.TopicId == "" {
return errors.New("Topic ID cannot be empty")
}
return nil
}