Skip to content

Commit

Permalink
use a worker pool to add concurrency limit on gif renders (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob ODwyer <rob.odwyer@taplytics.com>
  • Loading branch information
robbles and Rob ODwyer authored Dec 13, 2023
1 parent 7d3882c commit 77b6677
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/BattlesnakeOfficial/exporter
go 1.17

require (
github.com/alitto/pond v1.8.3
github.com/disintegration/imaging v1.6.2
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4
github.com/fogleman/gg v1.3.0
Expand Down
24 changes: 2 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alitto/pond v1.8.3 h1:ydIqygCLVPqIX/USe5EaV/aSRXTRXDEI9JwuDdu+/xs=
github.com/alitto/pond v1.8.3/go.mod h1:CmvIIGd5jKLasGI3D87qDkQxjzChdKMmnXMg3fG6M6Q=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -24,37 +26,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
goji.io/v3 v3.0.0 h1:CXZWGMTie+4tdhKiEpOlrUW9hCc8jF4LHs94sWdfcgQ=
goji.io/v3 v3.0.0/go.mod h1:c02FFnNiVNCDo+DpR2IhBQpM9r5G1BG/MkHNTPUJ13U=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
3 changes: 1 addition & 2 deletions http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,10 @@ func handleGIFGame(w http.ResponseWriter, r *http.Request) {
}

func handleCommonGIFGame(w http.ResponseWriter, r *http.Request, width, height int) {

gameID := pat.Param(r, "game")
engineURL := r.URL.Query().Get("engine_url")

log.WithField("game", gameID).WithField("engine_url", engineURL).Info("exporting game")
log.WithField("game", gameID).WithField("engine_url", engineURL).Info("rendering gif for game")

game, err := engine.GetGame(gameID, engineURL)
if err != nil {
Expand Down
41 changes: 35 additions & 6 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ import (
"syscall"
"time"

"github.com/alitto/pond"
log "github.com/sirupsen/logrus"
"goji.io/v3"
"goji.io/v3/pat"
)

// How many requests can be queued up for any GIF render before we start rejecting with HTTP 429
// Half of the current max in-flight requests
const DEFAULT_RENDER_BACKLOG = 40

type Server struct {
router *goji.Mux
httpServer *http.Server
}

func NewServer() *Server {
log.WithField("size", runtime.NumCPU()).Info("Starting GIF render pool")
renderPool := pond.New(runtime.NumCPU(), DEFAULT_RENDER_BACKLOG)

mux := goji.NewMux()
mux.Use(Recovery) // captures panics

Expand All @@ -40,14 +48,13 @@ func NewServer() *Server {

mux.HandleFunc(pat.Get("/customizations/:type/:name.:ext"), withCaching(handleCustomization))

mux.HandleFunc(pat.Get("/games/:game/:size.gif"), withCaching(handleGIFGameDimensions))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame.txt"), withCaching(handleASCIIFrame))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame/:size.gif"), withCaching(handleGIFFrameDimensions))
mux.HandleFunc(pat.Get("/games/:game/:size.gif"), withConcurrencyLimit(renderPool, withCaching(handleGIFGameDimensions)))
mux.HandleFunc(pat.Get("/games/:game/gif"), withConcurrencyLimit(renderPool, withCaching(handleGIFGame)))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame/:size.gif"), withConcurrencyLimit(renderPool, withCaching(handleGIFFrameDimensions)))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame/gif"), withConcurrencyLimit(renderPool, withCaching(handleGIFFrame)))

// Deprecated and undocumented, remove these!
mux.HandleFunc(pat.Get("/games/:game/gif"), withCaching(handleGIFGame))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame.txt"), withCaching(handleASCIIFrame))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame/ascii"), withCaching(handleASCIIFrame))
mux.HandleFunc(pat.Get("/games/:game/frames/:frame/gif"), withCaching(handleGIFFrame))

return &Server{
router: mux,
Expand All @@ -57,6 +64,28 @@ func NewServer() *Server {
}
}

func withConcurrencyLimit(pool *pond.WorkerPool, wrappedHandler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
done := make(chan struct{})

// Try to submit a job to the pool asynchronously, and if it fails, reject the request
submitted := pool.TrySubmit(func() {
defer close(done)
wrappedHandler(w, r)
})

if !submitted {
close(done)
log.WithField("url", r.URL.String()).Print("No worker available from pool, rejecting request")
w.WriteHeader(http.StatusTooManyRequests)
return
} else {
// Block until the job that was submitted to the pool is done
<-done
}
}
}

func withCaching(wrappedHandler http.HandlerFunc) http.HandlerFunc {
appVersion, ok := os.LookupEnv("APP_VERSION")
if !ok {
Expand Down

0 comments on commit 77b6677

Please sign in to comment.