-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ignavan39/v2
add worker pool
- Loading branch information
Showing
5 changed files
with
101 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package wpqueue | ||
|
||
import "github.com/streadway/amqp" | ||
|
||
type QueueWorkerPool struct { | ||
workersCount int | ||
queue string | ||
deliveries <-chan amqp.Delivery | ||
results chan ResultDelivery | ||
} | ||
|
||
type ResultDelivery struct { | ||
WorkerId int | ||
Delivery amqp.Delivery | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package wpqueue | ||
|
||
import ( | ||
"fmt" | ||
"github.com/streadway/amqp" | ||
"sync" | ||
) | ||
|
||
func NewWorkerPool(count int, queue string, deliveries <-chan amqp.Delivery) QueueWorkerPool { | ||
return QueueWorkerPool{ | ||
workersCount: count, | ||
queue: queue, | ||
deliveries: deliveries, | ||
results: make(chan ResultDelivery), | ||
} | ||
} | ||
|
||
func (wp QueueWorkerPool) RunWorkerPool() { | ||
var wg sync.WaitGroup | ||
for i := 0; i < wp.workersCount; i++ { | ||
wg.Add(1) | ||
go wp.workerProcessing(i, &wg) | ||
} | ||
wg.Wait() | ||
close(wp.results) | ||
} | ||
|
||
func (wp QueueWorkerPool) Results() <-chan ResultDelivery { | ||
return wp.results | ||
} | ||
|
||
func (wp QueueWorkerPool) workerProcessing(numWorker int, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case delivery, ok := <-wp.deliveries: | ||
if !ok { | ||
fmt.Printf("[Rq][%s][%d][Run][channel closed]\n", wp.queue, numWorker) | ||
return | ||
} | ||
wp.results <- ResultDelivery{ | ||
WorkerId: numWorker, | ||
Delivery: delivery, | ||
} | ||
} | ||
} | ||
} |