-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplugin.go
More file actions
286 lines (234 loc) · 6.51 KB
/
plugin.go
File metadata and controls
286 lines (234 loc) · 6.51 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package http
import (
"context"
stdlog "log"
"net/http"
"sync"
_ "google.golang.org/genproto/protobuf/ptype" //nolint:revive,nolintlint
rrcontext "github.com/roadrunner-server/context"
"github.com/roadrunner-server/endure/v2/dep"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/http/v5/api"
"github.com/roadrunner-server/http/v5/config"
"github.com/roadrunner-server/http/v5/handler"
"github.com/roadrunner-server/http/v5/servers"
"github.com/roadrunner-server/pool/pool/static_pool"
"github.com/roadrunner-server/pool/state/process"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
jprop "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
const (
// PluginName declares plugin name.
PluginName = "http"
MB uint64 = 1024 * 1024
// configuration sections
sectionHTTPS = "http.ssl"
sectionHTTP2 = "http.http2"
sectionFCGI = "http.fcgi"
sectionUploads = "http.uploads"
// RrMode RR_HTTP env variable key (internal) if the HTTP presents
RrMode = "RR_MODE"
RrModeHTTP = "http"
)
// Plugin manages pool, http servers. The main http plugin structure
type Plugin struct {
mu sync.RWMutex
// otel propagators
prop propagation.TextMapPropagator
// plugins
server api.Server
log *zap.Logger
// stdlog passed to the http/https/fcgi servers to log their internal messages
stdLog *stdlog.Logger
experimentalFeatures bool
// http configuration
cfg *config.Config
// middlewares to chain
mdwr map[string]api.Middleware
// Pool which attached to all servers
pool api.Pool
// servers RR handler
handler *handler.Handler
// metrics
statsExporter *StatsExporter
// servers
servers []servers.InternalServer[any]
}
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (p *Plugin) Init(cfg api.Configurer, log api.Logger, srv api.Server) error {
const op = errors.Op("http_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(op, errors.Disabled)
}
err := p.unmarshal(cfg)
if err != nil {
return errors.E(op, err)
}
err = p.cfg.InitDefaults()
if err != nil {
return errors.E(op, err)
}
// check if we have experimental features enabled
p.experimentalFeatures = cfg.Experimental()
// get permissions
p.cfg.UID = srv.UID()
p.cfg.GID = srv.GID()
// rr logger (via plugin)
p.log = log.NamedLogger(PluginName)
// use time and date in UTC format
p.stdLog = stdlog.New(NewStdAdapter(p.log), "http_plugin: ", stdlog.Ldate|stdlog.Ltime|stdlog.LUTC)
p.mdwr = make(map[string]api.Middleware)
if !p.cfg.EnableHTTP() && !p.cfg.EnableTLS() && !p.cfg.EnableFCGI() {
return errors.E(op, errors.Disabled)
}
// initialize statsExporter
p.statsExporter = newWorkersExporter(p)
p.server = srv
p.servers = make([]servers.InternalServer[any], 0, 4)
p.prop = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
return nil
}
// Serve serves the svc.
func (p *Plugin) Serve() chan error {
errCh := make(chan error, 2)
p.mu.Lock()
defer p.mu.Unlock()
var err error
p.pool, err = p.server.NewPool(context.Background(), p.cfg.Pool, map[string]string{RrMode: RrModeHTTP}, p.log)
if err != nil {
errCh <- err
return errCh
}
p.handler, err = handler.NewHandler(
p.cfg,
p.pool,
p.log,
)
if err != nil {
errCh <- err
return errCh
}
// initialize servers based on the configuration
err = p.initServers()
if err != nil {
errCh <- err
return errCh
}
// apply access_logs, max_request, redirect middleware if specified by user
p.applyBundledMiddleware()
// start all servers
for i := range p.servers {
go func(idx int) {
errSt := p.servers[idx].Serve(p.mdwr, p.cfg.Middleware)
if errSt != nil {
errCh <- errSt
return
}
}(i)
}
return errCh
}
// Stop stops the http.
func (p *Plugin) Stop(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
doneCh := make(chan struct{}, 1)
go func() {
for _, srv := range p.servers {
if srv != nil {
srv.Stop()
}
}
if p.pool != nil {
switch pp := p.pool.(type) {
case *static_pool.Pool:
if pp != nil {
pp.Destroy(ctx)
}
default:
// pool is nil, nothing to do
}
}
doneCh <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-doneCh:
return nil
}
}
// ServeHTTP handles connection using set of middleware and pool PSR-7 server.
func (p *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if val, ok := r.Context().Value(rrcontext.OtelTracerNameKey).(string); ok {
tp := trace.SpanFromContext(r.Context()).TracerProvider()
ctx, span := tp.Tracer(val, trace.WithSchemaURL(semconv.SchemaURL),
trace.WithInstrumentationVersion(otelhttp.Version)).
Start(r.Context(), PluginName, trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
// inject
p.prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
r = r.WithContext(ctx)
}
// protect the case when the user sends Reset, and we are replacing handler with pool
p.mu.RLock()
p.handler.ServeHTTP(w, r)
p.mu.RUnlock()
_ = r.Body.Close()
}
// Workers returns slice with the process states for the workers
func (p *Plugin) Workers() []*process.State {
p.mu.RLock()
defer p.mu.RUnlock()
if p.pool == nil {
return nil
}
workers := p.pool.Workers()
ps := make([]*process.State, 0, len(workers))
for i := range workers {
state, err := process.WorkerProcessState(workers[i])
if err != nil {
return nil
}
ps = append(ps, state)
}
return ps
}
// Name returns endure.Named interface implementation
func (p *Plugin) Name() string {
return PluginName
}
// Reset destroys the old pool and replaces it with new one, waiting for old pool to die
func (p *Plugin) Reset() error {
const op = errors.Op("http_plugin_reset")
p.mu.Lock()
defer p.mu.Unlock()
p.log.Info("reset signal was received")
if p.pool == nil {
p.log.Info("pool is nil, nothing to reset")
return nil
}
err := p.pool.Reset(context.Background())
if err != nil {
return errors.E(op, err)
}
p.log.Info("plugin was successfully reset")
return nil
}
// Collects collecting http middlewares
func (p *Plugin) Collects() []*dep.In {
return []*dep.In{
dep.Fits(func(pp any) {
mdw := pp.(api.Middleware)
// just to be safe
p.mu.Lock()
p.mdwr[mdw.Name()] = mdw
p.mu.Unlock()
}, (*api.Middleware)(nil)),
}
}