-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrelay.go
59 lines (48 loc) · 1.34 KB
/
relay.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
package nrelay
import (
"context"
"log"
"github.com/pkg/errors"
)
type Relay interface {
Run(context.Context) error
}
// check interface
var (
_ Relay = (*MultipleSourceSingleDestinationRelay)(nil)
)
type MultipleSourceSingleDestinationRelay struct {
topic string
src Source
dst Destination
prefixSize int
workerNum int
logger *log.Logger
}
func (r *MultipleSourceSingleDestinationRelay) Run(ctx context.Context) error {
r.logger.Printf("info: relay/forward stream start:%s", r.topic)
if err := r.src.Open(); err != nil {
return errors.WithStack(err)
}
if err := r.dst.Open(r.workerNum); err != nil {
return errors.WithStack(err)
}
if err := r.src.Subscribe(r.topic, r.prefixSize, r.dst.Workers()); err != nil {
return errors.WithStack(err)
}
<-ctx.Done()
r.logger.Printf("info: relay/forward stream stop:%s", r.topic)
if err := r.src.Unsubscribe(); err != nil {
return errors.WithStack(err)
}
if err := r.src.Close(); err != nil {
return errors.WithStack(err)
}
if err := r.dst.Close(); err != nil {
return errors.WithStack(err)
}
return nil
}
func NewMultipleSourceSingleDestinationRelay(topic string, src Source, dst Destination, prefix, num int, logger *log.Logger) *MultipleSourceSingleDestinationRelay {
return &MultipleSourceSingleDestinationRelay{topic, src, dst, prefix, num, logger}
}