Feedback on a Threadsafe Buffer Manager

In this conversation, I mentioned that I used a Channel to manage preallocated objects over the lifetime of a pipeline.

I was hoping someone could offer some feedback on my approach to that:

"""
    get_buffer_manager(size_hint=0)
Return a channel of IOBuffers. It will always have a fresh buffer ready, and can hold returned
buffers to allow reuse
"""
function get_buffer_manager(size_hint=0)
    return Channel{IOBuffer}(Threads.nthreads()+1) do ch
        while isopen(ch)
            if !isready(ch)
                @info "creating a new IOBuffer"
                put!(ch, IOBuffer(;sizehint=size_hint))
            end
            sleep(0.5)
        end
    end
end

I can then take a new IOBuffer at the top of the pipeline and then return it with put in the final step of the pipeline. It works nicely, but it feels silly to have it do this sleep/while loop thing to check if the channel is empty. Is there a blocking way to wait for a channel to be empty/ready? Is there a better way to do what Iā€™m doing here?

2 Likes