-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
164 lines (129 loc) · 3.68 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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrpnng
import (
"context"
"errors"
"sync"
"time"
"github.com/xmidt-org/eventor"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/wrpnng/internal/processors/stopping"
"github.com/xmidt-org/wrpnng/internal/receiver"
"github.com/xmidt-org/wrpnng/internal/sender"
)
var (
errInvalidMsg = errors.New("invalid message")
)
// Server is a simple controller for managing a receiver and a set of senders.
//
// ingress and egress refer to the API side of the controller.
// - ingress describes the messages coming into the controller.
// - egress describes the messages leaving the controller.
//
// tx and rx refer to the network side of the controller.
// - tx describes the messages being sent out.
// - rx describes the messages being received.
type Server struct {
rOpts []receiver.Option
r *receiver.Receiver
sOpts []sender.Option
egress eventor.Eventor[wrp.Modifier]
senders senderMap
rxObservers wrp.Observers
txObservers wrp.Observers
ingressChain stopping.Processors
heartbeatInterval time.Duration
heartbeatCancel context.CancelFunc
wg sync.WaitGroup
lock sync.Mutex
}
var _ wrp.Processor = (*Server)(nil)
// NewServer creates a new Controller. The controller is not started until Start is
// called. The controller handles the registration message and sends heartbeats
// at regular intervals. The default heartbeat interval is 30 seconds.
func NewServer(opts ...ServerOption) (*Server, error) {
var srv Server
defaults := []ServerOption{
WithHeartbeatInterval(30 * time.Second),
}
vadors := []ServerOption{
createReceiver(),
createIngressChain(),
}
opts = append(defaults, opts...)
opts = append(opts, vadors...)
for _, opt := range opts {
if opt != nil {
if err := opt.apply(&srv); err != nil {
return nil, err
}
}
}
return &srv, nil
}
// Start begins listening for messages. It is idempotent.
func (srv *Server) Start() error {
srv.lock.Lock()
defer srv.lock.Unlock()
if srv.heartbeatCancel != nil {
return nil
}
ctx, cancel := context.WithCancel(context.Background())
srv.heartbeatCancel = cancel
srv.wg.Add(1)
go srv.sendHeartbeat(ctx)
return srv.r.Listen()
}
// Stop halts the controller. It is idempotent.
func (srv *Server) Stop() error {
srv.lock.Lock()
defer srv.lock.Unlock()
if srv.heartbeatCancel != nil {
srv.heartbeatCancel()
srv.heartbeatCancel = nil
}
err := errors.Join(
srv.r.Close(),
srv.senders.Close(),
)
srv.wg.Wait()
return err
}
// ProcessWRP is called when a message should be sent to the network.
func (srv *Server) ProcessWRP(ctx context.Context, msg wrp.Message) error {
return srv.ingressChain.ProcessWRP(ctx, msg)
}
func (srv *Server) handleRegisterMsg(_ context.Context, msg wrp.Message) error {
if msg.Type != wrp.ServiceRegistrationMessageType {
return wrp.ErrNotHandled
}
if msg.ServiceName == "" || msg.URL == "" {
return errInvalidMsg
}
opts := append(srv.sOpts, sender.WithURL(msg.URL))
return srv.senders.Upsert(msg.ServiceName, opts)
}
func (srv *Server) egressWRP(ctx context.Context, msg wrp.Message) error {
srv.egress.Visit(func(m wrp.Modifier) {
_, _ = m.ModifyWRP(ctx, msg)
})
return nil
}
// sendHeartbeat sends a ServiceAlive message at regular intervals until the
// context is canceled.
func (srv *Server) sendHeartbeat(ctx context.Context) {
defer srv.wg.Done()
msg := wrp.Message{
Type: wrp.ServiceAliveMessageType,
}
for {
select {
case <-ctx.Done():
return
case <-time.After(srv.heartbeatInterval):
srv.txObservers.ObserveWRP(ctx, msg)
_ = srv.senders.ProcessWRP(ctx, msg)
}
}
}