|
| 1 | +export AsyncTrajectory, UPDATE, SAMPLE, NO_OP |
| 2 | + |
| 3 | +struct Update end |
| 4 | +const UPDATE = Update() |
| 5 | +struct SampleDecision end |
| 6 | +const SAMPLE = SampleDecision() |
| 7 | +struct NoOp end |
| 8 | +const NO_OP = NoOp() |
| 9 | + |
| 10 | + |
| 11 | +""" |
| 12 | + RateLimiter(n_samping_per_update=0.25; min_sampling_length=0, buffer_range=0:0) |
| 13 | +
|
| 14 | +- `n_samping_per_update`, a positive number, float number is supported. |
| 15 | +- `min_sampling_length`, minimal number of elements to start sampling. |
| 16 | +- `buffer_range`, if a single number is provided, it will be transformed into |
| 17 | + `-buffer_range:buffer_range`. Once the `trajectory` reaches |
| 18 | + `min_sampling_length`, if the length of `trajectory` is greater than the upper |
| 19 | + bound plus `min_sampling_length`, the `rate_limiter` always return `SAMPLE`. |
| 20 | + While if the length of `trajectory` is less than `min_sampling_length` minus |
| 21 | + the lower bound of `buffer_range`, then the `rate_limiter` always return |
| 22 | + `UPDATE`. In other cases, whether to `SAMPLE` or `UPDATE` depends on the |
| 23 | + availability of `in_channel` or `out_channel`. |
| 24 | +""" |
| 25 | +struct RateLimiter |
| 26 | + n_samping_per_update::Float64 |
| 27 | + min_sampling_length::UInt |
| 28 | + buffer_range::UnitRange{Int} |
| 29 | + is_min_sampling_length_reached::Ref{Bool} |
| 30 | +end |
| 31 | + |
| 32 | +RateLimiter(n_samping_per_update=0.25; min_sampling_length=0, buffer_range=0) = RateLimiter(n_samping_per_update, min_sampling_length, buffer_range) |
| 33 | +RateLimiter(n_samping_per_update, min_sampling_length, buffer_range::Number) = RateLimiter(n_samping_per_update, convert(UInt, min_sampling_length), range(-buffer_range, buffer_range)) |
| 34 | +RateLimiter(n_samping_per_update, min_sampling_length, buffer_range) = RateLimiter(n_samping_per_update, min_sampling_length, buffer_range, Ref(false)) |
| 35 | + |
| 36 | +""" |
| 37 | + (r::RateLimiter)(n_update, n_sample, is_in_ready, is_out_ready) |
| 38 | +
|
| 39 | +- `n_update`, number of elements inserted into `trajectory`. |
| 40 | +- `n_sample`, number of batches sampled from `trajectory`. |
| 41 | +- `is_in_ready`, the `in_channel` has elements to put into `trajectory` or not. |
| 42 | +- `is_out_ready`, the `out_channel` is ready to consume new samplings or not. |
| 43 | +""" |
| 44 | +function (r::RateLimiter)(n_update, n_sample, is_in_ready, is_out_ready) |
| 45 | + if n_update >= r.min_sampling_length |
| 46 | + r.is_min_sampling_length_reached[] = true |
| 47 | + end |
| 48 | + |
| 49 | + if r.is_min_sampling_length_reached[] |
| 50 | + n_estimated_updates = n_sample / r.n_samping_per_update + r.min_sampling_length |
| 51 | + if n_estimated_updates < n_update + r.buffer_range[begin] |
| 52 | + SAMPLE |
| 53 | + elseif n_estimated_updates > n_update + r.buffer_range[end] |
| 54 | + UPDATE |
| 55 | + else |
| 56 | + if is_out_ready |
| 57 | + SAMPLE |
| 58 | + elseif is_in_ready |
| 59 | + UPDATE |
| 60 | + else |
| 61 | + NO_OP |
| 62 | + end |
| 63 | + end |
| 64 | + else |
| 65 | + UPDATE |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +##### |
| 70 | + |
| 71 | +struct CallMsg |
| 72 | + f::Any |
| 73 | + args::Tuple |
| 74 | + kw::Any |
| 75 | +end |
| 76 | + |
| 77 | +struct AsyncTrajectory |
| 78 | + trajectory |
| 79 | + rate_limiter |
| 80 | + channel_in |
| 81 | + channel_out |
| 82 | + task |
| 83 | + n_update_ref |
| 84 | + n_sample_ref |
| 85 | + |
| 86 | + function AsyncTrajectory(trajectory, rate_limiter; channel_in=Channel(1), channel_out=Channel(1)) |
| 87 | + n_update_ref = Ref(0) |
| 88 | + n_sample_ref = Ref(0) |
| 89 | + task = @async while true |
| 90 | + decision = rate_limiter(n_update, n_sample, isready(channel_in), Base.n_avail(channel_out) < channel_out_size) |
| 91 | + if decision === UPDATE |
| 92 | + msg = take!(channel_in) |
| 93 | + if msg.f === Base.push! |
| 94 | + push!(trajectory, msg.args...; msg.kw...) |
| 95 | + n_update_ref[] += 1 |
| 96 | + elseif msg.f === Base.append! |
| 97 | + append!(trajectory, msg.args...; msg.kw...) |
| 98 | + n_update_ref[] += length(msg.data) |
| 99 | + else |
| 100 | + msg.f(trajectory, msg.args...; msg.kw...) |
| 101 | + end |
| 102 | + elseif decision === SAMPLE |
| 103 | + put!(channel_out, rand(trajectory)) |
| 104 | + n_sample_ref[] += 1 |
| 105 | + end |
| 106 | + end |
| 107 | + new( |
| 108 | + trajectory, |
| 109 | + channel_in, |
| 110 | + channel_out, |
| 111 | + task, |
| 112 | + n_update_ref, |
| 113 | + n_sample_ref |
| 114 | + ) |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +Base.push!(t::AsyncTrajectory, args...; kw...) = put!(t.in, CallMsg(Base.push!, args, kw)) |
| 119 | +Base.append!(t::AsyncTrajectory, args...; kw...) = put!(t.in, CallMsg(Base.append!, args, kw)) |
| 120 | +Base.take!(t::AsyncTrajectory) = take!(t.out) |
0 commit comments