推送数据到流

Push data to stream

是否可以创建一个 Source,我将能够向其推送数据 "manually"(或者我可以以某种方式将其推送到 "regular" Source)?

类似于:

var source = Source.Empty<int>();
source.Push(10); //is something like this possible?

我的用例是创建一个源,只要我的 API 端点被调用,我就可以将数据推送到该源。

是的,这是可能的。查看 Source.Queue:

Source.Queue can be used for emitting elements to a stream from an actor (or from anything running outside the stream). The elements will be buffered until the stream can process them. You can Offer elements to the queue and they will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.

另一种选择是Source.ActorRef:

Messages sent to the actor that is materialized by Source.ActorRef will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.

Source.Queue不同,Source.ActorRef不支持背压。