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
|
# File 'lib/cosmo/utils/overrides.rb', line 53
def fetch(batch = 1, params = {})
raise ::NATS::JetStream::Error.new("nats: invalid batch size") if batch < 1
t = MonotonicTime.now
start_time = t timeout = params[:timeout] ||= 5
expires = (timeout * 1_000_000_000) - 100_000
next_req = { batch: batch }
msgs = []
case
when batch == 1
synchronize do
unless @pending_queue.empty?
msg = @pending_queue.pop
@pending_size -= msg.data.size
if JS.is_status_msg(msg)
case msg.["Status"]
when JS::Status::NoMsgs then nil
when JS::Status::RequestTimeout then nil else raise JS.from_msg(msg)
end
else
msgs << msg
end
end
end
next_req[:expires] = expires
if msgs.empty?
@nc.publish(@jsi.nms, JS.next_req_to_json(next_req), @subject)
synchronize { wait_for_msgs_cond.wait(timeout) }
unless @pending_queue.empty?
msg = @pending_queue.pop
@pending_size -= msg.data.size
msgs << msg
end
raise ::NATS::Timeout.new("nats: fetch timeout") if msgs.empty? && (MonotonicTime.since(t) > timeout)
if JS.is_status_msg(msgs.first)
msg = msgs.first
case msg.[JS::Header::Status]
when JS::Status::RequestTimeout then raise NATS::Timeout.new("nats: fetch request timeout")
else raise JS.from_msg(msgs.first)
end
end
end
when batch > 1
synchronize do
if batch <= @pending_queue.size
batch.times do
msg = @pending_queue.pop
@pending_size -= msg.data.size
if JS.is_status_msg(msg)
case msg.[JS::Header::Status]
when JS::Status::NoMsgs, JS::Status::RequestTimeout then next
else raise JS.from_msg(msg)
end
else
msgs << msg
end
end
return msgs
end
end
next_req[:no_wait] = true
@nc.publish(@jsi.nms, JS.next_req_to_json(next_req), @subject)
start_time = MonotonicTime.now
msg = nil
synchronize do
wait_for_msgs_cond.wait(timeout)
unless @pending_queue.empty?
msg = @pending_queue.pop
@pending_size -= msg.data.size
end
end
if !msg.nil? && JS.is_status_msg(msg)
case msg.[JS::Header::Status]
when JS::Status::NoMsgs
next_req[:expires] = expires
next_req.delete(:no_wait)
@nc.publish(@jsi.nms, JS.next_req_to_json(next_req), @subject)
when JS::Status::RequestTimeout
raise NATS::Timeout.new("nats: fetch request timeout")
else
raise JS.from_msg(msg)
end
else
msgs << msg unless msg.nil?
end
duration = MonotonicTime.since(start_time)
raise NATS::Timeout.new("nats: fetch timeout") if msgs.empty? && (duration > timeout)
needed = batch - msgs.count
while (needed > 0) && (MonotonicTime.since(start_time) < timeout)
duration = MonotonicTime.since(start_time)
synchronize do
if @pending_queue.empty?
deadline = timeout - duration
wait_for_msgs_cond.wait(deadline) if deadline > 0
duration = MonotonicTime.since(start_time)
if msgs.empty? && @pending_queue.empty? && (duration > timeout)
raise NATS::Timeout.new("nats: fetch timeout")
end
end
unless @pending_queue.empty?
msg = @pending_queue.pop
@pending_size -= msg.data.size
if JS.is_status_msg(msg)
case msg.[JS::Header::Status]
when JS::Status::NoMsgs, JS::Status::RequestTimeout
duration = MonotonicTime.since(start_time)
if duration > timeout
raise NATS::Timeout.new("nats: fetch timeout") if msgs.empty?
return msgs
end
else
raise JS.from_msg(msg)
end
else
msgs << msg
needed -= 1
end
end
end
end
end
raise ::NATS::Timeout.new("nats: fetch timeout") if msgs.empty? && (MonotonicTime.since(start_time) > timeout)
msgs
end
|