-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
52 lines (42 loc) · 737 Bytes
/
task.go
File metadata and controls
52 lines (42 loc) · 737 Bytes
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
package batch
import (
"context"
"fmt"
"sync"
)
type DuplicateUniqId struct {
msg any
}
func (e DuplicateUniqId) Error() string {
return fmt.Sprintf("duplicate uniqID: %s", e.msg.(string))
}
type Task[T any] struct {
ctx context.Context
Id any
Value T
mu sync.Mutex
cond *sync.Cond
err error
}
func (task *Task[T]) Key() string {
return fmt.Sprintf("%v", task.Id)
}
func (task *Task[T]) Wait() error {
task.mu.Lock()
task.cond.Wait()
task.mu.Unlock()
if task.err != nil {
return task.err
}
if e := context.Cause(task.ctx); e != nil {
task.err = e
return e
}
return nil
}
func (task *Task[T]) signal(err error) {
task.mu.Lock()
defer task.mu.Unlock()
task.err = err
task.cond.Signal()
}