msg-batcher 0.0.1 → 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 +34 -18
- metadata +2 -2
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
@@ -35,15 +35,26 @@ class MsgBatcher
|
|
35
35
|
start_timer
|
36
36
|
end
|
37
37
|
|
38
|
-
def kill
|
39
|
-
|
40
|
-
@
|
38
|
+
def kill(blocking: true)
|
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
|
41
52
|
end
|
42
53
|
|
43
54
|
# Thread-safe
|
44
55
|
# @raise [Error] when invoked when batcher has been closed
|
45
56
|
def push(entry)
|
46
|
-
raise Error,
|
57
|
+
raise Error, 'Batcher is closed - cannot push' if @closed
|
47
58
|
|
48
59
|
@m.lock
|
49
60
|
@m2.lock
|
@@ -78,7 +89,7 @@ class MsgBatcher
|
|
78
89
|
@storage = []
|
79
90
|
|
80
91
|
@already_released = true
|
81
|
-
dputs
|
92
|
+
dputs 'kill timer'
|
82
93
|
@timer_release_cv.signal # informing timer that release is happening
|
83
94
|
|
84
95
|
# Now interesting happens
|
@@ -91,12 +102,20 @@ class MsgBatcher
|
|
91
102
|
# So now we can be sure that timer is at the beginning, ready to wait for @timer_start_cv signal.
|
92
103
|
|
93
104
|
if from_push
|
94
|
-
dputs
|
105
|
+
dputs '--------before wait m2'
|
95
106
|
@timer_full_cycle_cv.wait @m2
|
96
107
|
@m2.unlock
|
97
|
-
dputs
|
108
|
+
dputs '-----22222'
|
98
109
|
@m.unlock
|
99
|
-
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
|
100
119
|
end
|
101
120
|
|
102
121
|
begin
|
@@ -116,31 +135,28 @@ class MsgBatcher
|
|
116
135
|
@already_released = false
|
117
136
|
# informing release that timer is at the beginning
|
118
137
|
@timer_full_cycle_cv.signal
|
119
|
-
dputs
|
120
|
-
|
121
|
-
|
138
|
+
dputs 'sdlkfjsd'
|
122
139
|
|
123
140
|
# Position: TT1
|
124
141
|
# Wait for invocation from push
|
125
142
|
# Each release invocation finishes when timer thread are below (waiting for @timer_start_cv)
|
126
|
-
|
127
|
-
dputs "TT1"
|
143
|
+
dputs 'TT1'
|
128
144
|
@timer_start_cv.wait @m2
|
129
|
-
|
145
|
+
|
146
|
+
dputs 'TT1 after wait'
|
130
147
|
@timer_started_cv.signal
|
131
148
|
|
132
|
-
dputs
|
149
|
+
dputs 'TT2'
|
133
150
|
# then wait either time to elapse or signal that data has been released
|
134
151
|
@timer_release_cv.wait @m2, @max_time_msecs / 1000.0
|
135
|
-
|
136
152
|
dputs "timer end #{@m2.owned?}"
|
137
|
-
|
138
153
|
# @m2 is locked here!
|
139
|
-
|
140
154
|
unless @already_released
|
141
155
|
dputs "timer's release"
|
142
156
|
release(false)
|
143
157
|
end
|
158
|
+
|
159
|
+
break if @closed
|
144
160
|
end
|
145
161
|
end
|
146
162
|
# wait for timer to be in waiting state
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msg-batcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ertygiq
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A Ruby library that facilitates thread-safe batch processing of messages.
|
14
14
|
In certain situations, processing multiple messages in batch is more efficient than
|