-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.go
More file actions
270 lines (218 loc) · 5.11 KB
/
common.go
File metadata and controls
270 lines (218 loc) · 5.11 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package octopus
import (
"errors"
"time"
)
const(
JOBUNSTART uint32 = 0
JOBDOING uint32 = 1
JOBDONE uint32 = 2
JOBCRASH uint32 = 3
)
const(
poolOpen uint32 = 4
poolClose uint32 = 5
)
var(
ErrPoolShutdown = errors.New("the pool is closed")
ErrJobTimedOut = errors.New("job request timed out")
ErrRunnableNoResult = errors.New("runnable job has not a result value")
ErrResultChannelClose = errors.New("result channel close")
ErrInvalidArguments = errors.New("Invalid Arguments")
ErrKeepAliveTimeArguments = errors.New("KeepAliveTime must be greater than 1 second")
)
type Runnable func()
type Callable func() (interface{})
type LogFunc func(string)
type DataProcessFunc func(interface{}) interface{}
// a worker represents a goroutine
type worker interface {
start()
getStopChannel() chan bool
getJobChannel() chan Future
}
// a Future represents the result of an asynchronous computation.
type Future interface {
// Wait for the job done, and then retrieves its result.
Get() (interface{},error)
// Wait for the result by setting timeout
GetTimed(time.Duration) (interface{},error)
// Get the execution status of the job, it may be JOBUNSTART, JOBDOING,JOBDONE or JOBCRASH
Status() uint32
// Return if the job is done
IsDone() bool
execute()
setStatus(uint32)
getData() interface{}
getResultChannel() chan interface{}
}
type dataFuture struct {
pool *DataProcessPool
data interface{}
resultChannel chan interface{}
status uint32
}
func newDataFuture(dataprocesspool *DataProcessPool, jobData interface{}) Future {
return &dataFuture {
pool : dataprocesspool ,
data : jobData ,
resultChannel : make(chan interface{}, 1) ,
status : JOBUNSTART,
}
}
func (f *dataFuture) setStatus(s uint32) {
f.status = s
}
func(f *dataFuture) getResultChannel() chan interface{} {
return f.resultChannel
}
func (f *dataFuture) getData() interface{} {
return f.data
}
func (f *dataFuture) execute() {
}
func (f *dataFuture) Get() (result interface{}, err error) {
var ok bool
result,ok = <- f.resultChannel
if !ok {
err = ErrResultChannelClose
} else {
close(f.resultChannel)
}
return
}
func (f *dataFuture) GetTimed(timeout time.Duration) (result interface{}, err error) {
var ok bool
select {
case result,ok = <- f.resultChannel :
if !ok {
err = ErrResultChannelClose
} else {
close(f.resultChannel)
}
return
case <- time.After(timeout*time.Millisecond) :
err = ErrJobTimedOut
return
}
}
func (f *dataFuture) Status() uint32 {
return f.status
}
func (f *dataFuture) IsDone() bool {
return f.status == JOBDONE
}
type callableFuture struct {
pool *WorkPool
job Callable
resultChannel chan interface{}
status uint32
}
func newCallableFuture(workpool *WorkPool, fn Callable) Future {
return &callableFuture{
pool: workpool,
job: fn,
resultChannel: make(chan interface{}, 1),
status: JOBUNSTART,
}
}
func (f *callableFuture) setStatus(s uint32) {
f.status = s
}
func(f *callableFuture) getResultChannel() chan interface{} {
return f.resultChannel
}
func (f *callableFuture) getData() interface{} {
return nil
}
func (f *callableFuture) execute() {
defer func() {
if err := recover(); err != nil {
f.status = JOBCRASH
f.pool.log("panic: " + err.(string))
}
}()
f.status = JOBDOING
result := f.job()
f.resultChannel <- result
f.status = JOBDONE
}
func (f *callableFuture) Get() (result interface{}, err error) {
var ok bool
result,ok = <- f.resultChannel
if !ok {
err = ErrResultChannelClose
} else {
close(f.resultChannel)
}
return
}
func (f *callableFuture) GetTimed(timeout time.Duration) (result interface{}, err error) {
var ok bool
select {
case result,ok = <- f.resultChannel :
if !ok {
err = ErrResultChannelClose
} else {
close(f.resultChannel)
}
return
case <- time.After(timeout*time.Millisecond) :
err = ErrJobTimedOut
return
}
}
func (f *callableFuture) Status() uint32 {
return f.status
}
func (f *callableFuture) IsDone() bool {
return f.status == JOBDONE
}
type runnableFuture struct {
pool *WorkPool
job Runnable
resultChannel chan interface{}
status uint32
}
func newRunnableFuture(workpool *WorkPool, fn Runnable) Future {
return &runnableFuture{
pool: workpool,
job: fn,
resultChannel: make(chan interface{}, 1),
status: JOBUNSTART,
}
}
func (f *runnableFuture) setStatus(s uint32) {
f.status = s
}
func (f *runnableFuture) getResultChannel() chan interface{} {
return f.resultChannel
}
func (f *runnableFuture) getData() interface{} {
return nil
}
func (f *runnableFuture) execute() {
defer func() {
if err := recover(); err != nil {
f.status = JOBCRASH
f.pool.log("panic: " + err.(string))
}
}()
f.status = JOBDOING
f.job()
f.status = JOBDONE
}
func (f *runnableFuture) Get() (result interface{}, err error) {
err = ErrRunnableNoResult
return
}
func (f *runnableFuture) GetTimed(timeout time.Duration) (result interface{}, err error) {
err = ErrRunnableNoResult
return
}
func (f *runnableFuture) Status() uint32 {
return f.status
}
func (f *runnableFuture) IsDone() bool {
return f.status == JOBDONE
}