-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
225 lines (198 loc) · 5.59 KB
/
server.go
File metadata and controls
225 lines (198 loc) · 5.59 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package udsrpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"runtime/debug"
"sync"
)
// HandlerFunc handles a single Request. The returned value is marshaled
// as the Response.Result. To return a custom error code, return an
// *Error (the server forwards Code and Message verbatim). Any other
// non-nil error is sent as ErrCodeInternal with err.Error() as the
// message.
type HandlerFunc func(ctx context.Context, conn *Conn, params json.RawMessage) (any, error)
// Server multiplexes incoming Requests on accepted connections to
// registered HandlerFuncs and tracks all live clients so events can be
// broadcast.
//
// A zero-value Server is not usable — call NewServer.
type Server struct {
handlerMu sync.RWMutex
handlers map[string]HandlerFunc
clientMu sync.RWMutex
clients map[*Conn]struct{}
hookMu sync.RWMutex
onConnect func(*Conn)
onDisconnect func(*Conn)
// Logger receives accept errors and recovered panics. Defaults to
// log.Default(); set to a no-op logger to silence the server.
Logger *log.Logger
}
// NewServer returns a Server with no handlers registered.
func NewServer() *Server {
return &Server{
handlers: make(map[string]HandlerFunc),
clients: make(map[*Conn]struct{}),
Logger: log.Default(),
}
}
// Handle registers fn as the handler for method. Calling Handle with
// the same method twice replaces the previous handler.
func (s *Server) Handle(method string, fn HandlerFunc) {
s.handlerMu.Lock()
defer s.handlerMu.Unlock()
s.handlers[method] = fn
}
// OnConnect installs a hook called once per accepted connection,
// before any messages are read. The hook runs synchronously on the
// accept goroutine; expensive work should be dispatched elsewhere.
func (s *Server) OnConnect(fn func(*Conn)) {
s.hookMu.Lock()
defer s.hookMu.Unlock()
s.onConnect = fn
}
// OnDisconnect installs a hook called once per connection when it
// closes (cleanly or after error).
func (s *Server) OnDisconnect(fn func(*Conn)) {
s.hookMu.Lock()
defer s.hookMu.Unlock()
s.onDisconnect = fn
}
// Clients returns a snapshot slice of all currently-connected clients.
// Safe to call concurrently with Serve.
func (s *Server) Clients() []*Conn {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
out := make([]*Conn, 0, len(s.clients))
for c := range s.clients {
out = append(out, c)
}
return out
}
// Broadcast sends an Event to every connected client. Errors per
// connection are logged but do not abort the broadcast.
func (s *Server) Broadcast(eventType string, data any) {
s.BroadcastFunc(eventType, data, nil)
}
// BroadcastFunc sends an Event to every connected client for which
// predicate returns true. A nil predicate matches all clients.
func (s *Server) BroadcastFunc(eventType string, data any, predicate func(*Conn) bool) {
for _, c := range s.Clients() {
if predicate != nil && !predicate(c) {
continue
}
if err := c.SendEvent(eventType, data); err != nil {
s.logf("broadcast %q to %s: %v", eventType, c.RemoteAddr(), err)
}
}
}
// Serve accepts connections on l and dispatches their Requests to
// registered handlers until ctx is canceled or l.Close is called.
//
// On ctx cancel, Serve closes l (so the blocked Accept returns) and
// returns nil. Other accept errors are returned to the caller.
func (s *Server) Serve(ctx context.Context, l net.Listener) error {
// Close the listener when ctx cancels so the Accept loop unblocks.
go func() {
<-ctx.Done()
_ = l.Close()
}()
for {
conn, err := l.Accept()
if err != nil {
if ctx.Err() != nil || errors.Is(err, net.ErrClosed) {
return nil //nolint:nilerr // expected shutdown
}
return fmt.Errorf("accept: %w", err)
}
c := NewConn(conn)
s.addClient(c)
go s.serveConn(ctx, c)
}
}
func (s *Server) serveConn(ctx context.Context, c *Conn) {
defer func() {
if r := recover(); r != nil {
s.logf("connection %s panic: %v\n%s", c.RemoteAddr(), r, debug.Stack())
}
s.removeClient(c)
_ = c.Close()
s.hookMu.RLock()
fn := s.onDisconnect
s.hookMu.RUnlock()
if fn != nil {
fn(c)
}
}()
s.hookMu.RLock()
onConn := s.onConnect
s.hookMu.RUnlock()
if onConn != nil {
onConn(c)
}
for {
msg, err := c.ReceiveMessage()
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
return
}
// Send a parse error response only if we can identify the request id —
// which we cannot here. Just close.
s.logf("receive from %s: %v", c.RemoteAddr(), err)
return
}
if msg.Request == nil {
// Server ignores Responses and Events from clients.
continue
}
req := msg.Request
go s.dispatch(ctx, c, req)
}
}
func (s *Server) dispatch(ctx context.Context, c *Conn, req *Request) {
defer func() {
if r := recover(); r != nil {
s.logf("handler %q panic: %v\n%s", req.Method, r, debug.Stack())
_ = c.SendError(req.ID, ErrCodeInternal, "handler panic")
}
}()
s.handlerMu.RLock()
fn, ok := s.handlers[req.Method]
s.handlerMu.RUnlock()
if !ok {
_ = c.SendError(req.ID, ErrCodeNotFound, "method not found: "+req.Method)
return
}
result, err := fn(ctx, c, req.Params)
if err != nil {
var e *Error
if errors.As(err, &e) {
_ = c.SendError(req.ID, e.Code, e.Message)
return
}
_ = c.SendError(req.ID, ErrCodeInternal, err.Error())
return
}
_ = c.SendResponse(req.ID, result)
}
func (s *Server) addClient(c *Conn) {
s.clientMu.Lock()
s.clients[c] = struct{}{}
s.clientMu.Unlock()
}
func (s *Server) removeClient(c *Conn) {
s.clientMu.Lock()
delete(s.clients, c)
s.clientMu.Unlock()
}
func (s *Server) logf(format string, args ...any) {
if s.Logger == nil {
return
}
s.Logger.Printf(format, args...)
}