-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasyc_call.go
63 lines (56 loc) · 1.5 KB
/
asyc_call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package concurrency
import (
"context"
"errors"
"time"
)
var ErrTimeout = errors.New("time out")
var ErrCancelled = errors.New("task has been cancelled")
// TaskResult is the result of the asynchronous task
type TaskResult struct {
Result interface{}
Err error
}
// ResultStub is returned immediately after calling AsynExecutor
// It is the stub to get the result.
type ResultStub struct {
tResult *TaskResult
retCh chan TaskResult
timeoutMs int64
ctx context.Context
}
// GetResult is to get the asynchronous task's result
// it will be blocked util the task is completed/failed.
func (rs *ResultStub) GetResult() TaskResult {
if rs.tResult != nil {
return *rs.tResult
}
var tResult TaskResult
timer := time.NewTimer(time.Millisecond * time.Duration(rs.timeoutMs))
select {
case ret := <-rs.retCh:
rs.tResult = &ret
case <-timer.C:
tResult.Err = ErrTimeout
rs.tResult = &tResult
case <-rs.ctx.Done():
tResult.Err = ErrCancelled
rs.tResult = &tResult
}
return *rs.tResult
}
type Task func(ctx context.Context) TaskResult
// AsynExecutor is to run your task in asynchronous way,
// task is the task need to be executed
// timeoutMs is the timeout setting for the task execution
// ResultStub ResultStub.GetResult() is to get the result of the task.
func AsynExecutor(ctx context.Context, task Task,
timeoutMs int64) ResultStub {
retCh := make(chan TaskResult, 1)
go func() {
retCh <- task(ctx)
}()
return ResultStub{retCh: retCh,
timeoutMs: timeoutMs,
ctx: ctx}
}