-
Notifications
You must be signed in to change notification settings - Fork 1
/
if-go.go
122 lines (108 loc) · 3.8 KB
/
if-go.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"time"
)
// GoCreator manages the life cycle of one or more goroutines
type GoCreator interface {
// Add indicates a new goroutine about to be launched.
// conduit indicates how errors will be propagated from
// the goroutine.
// exitAction describes what actions the GoCreator object
// will take upon goroutine exit
Add(conduit ErrorConduit, exitAction ExitAction) (goer Goer)
// Warnings provides an error channel shared by goroutines that
// do not have an individual channel
Warnings() (ch <-chan GoError)
// Wait waits for all goroutines managed by this GoCreator
// to exit
Wait()
// WaitPeriod waits for all goroutines managed by this GoCreator
// to exit, periodically printing a description of the goroutines
// that have yet to exit
WaitPeriod(duration ...time.Duration)
IsExit() (isExit bool)
}
// GoError is an error or a thread exit associated with a goroutine
// Goer returns the Goer object handling the goroutine that sent the error
type GoError interface {
error // Error() string
// GetError retrieves the original error value
GetError() (err error)
// Source describes the source and significance of the error
Source() (source GoErrorSource)
// Goer provides the Goer object associated with the goroutine
// causing the error
Goer() (goer Goer)
}
const (
// GeNonFatal indictaes a non-fatal error ocurring during processing
GeNonFatal = iota + 1
// GePreDoneExit indicates an exit value of a subordinate goroutine,
// other than the main goroutine associated with the GoCreator object
// err may be nil
GePreDoneExit
// GeExit indicates exit of the main goroutine
// err may be nil
GeExit
// GeInternal indictaes an error occuring inside the Goer object
GeInternal
)
type GoErrorSource uint8
type GoIndex int
const (
// EcSharedChan emits error on a shared error channel of the GoCreator object
EcSharedChan ErrorConduit = iota + 1
// EcErrChan emits error on an individual channel of the Goer object
EcErrChan
// TODO 220418 ECErrorStore stores error in a perrors.ErrorStore of the GoCreator object
//ECErrorStore
)
type ErrorConduit uint8
const (
// ExCancelOnExit cancels the GoCreator context ie. all actions on behalf of the
// GoCreator if the goroutine exits
ExCancelOnExit ExitAction = iota + 1
// ExIgnoreExit takes no action on goruotine exit
ExIgnoreExit
// ExCancelOnFailure cancels the GoCreator context ie. all actions on behalf of the
// GoCreator if the goroutine exits with error
ExCancelOnFailure
)
type ExitAction uint8
// Goer is the managing object for a goroutine
type Goer interface {
// Go gets the Go object, that is handed to its goroutine on launch.
Go() (g0 Go)
// Chan is a channel that will close on thread exit.
// Chan will emit errors as they occur if the thread was launched with ECErrChan
// Chan will emit an Exit result if the thread was launched with ECErrChan
Chan() (ch <-chan error)
// Wait allows to wait for this exact goroutine
Wait()
}
// parl.Go is a value provided to a goroutine allowing it
// to provide that it has finished and its result.
type Go interface {
// Register performs no function but allows the Go object to collect
// information on the new thread
Register()
// Add allows for a goroutine to have the caller wait for
// additional internal goroutines.
Add(delta int)
// AddError allows a goroutine to send non-fatal errors
AddError(err error)
// Done indicates that a goroutine has finished.
// err nil typically means successful exit.
Done(err error)
// Context will cancel when work done on behalf of this context
// should be canceled
Context() (ctx context.Context)
}
type GoCreatorFactory interface {
NewGoCreator(ctx context.Context) (goCreator GoCreator)
}