-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
36 lines (31 loc) · 975 Bytes
/
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
package main
import (
"errors"
"flag"
"log"
"math/rand"
"time"
"github.com/bgadrian/go-worker-thread-pool/server"
)
func main() {
MaxWorker := flag.Uint("MAX_WORKERS", 5, "max nr of workers")
MaxQueue := flag.Uint("MAX_QUEUE", 10, "max nr of jobs in queue")
flag.Parse()
//every payload (request to /job) from the client is sent here
processator := func(w *server.Worker, j server.Job) error {
text := j.Payload.Magic
if text == "error" {
return errors.New("error")
}
log.Printf("processing '%v' by %v\n", text, w.ID)
//simulating a very long time to process
//so we can understand the process
time.Sleep(time.Duration(rand.Intn(5)+3) * time.Second)
log.Printf("done processing '%v' by %v\n", text, w.ID)
return nil
}
log.Println("open http://localhost:8080 in your browser & keep this process open.")
dispatcher := server.NewDispatcher(int(*MaxWorker), int(*MaxQueue), processator)
dispatcher.Run()
server.StartServer(dispatcher)
}