-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask.go
More file actions
56 lines (46 loc) · 1.14 KB
/
task.go
File metadata and controls
56 lines (46 loc) · 1.14 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package workerpool
import (
"context"
"fmt"
"time"
)
// Task is a unit of work.
type Task interface {
// String returns the task identifier.
fmt.Stringer
// Err returns the error resulting from processing the
// unit of work.
Err() error
}
// Result is a completed Task that also reports its execution duration.
// It is passed to the callback registered with [WithResultCallback].
type Result interface {
Task
// Duration returns the time taken to execute the task.
Duration() time.Duration
}
type task struct {
run func(context.Context) error
id string
}
type taskResult struct {
err error
id string
duration time.Duration
}
// Ensure that taskResult implements the Result interface.
var _ Result = &taskResult{}
// String implements [fmt.Stringer] for taskResult.
func (t *taskResult) String() string {
return t.id
}
// Err returns the error resulting from processing the taskResult.
func (t *taskResult) Err() error {
return t.err
}
// Duration returns the time taken to execute the task.
func (t *taskResult) Duration() time.Duration {
return t.duration
}