1234567891011121314151617181920212223242526272829303132333435 |
- package workpool
- import (
- "fmt"
- "time"
- )
- // Task interface
- type Task interface {
- Run() *[]byte
- }
- // FutureTask out must be blocking chan (size=0)
- type FutureTask struct {
- T Task
- out chan *[]byte
- }
- // NewFutureTask .
- func NewFutureTask(t Task) *FutureTask {
- return &FutureTask{
- T: t,
- out: make(chan *[]byte, 1),
- }
- }
- // Wait for task return until timeout
- func (ft *FutureTask) Wait(timeout time.Duration) (res *[]byte, err error) {
- select {
- case res = <-ft.out:
- case <-time.After(timeout):
- err = fmt.Errorf("task(%+v) timeout", ft)
- }
- return
- }
|