|
| 1 | +# golang-worker-pool-management-with-tunny |
| 2 | + |
| 3 | +This repository is demo how to use tunny library to implementing worker-pool management |
| 4 | + |
| 5 | +## implementation |
| 6 | + |
| 7 | +1. simple worker pool with create function |
| 8 | + |
| 9 | +```golang |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "errors" |
| 14 | + "fmt" |
| 15 | + "runtime" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/Jeffail/tunny" |
| 19 | +) |
| 20 | + |
| 21 | +func SendEmail(email string, subject string, body string) { |
| 22 | + fmt.Printf("Sending email to %s\n", email) |
| 23 | + fmt.Printf("Subject %s\n Body: %s\n", subject, body) |
| 24 | + // Simulate sending email |
| 25 | + time.Sleep(2 * time.Second) |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + numCPUs := runtime.NumCPU() |
| 30 | + fmt.Printf("Number of CPUs: %d\n\n", numCPUs) |
| 31 | + |
| 32 | + pool := tunny.NewFunc(numCPUs, func(payload any) any { |
| 33 | + m, ok := payload.(map[string]any) |
| 34 | + if !ok { |
| 35 | + return errors.New("unable to extract map") |
| 36 | + } |
| 37 | + |
| 38 | + // Extract the fields |
| 39 | + email, ok := m["email"].(string) |
| 40 | + if !ok { |
| 41 | + return errors.New("email field is missing or not a string") |
| 42 | + } |
| 43 | + |
| 44 | + subject, ok := m["subject"].(string) |
| 45 | + if !ok { |
| 46 | + return errors.New("subject field is missing or not a string") |
| 47 | + } |
| 48 | + |
| 49 | + body, ok := m["body"].(string) |
| 50 | + if !ok { |
| 51 | + return errors.New("body field is missing or not a string") |
| 52 | + } |
| 53 | + |
| 54 | + SendEmail(email, subject, body) |
| 55 | + return nil |
| 56 | + }) |
| 57 | + defer pool.Close() |
| 58 | + |
| 59 | + for i := 0; i < 100; i++ { |
| 60 | + var data any = map[string]any{ |
| 61 | + "email": fmt.Sprintf("email%d@sample.io", i+1), |
| 62 | + "subject": "Welcome", |
| 63 | + "body": "Thank you for signing up", |
| 64 | + } |
| 65 | + go func() { |
| 66 | + result := pool.Process(data) |
| 67 | + if result == nil { |
| 68 | + fmt.Println("Mail sent!") |
| 69 | + } |
| 70 | + }() |
| 71 | + } |
| 72 | + |
| 73 | + for { |
| 74 | + qLen := pool.QueueLength() |
| 75 | + fmt.Printf("----------------- Queue Length: %d\n", qLen) |
| 76 | + if qLen == 0 { |
| 77 | + break |
| 78 | + } |
| 79 | + time.Sleep(1 * time.Second) |
| 80 | + } |
| 81 | + time.Sleep(3 * time.Second) |
| 82 | +} |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +2. manage state by create worker structure with each state hook |
| 87 | + |
| 88 | +```golang |
| 89 | +package main |
| 90 | + |
| 91 | +import ( |
| 92 | + "fmt" |
| 93 | + "runtime" |
| 94 | + "time" |
| 95 | + |
| 96 | + "github.com/Jeffail/tunny" |
| 97 | +) |
| 98 | + |
| 99 | +type myWorker struct { |
| 100 | + jobID int |
| 101 | + state string |
| 102 | +} |
| 103 | + |
| 104 | +func (w myWorker) Process(payload any) any { |
| 105 | + w.jobID, _ = payload.(int) |
| 106 | + w.state = "processing" |
| 107 | + fmt.Printf("Processing job %v, state: %s\n", payload, w.state) |
| 108 | + time.Sleep(2 * time.Second) |
| 109 | + return nil |
| 110 | +} |
| 111 | +func (w myWorker) BlockUntilReady() { |
| 112 | + w.state = "starting" |
| 113 | + fmt.Printf("State: %s\n", w.state) |
| 114 | + time.Sleep(10 * time.Millisecond) |
| 115 | +} |
| 116 | + |
| 117 | +func (w myWorker) Interrupt() { |
| 118 | + w.state = "interrputed" |
| 119 | + fmt.Printf("State: %s\n", w.state) |
| 120 | + time.Sleep(10 * time.Millisecond) |
| 121 | +} |
| 122 | + |
| 123 | +func (w myWorker) Terminate() { |
| 124 | + w.state = "terminated" |
| 125 | + fmt.Printf("State: %s\n", w.state) |
| 126 | +} |
| 127 | + |
| 128 | +func main() { |
| 129 | + numCPUs := runtime.NumCPU() |
| 130 | + pool := tunny.New(numCPUs, func() tunny.Worker { |
| 131 | + return myWorker{} |
| 132 | + }) |
| 133 | + defer pool.Close() |
| 134 | + |
| 135 | + for i := 0; i < 10; i++ { |
| 136 | + go func() { |
| 137 | + var data any = i |
| 138 | + result := pool.Process(data) |
| 139 | + if result == nil { |
| 140 | + fmt.Println("success!") |
| 141 | + } else { |
| 142 | + fmt.Println("failure!") |
| 143 | + } |
| 144 | + }() |
| 145 | + } |
| 146 | + for { |
| 147 | + qLen := pool.QueueLength() |
| 148 | + fmt.Printf("------------------- Queue Length: %d\n", qLen) |
| 149 | + if qLen == 0 { |
| 150 | + break |
| 151 | + } |
| 152 | + time.Sleep(1 * time.Second) |
| 153 | + } |
| 154 | + |
| 155 | + time.Sleep(5 * time.Second) |
| 156 | + fmt.Println("Done!") |
| 157 | +} |
| 158 | + |
| 159 | +``` |
0 commit comments