-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (114 loc) · 2.57 KB
/
main.go
File metadata and controls
140 lines (114 loc) · 2.57 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
_ "expvar"
"fmt"
"math"
"math/rand"
"sync"
"time"
)
const maxWorkers = 5
var originalRequest int
type job struct {
name string
duration time.Duration
}
func doWork(id int, j job) {
fmt.Printf("worker%d: started %s, working for %fs\n", id, j.name, j.duration.Seconds())
time.Sleep(j.duration)
fmt.Printf("worker%d: completed %s!\n", id, j.name)
}
func main() {
var currentRequests int
wg := &sync.WaitGroup{}
for {
var size int
var count int = 0
jobs := make(chan job)
currentRequests = 12
requests := currentRequests
//////
fmt.Println(requests)
{
originalRequest = requests
requests = int(math.Floor(float64(requests / 5)))
requests = requests * 5
if requests == 0 {
size = 0
} else {
size = maxWorkers
}
wg.Add(size)
for j := 1; j <= size; j++ {
go func(j int) {
defer wg.Done()
for c := range jobs {
doWork(j, c)
}
}(j)
}
for i := 0; i < requests; i++ {
name := fmt.Sprintf("job-%d", i)
duration := time.Duration(rand.Intn(1000)) * time.Millisecond
fmt.Printf("adding: %s %s\n", name, duration)
jobs <- job{name, duration}
}
//time.Sleep(3 * time.Second)
close(jobs)
wg.Wait()
////////for the remaining number of requests.....
fmt.Println("65 left")
size = originalRequest - requests
fmt.Println(size)
wg = &sync.WaitGroup{}
jobs := make(chan job)
wg.Add(size)
for j := 1; j <= size; j++ {
go func(j int) {
defer wg.Done()
for c := range jobs {
doWork(j, c)
}
}(j)
////////////////////////
ticker := time.NewTicker(1 * time.Second)
go func() {
currentRequests++ //////////////////////////////
fmt.Println(currentRequests)
for _ = range ticker.C {
if size == 5 || count == 10 {
ticker.Stop()
}
// currentRequests = 17 ///
extra := currentRequests - originalRequest
if extra == 0 {
count++
}
for size < 5 && extra > 0 {
size++
extra--
originalRequest++
go func(j int) {
defer wg.Done()
for c := range jobs {
doWork(j, c)
}
}(size)
}
}
}()
}
// originalRequest = originalRequest + count
fmt.Println(currentRequests) ///
for i := requests; i < currentRequests; i++ {
name := fmt.Sprintf("job-%d", i)
duration := time.Duration(rand.Intn(1000)) * time.Millisecond
fmt.Printf("adding: %s %s\n", name, duration)
jobs <- job{name, duration}
}
time.Sleep(15 * time.Second)
close(jobs)
}
}
// wg.Wait()
}