vaskir
06/14/2017, 1:58 PMObjectPool
from Hopac (to see whether it will be easier to understand). I'm inlining that functions into the main select
, looks ok. Another problem is porting the Nacks. As I understand, coroutines do not support them? In short, a Nack is a alternative that becomes available for synchronization when client code's select
does not select the alternative which this Nack is bound to. So I ported this function:
reqCh ^=> fun (nack, replyCh) ->
let ok available instance =
replyCh *<- Ok instance ^=>. loop (available, given + 1u) <|>
nack ^=>. loop (instance :: available, given)
match available with
| instance :: available -> ok available instance
| [] -> try createNew () |> PoolEntry.Create |> ok available
with e -> (replyCh *<- Fail e <|> nack) ^=>. loop (available, given)
to this select branch:
reqCh.onReceive { (nack, replyCh) ->
if (available.empty()) {
try {
val instance = PoolEntry(createNew())
select<Unit> {
replyCh.onSend(Ok(instance)) { given++ }
nack.onSend(Unit) { available.add(instance) }
}
} catch (e : Throwable) {
select<Unit> {
replyCh.onSend(Err(e)) {}
nack.onSend(Unit) {}
}
}
}
else {
val instance = available.pop()
select {
replyCh.onSend(Ok(instance)) { given++ }
nack.onSend(Unit) { available.add(instance) }
}
}
}
https://github.com/Hopac/Hopac/blob/master/Docs/Programming.md#negative-acknowledgments
I'm not saying Kotlin must support nacks, just asking if ^^^ make sense.