Skip to content

Some way of subscribe to shared flow without slowing-down others #4224

Open
@kunyavskiy

Description

@kunyavskiy

Use case

I have a SharedFlow, which shares some complex computations with many consumers. Basically, I have two types of consumers: some important internal services of the application and web clients, which read data over HTTP or websocket.

Events in flow are some diffs with snapshots sent once in onSubscribe. None of the consumers is capable of skipping events. Web clients can reconnect to receive new snapshots; internal services must connect once. Also, internal services are quite fast and behave well, while web clients can be different because of connection issues or just the browser being too slow in drawing things.

And here I have a problem with organizing such a kind of subscription. I either need to make a large buffer before shareIn, which leads to unbounded memory usage, or, I need to allow a slow client to affect other consumers and even suspend computation, if there is a small buffer.

The Shape of the API

Ideally, I would like to specify some SharedFlow subscriptions as "unimportant". As an effect, if only "unimportant" subscribers still have not consumed an element that needs to be removed from the buffer, they are just canceled, and no suspension of the emitter happens.

I'm not sure what a good API for that is, but it probably involves something like the .onSubscribe function, which transforms shared flow into a special wrapper implementation of sharedFlow.

Maybe another option is resubscribing instead of canceling the consumer. It looks like it's not much distinguishable from DROP_OLDEST bufferisation, but in case of non-trivial onSubscribe it would be different.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions