functions: common concurrency stream for sync and async (#314)

* functions: add bounded concurrency

* functions: plug runners to sync and async interfaces

* functions: update documentation about the new env var

* functions: fix test flakiness

* functions: the runner is self-regulated, no need to set a number of runners

* functions: push the execution to the background on incoming requests

* functions: ensure async tasks are always on

* functions: add prioritization to tasks consumption

Ensure that Sync tasks are consumed before Async tasks. Also, fixes
termination races problems for free.

* functions: remove stale comments

* functions: improve mem availability calculation

* functions: parallel run for async tasks

* functions: check for memory availability before pulling async task

* functions: comment about rnr.hasAvailableMemory and sync.Cond

* functions: implement memory check for async runners using Cond vars

* functions: code grooming

- remove unnecessary goroutines
- fix stale docs
- reorganize import group

* Revert "functions: implement memory check for async runners using Cond vars"

This reverts commit 922e64032201a177c03ce6a46240925e3d35430d.

* Revert "functions: comment about rnr.hasAvailableMemory and sync.Cond"

This reverts commit 49ad7d52d341f12da9603b1a1df9d145871f0e0a.

* functions: set a minimum memory availability for sync

* functions: simplify the implementation by removing the priority queue

* functions: code grooming

- code deduplication
- review waitgroups Waits
This commit is contained in:
C Cirello
2016-11-18 18:23:26 +01:00
committed by Seif Lotfy سيف لطفي
parent c1f361dd0c
commit 9d06b6e687
13 changed files with 205 additions and 98 deletions

22
main.go
View File

@@ -23,7 +23,6 @@ const (
envDB = "db_url"
envPort = "port" // be careful, Gin expects this variable to be "port"
envAPIURL = "api_url"
envNumAsync = "num_async"
)
func init() {
@@ -37,7 +36,6 @@ func init() {
viper.SetDefault(envDB, fmt.Sprintf("bolt://%s/data/bolt.db?bucket=funcs", cwd))
viper.SetDefault(envPort, 8080)
viper.SetDefault(envAPIURL, fmt.Sprintf("http://127.0.0.1:%d", viper.GetInt(envPort)))
viper.SetDefault(envNumAsync, 1)
viper.AutomaticEnv() // picks up env vars automatically
logLevel, err := log.ParseLevel(viper.GetString("log_level"))
if err != nil {
@@ -82,18 +80,22 @@ func main() {
},
}
tasks := make(chan runner.TaskRequest)
svr.AddFunc(func(ctx context.Context) {
srv := server.New(ds, mqType, rnr)
runner.StartWorkers(ctx, rnr, tasks)
})
svr.AddFunc(func(ctx context.Context) {
srv := server.New(ds, mqType, rnr, tasks)
srv.Run(ctx)
})
apiURL, numAsync := viper.GetString(envAPIURL), viper.GetInt(envNumAsync)
log.Debug("async workers:", numAsync)
if numAsync > 0 {
svr.AddFunc(func(ctx context.Context) {
runner.RunAsyncRunner(ctx, apiURL, numAsync)
})
}
apiURL := viper.GetString(envAPIURL)
svr.AddFunc(func(ctx context.Context) {
runner.RunAsyncRunner(ctx, apiURL, tasks, rnr)
})
svr.Serve(ctx)
close(tasks)
}