@@ -17,23 +17,26 @@ import (
17
17
// ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue
18
18
type ByteFIFOQueueConfiguration struct {
19
19
WorkerPoolConfiguration
20
- Workers int
21
- Name string
20
+ Workers int
21
+ Name string
22
+ WaitOnEmpty bool
22
23
}
23
24
24
25
var _ Queue = & ByteFIFOQueue {}
25
26
26
27
// ByteFIFOQueue is a Queue formed from a ByteFIFO and WorkerPool
27
28
type ByteFIFOQueue struct {
28
29
* WorkerPool
29
- byteFIFO ByteFIFO
30
- typ Type
31
- closed chan struct {}
32
- terminated chan struct {}
33
- exemplar interface {}
34
- workers int
35
- name string
36
- lock sync.Mutex
30
+ byteFIFO ByteFIFO
31
+ typ Type
32
+ closed chan struct {}
33
+ terminated chan struct {}
34
+ exemplar interface {}
35
+ workers int
36
+ name string
37
+ lock sync.Mutex
38
+ waitOnEmpty bool
39
+ pushed chan struct {}
37
40
}
38
41
39
42
// NewByteFIFOQueue creates a new ByteFIFOQueue
@@ -45,14 +48,16 @@ func NewByteFIFOQueue(typ Type, byteFIFO ByteFIFO, handle HandlerFunc, cfg, exem
45
48
config := configInterface .(ByteFIFOQueueConfiguration )
46
49
47
50
return & ByteFIFOQueue {
48
- WorkerPool : NewWorkerPool (handle , config .WorkerPoolConfiguration ),
49
- byteFIFO : byteFIFO ,
50
- typ : typ ,
51
- closed : make (chan struct {}),
52
- terminated : make (chan struct {}),
53
- exemplar : exemplar ,
54
- workers : config .Workers ,
55
- name : config .Name ,
51
+ WorkerPool : NewWorkerPool (handle , config .WorkerPoolConfiguration ),
52
+ byteFIFO : byteFIFO ,
53
+ typ : typ ,
54
+ closed : make (chan struct {}),
55
+ terminated : make (chan struct {}),
56
+ exemplar : exemplar ,
57
+ workers : config .Workers ,
58
+ name : config .Name ,
59
+ waitOnEmpty : config .WaitOnEmpty ,
60
+ pushed : make (chan struct {}, 1 ),
56
61
}, nil
57
62
}
58
63
@@ -76,6 +81,14 @@ func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error {
76
81
if err != nil {
77
82
return err
78
83
}
84
+ if q .waitOnEmpty {
85
+ defer func () {
86
+ select {
87
+ case q .pushed <- struct {}{}:
88
+ default :
89
+ }
90
+ }()
91
+ }
79
92
return q .byteFIFO .PushFunc (bs , fn )
80
93
}
81
94
@@ -131,6 +144,16 @@ func (q *ByteFIFOQueue) readToChan() {
131
144
}
132
145
133
146
if len (bs ) == 0 {
147
+ if q .waitOnEmpty && q .byteFIFO .Len () == 0 {
148
+ q .lock .Unlock ()
149
+ log .Trace ("%s: %s Waiting on Empty" , q .typ , q .name )
150
+ select {
151
+ case <- q .pushed :
152
+ continue
153
+ case <- q .closed :
154
+ continue
155
+ }
156
+ }
134
157
q .lock .Unlock ()
135
158
time .Sleep (time .Millisecond * 100 )
136
159
continue
0 commit comments