-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.cr
371 lines (353 loc) · 11.4 KB
/
channel.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
require "./concur"
abstract class ::Channel(T)
# Returns an enumerable of values received from `self` containing at most *max_items* elements.
#
# If `self` is closed in the process, then the returned enumerable might include fewer elements.
def take(max_items : Int32)
([] of T).tap { |items|
(1..max_items).each do
items << self.receive
rescue Channel::ClosedError
break
end
}
end
# Returns a channel that receives values from both *self* and *other*, as soon as they are available.
#
# The returned channel is closed as soon as both the input channels are closed and empty - but will
# continue to operate while at least one of them is open.
#
# NOTE: If both channels have values ready to be received, then one will be selected at random.
def merge(other : Channel(J), name = nil, buffer_size = 0) : Channel(T | J) forall J
channels = [self, other]
Channel(T | J).new(buffer_size).tap { |stream|
spawn(name: name) do
loop do
stream.send Channel.receive_first(channels.shuffle) # shuffle to increase fairness
rescue Channel::ClosedError
channels.reject!(&.closed?)
break if channels.empty?
end
stream.close
end
}
end
# Returns a channel that receives at most *items_per_sec* items per second from the
# caller - or *max_burst* items, if no elements were received in a while.
#
# The returned channel is closed once `self` is closed and empty and a new value
# can be received, based on the rate limiting parameters.
#
# Refer to the documentation of [RateLimiter](https://github.com/lbarasti/rate_limiter)
# for more details.
def rate_limit(items_per_sec : Float64, max_burst : Int32 = 1, name = nil, buffer_size = 0)
rl = RateLimiter.new(rate: items_per_sec, max_burst: max_burst)
Channel(T).new(buffer_size).tap { |stream|
spawn(name: name) do
loop do
rl.get
stream.send self.receive
end
rescue Channel::ClosedError
stream.close
end
}
end
# Returns a channel that receives values from `self` transformed via *block*.
#
# A *workers* parameter can be supplied to make the computation of the block concurrent.
# Note that, for *workers* > 1, no order guarantees are made.
#
# The returned channel will close once `self` is closed and empty, and all
# the outstanding runs of *block* are completed.
#
# Any exception raised while evaluating *block* will be passed
# to the optional callback *on_error*, together with the value that
# triggered the error.
#
# Example:
# ```
# source([1,2,3])
# .map(workers: 2) { |v| sleep rand; v**2 } # => [4, 1, 9]
# ```
def map(
workers = 1,
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&block : T -> V
) : Channel(V) forall V
Channel(V).new(buffer_size).tap { |stream|
countdown = Channel(Nil).new(workers)
workers.times { |w_i|
spawn(name: name.try { |s| "#{s}.#{w_i}" }) do
self.listen { |t|
begin
res = block.call(t)
stream.send res
rescue ex
on_error.call(t, ex)
end
}
ensure
countdown.send(nil)
end
}
spawn(name: name.try { |s| "#{s}.countdown" }) do
workers.times { countdown.receive }
countdown.close
stream.close
end
}
end
# Returns a channel that receives each value of the enumerables produced
# applying *block* to values received by `self`.
#
# A *workers* parameter can be supplied to make the computation of the block concurrent.
# Note that, for *workers* > 1, no order guarantees are made - see `#map` for an example.
#
# The returned channel is closed once `self` is closed and empty, and all
# the outstanding runs of *block* are completed.
#
# See `#map` for details on the exception handling strategy.
def flat_map(
workers = 1,
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&block : T -> Enumerable(V)
) : Channel(V) forall V
enum_stream = map(workers: workers, name: name.try{ |s| "#{s}.map" },
on_error: on_error, &block)
Concur.flatten(enum_stream, name, buffer_size)
end
# Returns a channel that receives values from `self` transformed via *block* and based
# on the provided *initial_state*.
#
# NOTE: *block* is a function that takes the current state and a value received
# from `self` and returns a tuple composed of the next state to be passed to the block
# and the next value to be received by the returned channel.
#
# The returned channel is closed once `self` is closed and empty, and any outstanding
# run of *block* is completed.
#
# See `#map` for details on the exception handling strategy.
def map(
initial_state : S,
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&block : S, T -> {S, V}
) forall S,V
state = initial_state
self.map(name: name, buffer_size: buffer_size, on_error: on_error) { |t|
state, v = block.call(state, t)
v
}
end
# Returns a channel that receives each successive accumulated state produced by *block*,
# based on the initial state *acc* and on the values received by `self`.
#
# The returned channel is closed once `self` is closed and empty, and any outstanding
# run of *block* is completed.
#
# See `#map` for details on the exception handling strategy.
def scan(
acc : U,
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&block : U, T -> U
) : Channel(U) forall U
map(acc, name, buffer_size, on_error) { |state, v|
res = block.call(state, v)
{res, res}
}
end
# Returns a channel that receives tuples of values coming from `self` and *other*
# transformed via *block*.
#
# The returned channel is closed once either `self` or `other` are closed and empty,
# and any outstanding run of *block* is completed.
#
# See `#map` for details on the exception handling strategy.
def zip(
other : Channel(U),
name = nil,
buffer_size = 0,
on_error = ->(tu : {T, U}, ex : Exception) {},
&block : T, U -> V
) : Channel(V) forall U, V
Channel(V).new(buffer_size).tap { |stream|
spawn(name: name) do
loop do
p1 = self.receive
p2 = other.receive
begin
res = block.call(p1, p2)
stream.send res
rescue ex
on_error.call({p1, p2}, ex)
end
end
rescue Channel::ClosedError
stream.close
end
}
end
# Returns an array of *out_ports* channels which receive each value
# received by `self`.
#
# The returned channels will close once `self` is closed and empty.
#
# If one of the returned channels is closed, then `#broadcast` will
# close every other channel.
#
# NOTE: The rate at which values are received by each channel is limited
# by the slowest consumer. Values are sent to channels in the order they
# were returned.
def broadcast(out_ports = 2, name = nil, buffer_size = 0) : Array(Channel(T))
out_ports.times
.map { Channel(T).new(buffer_size) }
.to_a
.tap { |streams|
spawn(name: name) do
self.listen { |v|
streams.each(&.send(v))
}
streams.each(&.close)
end
}
end
# Returns a channel which receives values received by `self`
# that satisfy the given *predicate*.
#
# The returned channels will close once `self` is closed and empty.
#
# Any exception raised while evaluating *predicate* will be passed
# to the optional callback *on_error*, together with the value that
# triggered the error.
def select(
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&predicate : T -> Bool
) : Channel(T)
Channel(T).new(buffer_size).tap { |stream|
spawn(name: name) do
loop do
t = self.receive
begin
should_send = predicate.call(t)
stream.send(t) if should_send
rescue ex
on_error.call(t, ex)
end
end
rescue Channel::ClosedError
stream.close
end
}
end
# Returns a channel which receives values received by `self`
# that do **not** satisfy the given *predicate*.
#
# The returned channels will close once `self` is closed and empty.
#
# See `#select` for details on the exception handling strategy.
def reject(
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&predicate : T -> Bool
) : Channel(T)
self.select(name: name, buffer_size: buffer_size, on_error: on_error) { |v|
!predicate.call(v)
}
end
# Returns a tuple of two channels, one receiving values that satisfy
# the given *predicate* and one receiving the remaining ones.
#
# The returned channels will close once `self` is closed and empty.
#
# See `#select` for details on the exception handling strategy.
def partition(
name = nil,
buffer_size = 0,
on_error = ->(t : T, ex : Exception) {},
&predicate : T -> Bool
) : {Channel(T), Channel(T)}
{Channel(T).new(buffer_size), Channel(T).new(buffer_size)}.tap { |pass, fail|
spawn(name: name) do
self.listen do |t|
predicate.call(t) ? (pass.send(t)) : (fail.send(t))
rescue ex
on_error.call(t, ex)
end
pass.close
fail.close
end
}
end
# Returns a channel that receives values in batches either every *size* values
# received or every *interval*, if a batch has not been sent within the last *interval*.
#
# The returned channels will close once `self` is closed and empty.
def batch(size : Int32, interval : Time::Span, name = nil, buffer_size = 0) : Channel(Enumerable(T))
Channel(Enumerable(T)).new(buffer_size).tap { |stream|
memory = Array(T).new(size)
tick = Concur.every(interval) { nil }
sent = false
spawn(name: name) do
loop do
select
when v = self.receive
memory << v
if memory.size >= size
stream.send(memory.dup)
memory.clear
sent = true
end
when tick.receive
unless sent
stream.send(memory.dup)
memory.clear
end
sent = false
end
end
rescue Channel::ClosedError
stream.send(memory.dup)
stream.close
end
}
end
# Receives values from `self` and processes them via *block*.
#
# If no exceptions are raised while evaluating *block*, then the
# statement returns once `self` is closed and empty.
#
# NOTE: This method runs on the current fiber.
#
# NOTE: If exceptions are not handled within *block*, then any exception
# raised within the block will crash the calling fiber.
def listen(&block : T -> _)
loop do
block.call(self.receive)
rescue Channel::ClosedError
break
end
end
# Runs the given *block* for each value received from `self`.
# The execution of the block takes place on a separate fiber.
#
# The fiber running the block will stop once `self` is closed and empty.
#
# NOTE: If exceptions are not handled within *block*, then any exception
# raised within the block will crash the spawned fiber.
def each(name = nil, &block : T -> _)
spawn(name: name) do
self.listen(&block)
end
end
end