-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.go
More file actions
238 lines (205 loc) · 5.57 KB
/
plugin.go
File metadata and controls
238 lines (205 loc) · 5.57 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
226
227
228
229
230
231
232
233
234
235
236
237
238
package rpc
import (
"context"
"crypto/tls"
"encoding/json"
stderrors "errors"
"log/slog"
"net"
"net/http"
"strings"
"sync/atomic"
"connectrpc.com/grpcreflect"
"github.com/roadrunner-server/endure/v2/dep"
"github.com/roadrunner-server/errors"
)
// PluginName contains default plugin name.
const PluginName = "rpc"
// Plugin is an RPC service.
type Plugin struct {
cfg Config
log *slog.Logger
// set of the plugins, which are implement RPCer interface and can be plugged into the RR via RPC
plugins map[string]RPCer
listener net.Listener
server *http.Server
closed atomic.Bool
rrVersion string
// whole configuration
wcfg []byte
}
// RPCer declares the ability to expose a Connect-RPC service. Implementations
// typically delegate to a generated connect.NewXxxServiceHandler(impl).
type RPCer interface {
// Name of the plugin.
Name() string
// RPC returns the URL prefix and HTTP handler this plugin wants mounted on
// the rpc server's mux.
RPC() (string, http.Handler)
}
type Configurer interface {
// RRVersion returns current RR version
RRVersion() string
// Unmarshal returns the whole configuration
Unmarshal(out any) error
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if config section exists.
Has(name string) bool
}
type Logger interface {
NamedLogger(name string) *slog.Logger
}
// Init rpc service. Must return true if service is enabled.
func (s *Plugin) Init(cfg Configurer, log Logger) error {
const op = errors.Op("rpc_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(PluginName, &s.cfg)
if err != nil {
return errors.E(op, errors.Disabled, err)
}
// Init defaults
s.cfg.InitDefaults()
// Init pluggable plugins map
s.plugins = make(map[string]RPCer, 1)
// init logs
s.log = log.NamedLogger(PluginName)
// validate config
err = s.cfg.Valid()
if err != nil {
return errors.E(op, err)
}
var WholeCfg any
err = cfg.Unmarshal(&WholeCfg)
if err != nil {
return errors.E(op, err)
}
s.wcfg, err = json.Marshal(WholeCfg)
if err != nil {
return err
}
s.rrVersion = cfg.RRVersion()
return nil
}
// Serve serves the service.
func (s *Plugin) Serve() chan error {
const op = errors.Op("rpc_plugin_serve")
errCh := make(chan error, 1)
// register the rpc plugin's own API surface alongside discovered plugins
s.plugins[PluginName] = s
mux := http.NewServeMux()
services := make([]string, 0, len(s.plugins))
for name, rpcer := range s.plugins {
path, handler := rpcer.RPC()
if path == "" || handler == nil {
s.log.Warn("plugin returned empty rpc handler", "plugin", name)
continue
}
// http.ServeMux.Handle panics on patterns missing a leading slash.
if !strings.HasPrefix(path, "/") {
s.log.Warn("plugin rpc handler path must start with '/'", "plugin", name, "path", path)
continue
}
mux.Handle(path, handler)
// derive the gRPC service name from the mount path
// (`/<service>/<Method>` or `/<service>/`)
svc := strings.TrimPrefix(path, "/")
if i := strings.Index(svc, "/"); i >= 0 {
svc = svc[:i]
}
services = append(services, svc)
}
// gRPC server reflection so operators can list services with grpcurl
if len(services) > 0 {
reflector := grpcreflect.NewStaticReflector(services...)
rpath, rhandler := grpcreflect.NewHandlerV1(reflector)
mux.Handle(rpath, rhandler)
rpath, rhandler = grpcreflect.NewHandlerV1Alpha(reflector)
mux.Handle(rpath, rhandler)
}
listener, err := s.cfg.Listener()
if err != nil {
errCh <- errors.E(op, err)
return errCh
}
s.listener = listener
protocols := new(http.Protocols)
protocols.SetHTTP1(true)
protocols.SetUnencryptedHTTP2(true)
s.server = &http.Server{
Handler: mux,
Protocols: protocols,
ReadHeaderTimeout: s.cfg.RequestTimeout,
ReadTimeout: s.cfg.RequestTimeout,
}
useTLS := s.cfg.TLS != nil
if useTLS {
cert, err := tls.LoadX509KeyPair(s.cfg.TLS.Cert, s.cfg.TLS.Key)
if err != nil {
_ = s.listener.Close()
errCh <- errors.E(op, err)
return errCh
}
tlsProto := new(http.Protocols)
tlsProto.SetHTTP1(true)
tlsProto.SetHTTP2(true)
s.server.Protocols = tlsProto
s.server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h2", "http/1.1"},
}
}
s.log.Debug("plugin was started",
"address", s.cfg.Listen,
"tls", useTLS,
"services", services,
)
go func() {
var serveErr error
if useTLS {
serveErr = s.server.ServeTLS(s.listener, "", "")
} else {
serveErr = s.server.Serve(s.listener)
}
if serveErr != nil && !stderrors.Is(serveErr, http.ErrServerClosed) && !s.closed.Load() {
errCh <- errors.E(op, serveErr)
}
}()
return errCh
}
// Stop stops the service.
func (s *Plugin) Stop(ctx context.Context) error {
const op = errors.Op("rpc_plugin_stop")
s.closed.Store(true)
if s.server == nil {
return nil
}
if err := s.server.Shutdown(ctx); err != nil {
return errors.E(op, err)
}
return nil
}
func (s *Plugin) Weight() uint {
return 100
}
// Name contains service name.
func (s *Plugin) Name() string {
return PluginName
}
// RPC exposes the rpc plugin's own API surface (Config, Version) so it is
// served alongside collected plugins.
func (s *Plugin) RPC() (string, http.Handler) {
return newSelfHandlers(s.wcfg, s.rrVersion)
}
// Collects all plugins which implement Name + RPCer interfaces
func (s *Plugin) Collects() []*dep.In {
return []*dep.In{
dep.Fits(func(p any) {
rpcer := p.(RPCer)
s.plugins[rpcer.Name()] = rpcer
}, (*RPCer)(nil)),
}
}