-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (60 loc) · 1.39 KB
/
main.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
68
69
70
71
72
73
74
75
76
package scale
import (
"fmt"
"log"
"net/http"
"sync"
"time"
)
var workerCount = 5
type Job struct {
ID int
}
func Main() {
//Creating a buffer channel to control the number of concurrent workers
jobQueue := make(chan Job, 10)
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go worker(i, jobQueue, &wg)
}
http.HandleFunc("/process", func(w http.ResponseWriter, r *http.Request) {
job := Job{ID: time.Now().Nanosecond()}
go func () {
jobQueue <- job
}()
fmt.Fprint(w, "Jo queued\n")
})
go func() {
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Server error: ", err)
}
}()
fmt.Println("Server is running on http://localhost:8080")
// Simulate client sending a lot of requests
for i := 0; i< 10; i++ {
go sendRequest()
}
wg.Wait()
close(jobQueue)
}
func worker(id int, jobQueue <- chan Job, wg *sync.WaitGroup) {
defer wg.Done()
for job := range jobQueue {
// Simulate a compute-heavy operation by sleeping for a duration
time.Sleep(2 * time.Second)
// Log the completion of the job
log.Printf("Worker %d completed job %d\n", id, job.ID)
}
}
func sendRequest() {
// Simulate sending a client request to the server
resp, err := http.Get("http://localhost:8080/process")
if err != nil {
log.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
log.Println("Response:", resp.Status)
}