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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/msg-batcher.rb +34 -18
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac80b44e3f48248b2355ee73c5ed6346a492f9f5749ba670fbc18310171535cc
4
- data.tar.gz: 42537898648e2e953ebb3df19fec938db4cc8da487f5014569a4477a71c666ff
3
+ metadata.gz: c39c802008e79837870af5f88a607a52bc0d1fe47a634012b2fb9d683560d726
4
+ data.tar.gz: c2ca34fdb97390eb44953dcd2fd3fbefdad4c15f380ca52384ba1d7e1ff0e113
5
5
  SHA512:
6
- metadata.gz: 8a478b76db6a65bb7a428e01d714a76486f7d1932328b0157da799f83fb05767cab488108ae7dd16b531110e99ea3b6d5dcae15e02bd667e6e9b52e27caea955
7
- data.tar.gz: fade326da9e58081a4a8d89d8f05bd368995556c97a12a805f38802974cbe481a3c54a597c1cf23764bd476463269f5b9bd41c62ea6b0d5c58829763704494bc
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
- @closed = true
40
- @timer_thread.exit
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, "Batcher is closed - cannot push" if @closed
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 "kill timer"
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 "--------before wait m2"
105
+ dputs '--------before wait m2'
95
106
  @timer_full_cycle_cv.wait @m2
96
107
  @m2.unlock
97
- dputs "-----22222"
108
+ dputs '-----22222'
98
109
  @m.unlock
99
- dputs "---- unlock all"
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 "sdlkfjsd"
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
- dputs "TT1 after wait"
145
+
146
+ dputs 'TT1 after wait'
130
147
  @timer_started_cv.signal
131
148
 
132
- dputs "TT2"
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.1
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-10 00:00:00.000000000 Z
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