Structured caching in an actor

On Actors and caching;

I like implementing these algorithms lock free with Actors but this thread along with How to use withTaskCancellationHandler properly? point to some of the issues.

It can help to associate cancellation with a stable identifier — SE-0392 and Actor.assertIsolated (thanks @ktoso) gave me confidence to generalise cancellation which IMO really improves the API when used with Actors:

await withIdentifiableContinuation(isolation: self) { (continuation, id) in
  addContinuation(continuation, with: id)
} onCancel: { id in
  Task { await self.cancelContinuation(with: id) }
}

The continuation can be immediately appended to the actors isolated state and work commences. Cancellation is nonisolated so must be enqueued on the actor.

If only CheckedContinuation was Identifiable....

1 Like