-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
128 lines (107 loc) · 2.9 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package nrelay
import (
"context"
"log"
"github.com/nats-io/nats.go"
"github.com/octu0/chanque"
"github.com/pkg/errors"
)
type Server interface {
Run(context.Context) error
}
type ServerOptFunc func(*serverOpt)
type serverOpt struct {
relayConf RelayConfig
executor *chanque.Executor
logger *log.Logger
natsOpts []nats.Option
}
func ServerOptRelayConfig(conf RelayConfig) ServerOptFunc {
return func(opt *serverOpt) {
opt.relayConf = conf
}
}
func ServerOptExecutor(executor *chanque.Executor) ServerOptFunc {
return func(opt *serverOpt) {
opt.executor = executor
}
}
func ServerOptLogger(logger *log.Logger) ServerOptFunc {
return func(opt *serverOpt) {
opt.logger = logger
}
}
func ServerOptNatsOptions(natsOpts ...nats.Option) ServerOptFunc {
return func(opt *serverOpt) {
opt.natsOpts = natsOpts
}
}
// check interface
var (
_ (Server) = (*DefaultServer)(nil)
)
type DefaultServer struct {
opt *serverOpt
}
func (s *DefaultServer) Run(ctx context.Context) error {
sourceNatsUrls := make([]string, 0, 2)
sourceNatsUrls = append(sourceNatsUrls, s.opt.relayConf.PrimaryUrl)
if 0 < len(s.opt.relayConf.SecondaryUrl) {
sourceNatsUrls = append(sourceNatsUrls, s.opt.relayConf.SecondaryUrl)
}
relays := make([]Relay, 0, len(s.opt.relayConf.Topics)*len(sourceNatsUrls))
for topic, conf := range s.opt.relayConf.Topics {
src := NewMultipleSource(sourceNatsUrls, s.opt.natsOpts, s.opt.logger)
dst := NewSingleDestination(s.opt.executor, s.opt.relayConf.NatsUrl, s.opt.natsOpts, s.opt.logger)
relay := NewMultipleSourceSingleDestinationRelay(topic, src, dst, conf.PrefixSize, conf.WorkerNum, s.opt.logger)
relays = append(relays, relay)
}
return runRelays(ctx, s.opt.executor, s.opt.logger, relays)
}
func NewDefaultServer(funcs ...ServerOptFunc) *DefaultServer {
opt := new(serverOpt)
for _, fn := range funcs {
fn(opt)
}
return &DefaultServer{opt}
}
func runRelays(ctx context.Context, executor *chanque.Executor, logger *log.Logger, relays []Relay) error {
logger.Printf("info: relay server stared")
sctx, cancel := context.WithCancel(ctx)
defer cancel()
errCh := make(chan error, 0)
subexec := executor.SubExecutor()
for i, relay := range relays {
subexec.Submit(func(idx int, r Relay) chanque.Job {
return func() {
logger.Printf("debug: relay[%d] started", idx)
defer logger.Printf("debug: relay[%d] stoped", idx)
if err := r.Run(sctx); err != nil {
select {
case errCh <- errors.WithStack(err):
default:
// already shutdown
}
}
}
}(i, relay))
}
select {
case <-ctx.Done():
logger.Printf("info: relay server shutdown, context done")
cancel()
subexec.Wait()
close(errCh)
if err := <-errCh; err != nil {
return errors.WithStack(err)
}
return nil
case err := <-errCh:
logger.Printf("info: relay server shutdown, error happen")
cancel()
subexec.Wait()
close(errCh)
return errors.WithStack(err)
}
return nil
}