-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathmain.go
More file actions
424 lines (366 loc) · 12.2 KB
/
main.go
File metadata and controls
424 lines (366 loc) · 12.2 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"context"
"errors"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/gin-contrib/cors"
limits "github.com/gin-contrib/size"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
middleware "github.com/oapi-codegen/gin-middleware"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/auth"
authcache "github.com/e2b-dev/infra/packages/api/internal/cache/auth"
"github.com/e2b-dev/infra/packages/api/internal/handlers"
customMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware"
metricsMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware/otel/metrics"
tracingMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware/otel/tracing"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/shared/pkg/env"
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
)
const (
serviceName = "orchestration-api"
maxMultipartMemory = 1 << 27 // 128 MiB
maxUploadLimit = 1 << 28 // 256 MiB
maxReadTimeout = 75 * time.Second
maxWriteTimeout = 75 * time.Second
idleTimeout = 620 * time.Second
defaultPort = 80
)
var (
commitSHA string
expectedMigrationTimestamp string
)
func NewGinServer(ctx context.Context, logger *zap.Logger, apiStore *handlers.APIStore, swagger *openapi3.T, port int) *http.Server {
// Clear out the servers array in the swagger spec, that skips validating
// that server names match. We don't know how this thing will be run.
swagger.Servers = nil
r := gin.New()
r.Use(
// We use custom otel gin middleware because we want to log 4xx errors in the otel
customMiddleware.ExcludeRoutes(
tracingMiddleware.Middleware(serviceName),
"/health",
"/sandboxes/:sandboxID/refreshes",
"/templates/:templateID/builds/:buildID/logs",
"/templates/:templateID/builds/:buildID/status",
),
customMiddleware.IncludeRoutes(
metricsMiddleware.Middleware(serviceName),
"/sandboxes",
"/sandboxes/:sandboxID",
"/sandboxes/:sandboxID/pause",
"/sandboxes/:sandboxID/resume",
),
gin.Recovery(),
)
config := cors.DefaultConfig()
// Allow all origins
config.AllowAllOrigins = true
config.AllowHeaders = []string{
// Default headers
"Origin",
"Content-Length",
"Content-Type",
// API Key header
"Authorization",
"X-API-Key",
// Custom headers sent from SDK
"browser",
"lang",
"lang_version",
"machine",
"os",
"package_version",
"processor",
"publisher",
"release",
"sdk_runtime",
"system",
}
r.Use(cors.New(config))
// Create a team API Key auth validator
AuthenticationFunc := auth.CreateAuthenticationFunc(
apiStore.Tracer,
apiStore.GetTeamFromAPIKey,
apiStore.GetUserFromAccessToken,
apiStore.GetUserIDFromSupabaseToken,
apiStore.GetTeamFromSupabaseToken,
)
// Use our validation middleware to check all requests against the
// OpenAPI schema.
r.Use(
limits.RequestSizeLimiter(maxUploadLimit),
middleware.OapiRequestValidatorWithOptions(swagger,
&middleware.Options{
ErrorHandler: utils.ErrorHandler,
MultiErrorHandler: utils.MultiErrorHandler,
Options: openapi3filter.Options{
AuthenticationFunc: AuthenticationFunc,
// Handle multiple errors as MultiError type
MultiError: true,
},
}),
)
r.Use(
// Request logging must be executed after authorization (if required) is done,
// so that we can log team ID.
customMiddleware.ExcludeRoutes(
func(c *gin.Context) {
var teamID = ""
// Get team from context, use TeamContextKey
teamInfo := c.Value(auth.TeamContextKey)
if teamInfo != nil {
teamID = teamInfo.(authcache.AuthTeamInfo).Team.ID.String()
}
reqLogger := logger
if teamID != "" {
reqLogger = logger.With(zap.String("team_id", teamID))
}
ginzap.Ginzap(reqLogger, time.RFC3339Nano, true)(c)
},
"/health",
"/sandboxes/:sandboxID/refreshes",
"/templates/:templateID/builds/:buildID/logs",
"/templates/:templateID/builds/:buildID/status",
),
)
// We now register our store above as the handler for the interface
api.RegisterHandlersWithOptions(r, apiStore, api.GinServerOptions{
ErrorHandler: func(c *gin.Context, err error, statusCode int) {
utils.ErrorHandler(c, err.Error(), statusCode)
},
})
r.MaxMultipartMemory = maxMultipartMemory
s := &http.Server{
Handler: r,
Addr: fmt.Sprintf("0.0.0.0:%d", port),
// Configure timeouts to be greater than the proxy timeouts.
// https://github.com/golang/go/issues/47007
ReadTimeout: maxReadTimeout,
WriteTimeout: maxWriteTimeout,
IdleTimeout: idleTimeout,
BaseContext: func(net.Listener) context.Context { return ctx },
}
return s
}
func run() int {
ctx, cancel := context.WithCancel(context.Background()) // root context
defer cancel()
// TODO: additional improvements to signal handling/shutdown:
// - provide access to root context in the signal handling
// context so request scoped work can start background tasks
// without needing to make an unattached context.
// - provide mechanism to inform shutdown that background
// work has completed (waitgroup, counter, etc.) to avoid
// exiting early.
var (
port int
debug string
)
flag.IntVar(&port, "port", defaultPort, "Port for test HTTP server")
flag.StringVar(&debug, "debug", "false", "is debug")
flag.Parse()
instanceID := uuid.New().String()
if !env.IsLocal() {
otlpCleanup := telemetry.InitOTLPExporter(ctx, serviceName, commitSHA, instanceID)
defer otlpCleanup(ctx)
}
logger := zap.Must(logger.NewLogger(ctx, logger.LoggerConfig{
ServiceName: serviceName,
IsInternal: true,
IsDebug: env.IsDebug(),
Cores: []zapcore.Core{logger.GetOTELCore(serviceName)},
}))
defer logger.Sync()
zap.ReplaceGlobals(logger)
sbxLoggerExternal := sbxlogger.NewLogger(
ctx,
sbxlogger.SandboxLoggerConfig{
ServiceName: serviceName,
IsInternal: false,
CollectorAddress: os.Getenv("LOGS_COLLECTOR_ADDRESS"),
},
)
defer sbxLoggerExternal.Sync()
sbxlogger.SetSandboxLoggerExternal(sbxLoggerExternal)
sbxLoggerInternal := sbxlogger.NewLogger(
ctx,
sbxlogger.SandboxLoggerConfig{
ServiceName: serviceName,
IsInternal: true,
CollectorAddress: os.Getenv("LOGS_COLLECTOR_ADDRESS"),
},
)
defer sbxLoggerInternal.Sync()
sbxlogger.SetSandboxLoggerInternal(sbxLoggerInternal)
// Convert the string expectedMigrationTimestamp to a int64
expectedMigration, err := strconv.ParseInt(expectedMigrationTimestamp, 10, 64)
if err != nil {
// If expectedMigrationTimestamp is not set, we set it to 0
logger.Warn("failed to parse expected migration timestamp", zap.Error(err))
expectedMigration = 0
}
err = utils.CheckMigrationVersion(expectedMigration)
if err != nil {
logger.Fatal("failed to check migration version", zap.Error(err))
}
logger.Info("Starting API service...", zap.String("commit_sha", commitSHA), zap.String("instance_id", instanceID))
if debug != "true" {
gin.SetMode(gin.ReleaseMode)
}
swagger, err := api.GetSwagger()
if err != nil {
// this will call os.Exit: defers won't run, but none
// need to yet. Change this if this is called later.
logger.Error("error loading swagger spec", zap.Error(err))
return 1
}
var cleanupFns []func(context.Context) error
exitCode := &atomic.Int32{}
cleanupOp := func() {
// some cleanup functions do work that requires a context. passing shutdown a
// specific context here so that all timeout configuration is in one place.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
start := time.Now()
// doing shutdown in parallel to avoid
// unintentionally: creating shutdown ordering
// effects.
cwg := &sync.WaitGroup{}
count := 0
for idx := range cleanupFns {
if cleanup := cleanupFns[idx]; cleanup != nil {
cwg.Add(1)
count++
go func(
op func(context.Context) error,
idx int,
) {
defer cwg.Done()
if err := op(ctx); err != nil {
exitCode.Add(1)
logger.Error("cleanup operation error", zap.Int("index", idx), zap.Error(err))
}
}(cleanup, idx)
cleanupFns[idx] = nil
}
}
if count == 0 {
logger.Info("no cleanup operations")
return
}
logger.Info("running cleanup operations", zap.Int("count", count))
cwg.Wait() // this doesn't have a timeout
logger.Info("cleanup operations completed", zap.Int("count", count), zap.Duration("duration", time.Since(start)))
}
cleanupOnce := &sync.Once{}
cleanup := func() { cleanupOnce.Do(cleanupOp) }
defer cleanup()
// Create an instance of our handler which satisfies the generated interface
// (use the outer context rather than the signal handling
// context so it doesn't exit first.)
apiStore := handlers.NewAPIStore(ctx)
cleanupFns = append(cleanupFns, apiStore.Close)
// pass the signal context so that handlers know when shutdown is happening.
s := NewGinServer(ctx, logger, apiStore, swagger, port)
// ////////////////////////
//
// Start the HTTP service
// set up the signal handlers so that we can trigger a
// shutdown of the HTTP service when the process catches the
// specified signal. The parent context isn't canceled until
// after the HTTP service returns, to avoid terminating
// connections to databases and other upstream services before
// the HTTP server has shut down.
signalCtx, sigCancel := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
defer sigCancel()
wg := &sync.WaitGroup{}
// in the event of an unhandled panic *still* wait for the
// HTTP service to terminate:
defer wg.Wait()
wg.Add(1)
go func() {
defer wg.Done()
// make sure to cancel the parent context before this
// goroutine returns, so that in the case of a panic
// or error here, the other thread won't block until
// signaled.
defer cancel()
logger.Info("http service starting", zap.Int("port", port))
// Serve HTTP until shutdown.
err := s.ListenAndServe()
switch {
case errors.Is(err, http.ErrServerClosed):
logger.Info("http service shutdown successfully", zap.Int("port", port))
case err != nil:
exitCode.Add(1)
logger.Error("http service encountered error", zap.Int("port", port), zap.Error(err))
default:
// this probably shouldn't happen...
logger.Info("http service exited without error", zap.Int("port", port))
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-signalCtx.Done()
// Start returning 503s for health checks
// to signal that the service is shutting down.
// This is a bit of a hack, but this way we can properly propagate
// the health status to the load balancer.
apiStore.Healthy = false
// Skip the delay in local environment for instant shutdown
if !env.IsLocal() {
time.Sleep(15 * time.Second)
}
// if the parent context `ctx` is canceled the
// shutdown will return early. This should only happen
// if there's an error in starting the http service
// (and would be a noop), or if there's an unhandled
// panic and defers start running, _probably_ won't
// even have a chance to return before the program
// returns.
if err := s.Shutdown(ctx); err != nil {
exitCode.Add(1)
logger.Error("http service shutdown error", zap.Int("port", port), zap.Error(err))
}
}()
// wait for the HTTP service to complete shutting down first
// before doing other cleanup, we're listening for the signal
// termination in one of these background threads.
wg.Wait()
// call cleanup explicitly because defers (from above) do not
// run on os.Exit.
cleanup()
// TODO: wait for additional work to coalesce
//
// currently we only wait for the HTTP handlers to return, and
// then cancel the remaining context and run all of the
// cleanup functions. Background go routines at this point
// terminate. Would need to have a goroutine pool or worker
// coordinator running to manage and track that work.
// Exit, with appropriate code.
return int(exitCode.Load())
}
func main() {
os.Exit(run())
}