-
Notifications
You must be signed in to change notification settings - Fork 91
/
options_server.go
116 lines (93 loc) · 2.99 KB
/
options_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
package jsonrpc
import (
"context"
"reflect"
"time"
"golang.org/x/xerrors"
)
// note: we embed reflect.Type because proxy-structs are not comparable
type jsonrpcReverseClient struct{ reflect.Type }
type ParamDecoder func(ctx context.Context, json []byte) (reflect.Value, error)
type ServerConfig struct {
maxRequestSize int64
pingInterval time.Duration
paramDecoders map[reflect.Type]ParamDecoder
errors *Errors
reverseClientBuilder func(context.Context, *wsConn) (context.Context, error)
tracer Tracer
}
type ServerOption func(c *ServerConfig)
func defaultServerConfig() ServerConfig {
return ServerConfig{
paramDecoders: map[reflect.Type]ParamDecoder{},
maxRequestSize: DEFAULT_MAX_REQUEST_SIZE,
pingInterval: 5 * time.Second,
}
}
func WithParamDecoder(t interface{}, decoder ParamDecoder) ServerOption {
return func(c *ServerConfig) {
c.paramDecoders[reflect.TypeOf(t).Elem()] = decoder
}
}
func WithMaxRequestSize(max int64) ServerOption {
return func(c *ServerConfig) {
c.maxRequestSize = max
}
}
func WithServerErrors(es Errors) ServerOption {
return func(c *ServerConfig) {
c.errors = &es
}
}
func WithServerPingInterval(d time.Duration) ServerOption {
return func(c *ServerConfig) {
c.pingInterval = d
}
}
// WithTracer allows the instantiator to trace the method calls and results.
// This is useful for debugging a client-server interaction.
func WithTracer(l Tracer) ServerOption {
return func(c *ServerConfig) {
c.tracer = l
}
}
// WithReverseClient will allow extracting reverse client on **WEBSOCKET** calls.
// RP is a proxy-struct type, much like the one passed to NewClient.
func WithReverseClient[RP any](namespace string) ServerOption {
return func(c *ServerConfig) {
c.reverseClientBuilder = func(ctx context.Context, conn *wsConn) (context.Context, error) {
cl := client{
namespace: namespace,
paramEncoders: map[reflect.Type]ParamEncoder{},
}
// todo test that everything is closing correctly
cl.exiting = conn.exiting
requests := cl.setupRequestChan()
conn.requests = requests
calls := new(RP)
err := cl.provide([]interface{}{
calls,
})
if err != nil {
return nil, xerrors.Errorf("provide reverse client calls: %w", err)
}
return context.WithValue(ctx, jsonrpcReverseClient{reflect.TypeOf(calls).Elem()}, calls), nil
}
}
}
// ExtractReverseClient will extract reverse client from context. Reverse client for the type
// will only be present if the server was constructed with a matching WithReverseClient option
// and the connection was a websocket connection.
// If there is no reverse client, the call will return a zero value and `false`. Otherwise a reverse
// client and `true` will be returned.
func ExtractReverseClient[C any](ctx context.Context) (C, bool) {
c, ok := ctx.Value(jsonrpcReverseClient{reflect.TypeOf(new(C)).Elem()}).(*C)
if !ok {
return *new(C), false
}
if c == nil {
// something is very wrong, but don't panic
return *new(C), false
}
return *c, ok
}