go-playground/pool là dự án mã nguồn mở trên GitHub, viết chủ yếu bằng Go, với 723 sao. :speedboat: a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Package pool implements a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation.
Features:
Dead simple to use and makes no assumptions about how you will use it.
Automatic recovery from consumer goroutines which returns an error to the results
Pool v2 advantages over Pool v1:
Up to 300% faster due to lower contention ( BenchmarkSmallRun used to take 3 seconds, now 1 second )
Cancels are much faster
Easier to use, no longer need to know the # of Work Units to be processed.
Pool can now be used as a long running/globally defined pool if desired ( v1 Pool was only good for one run )
Supports single units of work as well as batching
Pool can easily be reset after a Close() or Cancel() for reuse.
Multiple Batches can be run and even cancelled on the same Pool.
Supports individual Work Unit cancellation.
Pool v3 advantages over Pool v2:
Objects are not interfaces allowing for less breaking changes going forward.
Now there are 2 Pool types, both completely interchangeable, a limited worker pool and unlimited pool.
Simpler usage of Work Units, instead of <-work.Done now can do work.Wait()
Installation
Use go get.
go get gopkg.in/go-playground/pool.v3
Then import the pool package into your own code.
import "gopkg.in/go-playground/pool.v3"
Important Information READ THIS!
It is recommended that you cancel a pool or batch from the calling function and not inside of the Unit of Work, it will work fine, however because of the goroutine scheduler and context switching it may not cancel as soon as if called from outside.
When Batching DO NOT FORGET TO CALL batch.QueueComplete(), if you do the Batch WILL deadlock
It is your responsibility to call WorkUnit.IsCancelled() to check if it's cancelled after a blocking operation like waiting for a connection from a pool. (optional)
Usage and documentation
Please see http://godoc.org/gopkg.in/go-playground/pool.v3 for detailed usage docs.
Examples:
both Limited Pool and Unlimited Pool have the same signatures and are completely interchangeable.
Per Unit Work
package main
import (
"fmt"
"time"
"gopkg.in/go-playground/pool.v3"
)
func main() {
p := pool.NewLimited(10)
defer p.Close()
user := p.Queue(getUser(13))
other := p.Queue(getOtherInfo(13))
user.Wait()
if err := user.Error(); err != nil {
// handle error
}
// do stuff with user
username := user.Value().(string)
fmt.Println(username)
other.Wait()
if err := other.Error(); err != nil {
// handle error
}
// do stuff with other
otherInfo := other.Value().(string)
fmt.Println(otherInfo)
}
func getUser(id int) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return "Joeybloggs", nil
}
}
func getOtherInfo(id int) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return "Other Info", nil
}
}
Batch Work
package main
import (
"fmt"
"time"
"gopkg.in/go-playground/pool.v3"
)
func main() {
p := pool.NewLimited(10)
defer p.Close()
batch := p.Batch()
// for max speed Queue in another goroutine
// but it is not required, just can't start reading results
// until all items are Queued.
go func() {
for i := 0; i < 10; i++ {
batch.Queue(sendEmail("email content"))
}
// DO NOT FORGET THIS OR GOROUTINES WILL DEADLOCK
// if calling Cancel() it calles QueueComplete() internally
batch.QueueComplete()
}()
for email := range batch.Results() {
if err := email.Error(); err != nil {
// handle error
// maybe call batch.Cancel()
}
// use return value
fmt.Println(email.Value().(bool))
}
}
func sendEmail(email string) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return true, nil // everything ok, send nil, error if not
}
}
Benchmarks
Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go 1.6.2
run with 1, 2, 4,8 and 16 cpu to show it scales well...16 is double the # of logical cores on this machine.
NOTE: Cancellation times CAN vary depending how busy your system is and how the goroutine scheduler is but
worse case I've seen is 1s to cancel instead of 0ns
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/go-playground/pool là nguồn chính thức.
go-playground/pool có phải mã nguồn mở không?
Có — go-playground/pool phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/go-playground/pool.
go-playground/pool có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho go-playground/pool. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
go-playground/pool có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho go-playground/pool. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
go-playground/pool còn đang phát triển không?
Commit gần nhất trên go-playground/pool là 5.2 năm trước (theo timestamp GitHub). Repo có 64 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
go-playground/pool dùng license gì?
go-playground/pool phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về pool?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về pool.