-
Notifications
You must be signed in to change notification settings - Fork 80
/
server.go
189 lines (151 loc) · 3.93 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package rabbitmq
import (
"context"
"fmt"
"net/url"
"sync"
"github.com/go-kratos/kratos/v2/transport"
"github.com/tx7do/kratos-transport/broker"
"github.com/tx7do/kratos-transport/broker/rabbitmq"
"github.com/tx7do/kratos-transport/utils"
)
var (
_ transport.Server = (*Server)(nil)
_ transport.Endpointer = (*Server)(nil)
)
type SubscriberMap map[string]broker.Subscriber
type SubscribeOption struct {
handler broker.Handler
binder broker.Binder
subscribeOptions []broker.SubscribeOption
}
type SubscribeOptionMap map[string]*SubscribeOption
type Server struct {
broker.Broker
brokerOpts []broker.Option
subscribers SubscriberMap
subscriberOpts SubscribeOptionMap
sync.RWMutex
started bool
baseCtx context.Context
err error
keepAlive *utils.KeepAliveService
enableKeepAlive bool
}
func NewServer(opts ...ServerOption) *Server {
srv := &Server{
baseCtx: context.Background(),
subscribers: SubscriberMap{},
subscriberOpts: SubscribeOptionMap{},
brokerOpts: []broker.Option{},
started: false,
keepAlive: utils.NewKeepAliveService(nil),
enableKeepAlive: true,
}
srv.init(opts...)
srv.Broker = rabbitmq.NewBroker(srv.brokerOpts...)
return srv
}
func (s *Server) init(opts ...ServerOption) {
for _, o := range opts {
o(s)
}
}
func (s *Server) Name() string {
return string(KindRabbitMQ)
}
func (s *Server) Start(ctx context.Context) error {
if s.err != nil {
return s.err
}
if s.started {
return nil
}
s.err = s.Init()
if s.err != nil {
LogErrorf("init broker failed: [%s]", s.err.Error())
return s.err
}
s.err = s.Connect()
if s.err != nil {
return s.err
}
if s.enableKeepAlive {
go func() {
_ = s.keepAlive.Start()
}()
}
LogInfof("server listening on: %s", s.Address())
s.err = s.doRegisterSubscriberMap()
if s.err != nil {
return s.err
}
s.baseCtx = ctx
s.started = true
return nil
}
func (s *Server) Stop(_ context.Context) error {
LogInfo("server stopping")
s.started = false
return s.Disconnect()
}
func (s *Server) Endpoint() (*url.URL, error) {
if s.err != nil {
return nil, s.err
}
return s.keepAlive.Endpoint()
}
func (s *Server) RegisterSubscriber(ctx context.Context, routingKey string, handler broker.Handler, binder broker.Binder, opts ...broker.SubscribeOption) error {
s.Lock()
defer s.Unlock()
if s.baseCtx == nil {
s.baseCtx = context.Background()
}
if ctx == nil {
ctx = s.baseCtx
}
// context必须要插入到头部,否则后续传入的配置会被覆盖掉。
opts = append([]broker.SubscribeOption{broker.WithSubscribeContext(ctx)}, opts...)
if s.started {
return s.doRegisterSubscriber(routingKey, handler, binder, opts...)
} else {
s.subscriberOpts[routingKey] = &SubscribeOption{handler: handler, binder: binder, subscribeOptions: opts}
}
return nil
}
func RegisterSubscriber[T any](srv *Server, ctx context.Context, topic string, handler func(context.Context, string, broker.Headers, *T) error, opts ...broker.SubscribeOption) error {
return srv.RegisterSubscriber(ctx,
topic,
func(ctx context.Context, event broker.Event) error {
switch t := event.Message().Body.(type) {
case *T:
if err := handler(ctx, event.Topic(), event.Message().Headers, t); err != nil {
return err
}
default:
return fmt.Errorf("unsupported type: %T", t)
}
return nil
},
func() broker.Any {
var t T
return &t
},
opts...,
)
}
func (s *Server) doRegisterSubscriber(routingKey string, handler broker.Handler, binder broker.Binder, opts ...broker.SubscribeOption) error {
sub, err := s.Subscribe(routingKey, handler, binder, opts...)
if err != nil {
return err
}
s.subscribers[routingKey] = sub
return nil
}
func (s *Server) doRegisterSubscriberMap() error {
for topic, opt := range s.subscriberOpts {
_ = s.doRegisterSubscriber(topic, opt.handler, opt.binder, opt.subscribeOptions...)
}
s.subscriberOpts = SubscribeOptionMap{}
return nil
}