74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gopkg.in/go-playground/pool.v3"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
|
||
|
|
p := pool.New()
|
||
|
|
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
|
||
|
|
}
|
||
|
|
}
|