msg-batcher 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/msg-batcher.rb +32 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c39c802008e79837870af5f88a607a52bc0d1fe47a634012b2fb9d683560d726
|
4
|
+
data.tar.gz: c2ca34fdb97390eb44953dcd2fd3fbefdad4c15f380ca52384ba1d7e1ff0e113
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6398ddc1c414860880e4ed71e885856c657a5de8bc04e867fae8aa0c636b9ae9cdb8fe0d3228cb1eae086671ba9f6d82ff908c09b5b0215753d07792e05a4995
|
7
|
+
data.tar.gz: 2c301ea4cb70350c1a5a048612d7959dbda580897b99567d6b61a2ac3ee221271883040f70a4bbfb07c9916fa83407b2776d60b059fe251534217df01a73ec0c
|
data/lib/msg-batcher.rb
CHANGED
@@ -36,17 +36,25 @@ class MsgBatcher
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def kill(blocking: true)
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
# no #push will interfere
|
40
|
+
@m.synchronize do
|
41
|
+
# want to make sure that timer thread is in a waiting position. Hence, acquiring @m2
|
42
|
+
@m2.synchronize do
|
43
|
+
@closed = true
|
44
|
+
# releasing timer thread
|
45
|
+
@timer_start_cv.signal
|
46
|
+
# This can happen, however, that timer thread will wait timeout on @timer_release_cv.
|
47
|
+
# If it was waiting on @timer_start_cv. Because timer thread won't reach @timer_release_cv wait poisition
|
48
|
+
@timer_release_cv.signal
|
49
|
+
end
|
50
|
+
@timer_thread.join if blocking
|
51
|
+
end
|
44
52
|
end
|
45
53
|
|
46
54
|
# Thread-safe
|
47
55
|
# @raise [Error] when invoked when batcher has been closed
|
48
56
|
def push(entry)
|
49
|
-
raise Error,
|
57
|
+
raise Error, 'Batcher is closed - cannot push' if @closed
|
50
58
|
|
51
59
|
@m.lock
|
52
60
|
@m2.lock
|
@@ -81,7 +89,7 @@ class MsgBatcher
|
|
81
89
|
@storage = []
|
82
90
|
|
83
91
|
@already_released = true
|
84
|
-
dputs
|
92
|
+
dputs 'kill timer'
|
85
93
|
@timer_release_cv.signal # informing timer that release is happening
|
86
94
|
|
87
95
|
# Now interesting happens
|
@@ -94,12 +102,20 @@ class MsgBatcher
|
|
94
102
|
# So now we can be sure that timer is at the beginning, ready to wait for @timer_start_cv signal.
|
95
103
|
|
96
104
|
if from_push
|
97
|
-
dputs
|
105
|
+
dputs '--------before wait m2'
|
98
106
|
@timer_full_cycle_cv.wait @m2
|
99
107
|
@m2.unlock
|
100
|
-
dputs
|
108
|
+
dputs '-----22222'
|
101
109
|
@m.unlock
|
102
|
-
dputs
|
110
|
+
dputs '---- unlock all'
|
111
|
+
end
|
112
|
+
|
113
|
+
if temp.empty?
|
114
|
+
# The situation when the batch is empty should not happen.
|
115
|
+
# However, when the batcher is killed and the timer thread is forced to finish this can happen
|
116
|
+
# In any case, just ignoring this empty batch and do not call callback on it
|
117
|
+
warn 'MsgBatcher: empty batch. This should not have happened' unless @closed
|
118
|
+
return
|
103
119
|
end
|
104
120
|
|
105
121
|
begin
|
@@ -119,34 +135,28 @@ class MsgBatcher
|
|
119
135
|
@already_released = false
|
120
136
|
# informing release that timer is at the beginning
|
121
137
|
@timer_full_cycle_cv.signal
|
122
|
-
dputs
|
123
|
-
|
124
|
-
|
138
|
+
dputs 'sdlkfjsd'
|
125
139
|
|
126
140
|
# Position: TT1
|
127
141
|
# Wait for invocation from push
|
128
142
|
# Each release invocation finishes when timer thread are below (waiting for @timer_start_cv)
|
129
|
-
|
130
|
-
dputs "TT1"
|
143
|
+
dputs 'TT1'
|
131
144
|
@timer_start_cv.wait @m2
|
132
|
-
break if @closed
|
133
145
|
|
134
|
-
dputs
|
146
|
+
dputs 'TT1 after wait'
|
135
147
|
@timer_started_cv.signal
|
136
148
|
|
137
|
-
dputs
|
149
|
+
dputs 'TT2'
|
138
150
|
# then wait either time to elapse or signal that data has been released
|
139
151
|
@timer_release_cv.wait @m2, @max_time_msecs / 1000.0
|
140
|
-
break if @closed
|
141
|
-
|
142
152
|
dputs "timer end #{@m2.owned?}"
|
143
|
-
|
144
153
|
# @m2 is locked here!
|
145
|
-
|
146
154
|
unless @already_released
|
147
155
|
dputs "timer's release"
|
148
156
|
release(false)
|
149
157
|
end
|
158
|
+
|
159
|
+
break if @closed
|
150
160
|
end
|
151
161
|
end
|
152
162
|
# wait for timer to be in waiting state
|