i want to use an AsyncStream
to buffer some plain-old-data:
actor SessionPool
{
private
let egress:AsyncStream<SessionIdentifier>.Iterator
nonisolated
let ingress:AsyncStream<SessionIdentifier>.Continuation
}
the idea is the stream would ensure that if a deinit
yields something to SessionPool.ingress
, that SessionIdentifier
should be available for the next consumer.
final
class Session
{
deinit
{
self.pool.ingress.yield(self.id)
}
}
@available(*, unavailable)
extension Session:Sendable
{
}
func useSessions(pool:SessionPool)
{
// number of generated sessions should never exceed 1!
do
{
let session:Session = await pool.next()
// session.deinit()
}
do
{
let session:Session = await pool.next()
// session.deinit()
}
}
but the problem is now implementing SessionPool.next()
.
extension SessionPool
{
nonisolated
func next() async -> Session
{
...
}
private
func next() -> SessionIdentifier
{
// ???
}
}
rather than suspending if self.egress
has no buffered session identifiers, next()
should just generate a new one and return it. ideally with some kind of API like
extension SessionPool
{
private
func next() -> SessionIdentifier
{
self.egress.nextIfBuffered() ?? self.generateNewSessionIdentifier()
}
}
is there any way to accomplish this?