From 4fc0fde729bf2e53e7660559879a38a82c5d428a Mon Sep 17 00:00:00 2001 From: scoiatael Date: Wed, 8 Mar 2017 14:14:02 +0100 Subject: [PATCH] Set concurrency to runtime NumCpus --- actions/context.go | 3 +++ actions/http_server.go | 2 +- config.go | 5 +++++ http/iris.go | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/actions/context.go b/actions/context.go index e5ba374..2d6f2bc 100644 --- a/actions/context.go +++ b/actions/context.go @@ -1,6 +1,7 @@ package actions import ( + "context" "encoding/json" "github.com/scoiatael/archai/http" @@ -12,6 +13,7 @@ type HttpHandler interface { Get(string, func(http.GetContext)) Post(string, func(http.PostContext)) Run(string) error + Stop(context.Context) } type Context interface { @@ -21,6 +23,7 @@ type Context interface { HandleErr(error) HttpHandler() HttpHandler Telemetry() telemetry.Datadog + Concurrency() int } type Action interface { diff --git a/actions/http_server.go b/actions/http_server.go index ea869eb..6f387e8 100644 --- a/actions/http_server.go +++ b/actions/http_server.go @@ -45,7 +45,7 @@ func (wj *WriteJob) Run(c Context) { func (hs HttpServer) Run(c Context) error { handler := c.HttpHandler() jobs := make(chan WriteJob, 50) - for w := 0; w < 3; w++ { + for w := 0; w < c.Concurrency(); w++ { go writer(jobs, c) } handler.Get("/stream/:id", func(ctx http.GetContext) { diff --git a/config.go b/config.go index 4f12f95..d3085ce 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "runtime" "github.com/scoiatael/archai/actions" "github.com/scoiatael/archai/http" @@ -98,3 +99,7 @@ func (c Config) Run() error { func (c Config) PrettyPrint() { util.PrettyPrint(c) } + +func (c Config) Concurrency() int { + return runtime.NumCPU() +} diff --git a/http/iris.go b/http/iris.go index e541cab..218e464 100644 --- a/http/iris.go +++ b/http/iris.go @@ -1,6 +1,8 @@ package http import ( + "context" + "gopkg.in/kataras/iris.v6" "github.com/scoiatael/archai/simplejson" @@ -81,3 +83,7 @@ func (h *IrisHandler) Run(addr string) error { return nil } + +func (h *IrisHandler) Stop(ctx context.Context) { + h.framework.Shutdown(ctx) +}