Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.94k stars 1.84k forks source link

ActorCoroutine with ConflatedBufferedChannel capactiy #4155

Open wjdtn7823 opened 3 months ago

wjdtn7823 commented 3 months ago

Use case

I want to build a ActorCoroutine via a Channel with Conflated Behavior and capacity. Reading the documentation, Channel should be build with capacity > 0 and, BufferOverFlow.DROP_OLDEST or BufferOverFlow.DROP_LATEST to accomplisth this.


public fun <E> Channel (
    capacity: Int = RENDEVZOUD,
    onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, // use BufferOverflow.DROP_LATEST or BufferOverflow.DROP_OLDEST
    onUndeliveredElement: ((E) ->Unit)? = null
)

However, the public actor API does not provide the way to build a Channel with BufferOverFlow Option

public fun <E> CoroutineScope.actor(
    context: CoroutineContext = EmptyCoroutineContext,
    capacity: Int = 0,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    onCompletion: CompletionHandler? = null,
    block: suspend  ActorScope<E>.() -> Unit
): SendChannel<E> {
//
   val channel = Channel<E>(capacity) //  onBufferOverFlow default value is used
...

I tried to make a custom actor builder API, but ActorCoroutine and LazyActorCoroutine inside the actor builder is package private so this won't work. Is there a reason to restrict this behavior? or can you change the API like below?

The Shape of the API

public fun <E> CoroutineScope.actor(
    context: CoroutineContext = EmptyCoroutineContext,
    capacity: Int = 0,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    onCompletion: CompletionHandler ?= null,
    onBufferOverflow: BufferOverflow: BufferOverflow.SUSPEND, // add onBufferOverflow option
    block : suspend ActorScope<E>.() -> Unit
): SendChannel<E> {
//
 val channel = Channel<E>(capacity, onBufferOverflow)
//
qwwdfsad commented 2 months ago

Unfortunately, actors are obsolete APIs, and we would like to avoid evolving them further or promoting their usage.

Could you please elaborate on the use-case where the actor is allowed to drop messages from its mailbox?