-
Notifications
You must be signed in to change notification settings - Fork 71
/
tunnel.go
69 lines (54 loc) · 1.75 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
package pulsar
import (
"context"
"github.com/apache/pulsar-client-go/pulsar"
"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 (p *Pulsar) Tunnel(ctx context.Context, tunnelOpts *opts.TunnelOptions, tunnelSvc tunnel.ITunnel, errorCh chan<- *records.ErrorRecord) error {
if err := validateTunnelOptions(tunnelOpts); err != nil {
return errors.Wrap(err, "invalid tunnel options")
}
llog := p.log.WithField("pkg", "pulsar/tunnel")
producer, err := p.client.CreateProducer(pulsar.ProducerOptions{Topic: tunnelOpts.Pulsar.Args.Topic})
if err != nil {
return errors.Wrap(err, "unable to create Pulsar producer")
}
if err := tunnelSvc.Start(ctx, "Apache Pulsar", errorCh); err != nil {
return errors.Wrap(err, "unable to create tunnel")
}
outboundCh := tunnelSvc.Read()
for {
select {
case outbound := <-outboundCh:
if _, err := producer.Send(ctx, &pulsar.ProducerMessage{Payload: outbound.Blob}); err != nil {
err = errors.Wrap(err, "Unable to replay message")
llog.Error(err)
return err
}
llog.Debugf("Replayed message to Pulsar topic '%s' for replay '%s'",
tunnelOpts.Pulsar.Args.Topic, outbound.ReplayId)
case <-ctx.Done():
llog.Debug("context cancelled")
return nil
}
}
}
func validateTunnelOptions(tunnelOpts *opts.TunnelOptions) error {
if tunnelOpts == nil {
return validate.ErrEmptyTunnelOpts
}
if tunnelOpts.Pulsar == nil {
return validate.ErrEmptyBackendGroup
}
if tunnelOpts.Pulsar.Args == nil {
return validate.ErrEmptyBackendArgs
}
if tunnelOpts.Pulsar.Args.Topic == "" {
return ErrEmptyTopic
}
return nil
}