summaryrefslogtreecommitdiff
path: root/pkg/pool/supervisor_pool.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-01-13 13:56:40 +0300
committerValery Piashchynski <[email protected]>2021-01-13 13:56:40 +0300
commit2eed81d8fdbf8ee5134bb3b3f4c11c63cf6d757c (patch)
tree362f0eacdf2373bf208441577c1e69b8337bd71e /pkg/pool/supervisor_pool.go
parent581bb5370c89d749b77209b841fec30544c16246 (diff)
Add IdleTTL test
Add removed state to the worker before it being removed
Diffstat (limited to 'pkg/pool/supervisor_pool.go')
-rwxr-xr-xpkg/pool/supervisor_pool.go23
1 files changed, 20 insertions, 3 deletions
diff --git a/pkg/pool/supervisor_pool.go b/pkg/pool/supervisor_pool.go
index 2e919eae..07fa7019 100755
--- a/pkg/pool/supervisor_pool.go
+++ b/pkg/pool/supervisor_pool.go
@@ -16,6 +16,9 @@ import (
const MB = 1024 * 1024
+// NSEC_IN_SEC nanoseconds in second
+const NSEC_IN_SEC int64 = 1000000000 //nolint:golint,stylecheck
+
type Supervised interface {
pool.Pool
// Start used to start watching process for all pool workers
@@ -186,14 +189,28 @@ func (sp *supervised) control() {
we are guessing that worker overlap idle time and has to be killed
*/
+ // 1610530005534416045 lu
+ // lu - now = -7811150814 - nanoseconds
+ // 7.8 seconds
// get last used unix nano
lu := workers[i].State().LastUsed()
+ // worker not used, skip
+ if lu == 0 {
+ continue
+ }
- // convert last used to unixNano and sub time.now
- res := int64(lu) - now.UnixNano()
+ // convert last used to unixNano and sub time.now to seconds
+ // negative number, because lu always in the past, except for the `back to the future` :)
+ res := ((int64(lu) - now.UnixNano()) / NSEC_IN_SEC) * -1
// maxWorkerIdle more than diff between now and last used
- if sp.cfg.IdleTTL-uint64(res) <= 0 {
+ // for example:
+ // After exec worker goes to the rest
+ // And resting for the 5 seconds
+ // IdleTTL is 1 second.
+ // After the control check, res will be 5, idle is 1
+ // 5 - 1 = 4, more than 0, YOU ARE FIRED (removed). Done.
+ if int64(sp.cfg.IdleTTL)-res <= 0 {
err = sp.pool.RemoveWorker(workers[i])
if err != nil {
sp.events.Push(events.PoolEvent{Event: events.EventSupervisorError, Payload: errors.E(op, err)})