Skip to content

Commit 195e12f

Browse files
committed
sandbox: reduce container starvation
Creating a container in the sandbox takes 500ms to 1s. The sandbox was creating containers serially, but serving requests in parallel. This means that we can be starved for workers with a trivial number of requests. In addition, the sandbox in production is not CPU bound, meaning we probably have room to do some extra work while serving a request. This CL introduces a worker pool to create containers. It also changes the readyContainer chan to unbuffered to avoid having twice as many containers as we expect while idle (the container waiting to be sent plus those already in the channel's buffer). Updates golang/go#25224 Updates golang/go#38530 Change-Id: I0e535cf65409c3dbf32329577a1c0687c2614a0d Reviewed-on: https://go-review.googlesource.com/c/playground/+/229981 Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 560694f commit 195e12f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

sandbox/sandbox.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func main() {
117117
}
118118
log.Printf("Go playground sandbox starting.")
119119

120-
readyContainer = make(chan *Container, *numWorkers)
120+
readyContainer = make(chan *Container)
121121
runSem = make(chan struct{}, *numWorkers)
122122
go handleSignals()
123123

@@ -148,7 +148,7 @@ func main() {
148148
mux.Handle("/", ochttp.WithRouteTag(http.HandlerFunc(rootHandler), "/"))
149149
mux.Handle("/run", ochttp.WithRouteTag(http.HandlerFunc(runHandler), "/run"))
150150

151-
go makeWorkers()
151+
makeWorkers()
152152
go internal.PeriodicallyDo(context.Background(), 10*time.Second, func(ctx context.Context, _ time.Time) {
153153
countDockerContainers(ctx)
154154
})
@@ -364,6 +364,12 @@ func runInGvisor() {
364364
func makeWorkers() {
365365
ctx := context.Background()
366366
stats.Record(ctx, mMaxContainers.M(int64(*numWorkers)))
367+
for i := 0; i < *numWorkers; i++ {
368+
go workerLoop(ctx)
369+
}
370+
}
371+
372+
func workerLoop(ctx context.Context) {
367373
for {
368374
c, err := startContainer(ctx)
369375
if err != nil {

0 commit comments

Comments
 (0)