-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaling.go
67 lines (58 loc) · 1.48 KB
/
scaling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"time"
)
type Poller struct {
Ticker *time.Ticker
quitCh chan struct{}
}
// check for the incoming job
func GetAvailableWorker(P *Pool) *Worker {
if P.Config.Scaling {
if len(P.Workers) == 0 {
fmt.Println("No workers Available")
fmt.Println("Trying to Scale Up")
// Scale Up to MaxWorkers
if P.Current < P.Config.MaxWorkers {
fmt.Println("Current number of workers is less than Max workers")
w := SpawnWorker()
go w.Start(&P.Wg, P.Workers)
P.Current += 1
P.Workers <- w
} else {
fmt.Println("Max Workers Limit reached.. Cannot Scale Up")
}
}
}
return <-P.Workers
}
// Check at regular intervals
func (P *Pool) PollStatus() {
Outer:
for {
select {
case <-P.Poller.Ticker.C:
// Case - 1A (No of workers is equal to Min workers)
if len(P.Workers) == P.Config.MinWorkers {
// Minimality reached
fmt.Println("Current number of Workers is equal to MinWorkers")
fmt.Println("Taking No action")
continue Outer
} else if len(P.Workers) > P.Config.MinWorkers {
// Case - 1B
// More workers than needed workers, Scale down the worker
fmt.Println("More number of Workers than MinWorkers")
worker := <-P.Workers
fmt.Println("Removing the Worker with id:", worker.ID)
worker.KillChan <- struct{}{}
P.Current -= 1
continue Outer
}
case <-P.Poller.quitCh:
fmt.Println("Stopping the Poller")
// TODO: Maybe Removing the workers from the pool??
break Outer
}
}
}