Re: [akka-user] On Reconnecting Client Connections

2015-06-07 Thread Akka Team
Hi Peter,

Unfortunately this is one of the swampy areas where we have not good
support yet. This pattern is already implementable (Http client connection
pooling does something like this and more) but it is very error prone.
AsyncStage can work if you know what are you doing :) If you have something
that works (or almost work) share with us.

In the snippet you posted there is a problem, namely that you use
callback.invoke from foreach, and therefore you ignore any backpressure
signal from the downstream of AsyncStage. This can work, if the client Flow
does not create too much stuff for one input element, but for example if
the message you send to the client flow says "GIMME LARGE FILE", and then
you get several GB streamed back, then this will blow up memory.

-Endre

On Tue, Jun 2, 2015 at 8:35 PM, Peter Swift 
wrote:

> Hello,
>   I'm working on a client-side application atop tcp streams, and I'm
> curious what is the recommended approach for handling reconnects. I'm
> thinking to wrap the underlying transport flow in an AsyncStage, and then
> rematerialize the transport on completion. It would look something like
> this:
>
> class ReconnectionStage[In, Out](transport: Flow[In, Out, Any],
> maxInFlight: Int) extends AsyncStage[In, Out, Any] {
>   private var inFlight = 0
>   private val buffer = FixedSizeBuffer[Out](maxInFlight)
>
>   private var fm: FlowMaterializer = _
>   private var callback: AsyncCallback[Any] = _
>   private var runnableFlow: RunnableFlow[(ActorRef, Future[Unit])] = _
>
>   private var input: ActorRef = _
>
>   private def connect() {
> val (ref, future) = runnableFlow.run()(fm)
> input = ref
> future.onComplete( _ =>
> callback.invoke(Reconnect))(fm.executionContext)
>   }
>
>   override def preStart(ctx: AsyncContext[Out, Any]) {
> fm = ctx.materializer
> callback = ctx.getAsyncCallback()
> runnableFlow = Source.actorRef(maxInFlight, OverflowStrategy.fail)
>   .via(transport).toMat(Sink.foreach(callback.invoke))(Keep.both)
>   }
>   ...
> }
>
> It's a bit involved, and I haven't yet finished -- so I guess I'm just
> here to check if I'm even on the right track :)
>
> Cheers,
> Peter
>
>  --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] On Reconnecting Client Connections

2015-06-02 Thread Peter Swift
Hello,
  I'm working on a client-side application atop tcp streams, and I'm 
curious what is the recommended approach for handling reconnects. I'm 
thinking to wrap the underlying transport flow in an AsyncStage, and then 
rematerialize the transport on completion. It would look something like 
this:

class ReconnectionStage[In, Out](transport: Flow[In, Out, Any], 
maxInFlight: Int) extends AsyncStage[In, Out, Any] {
  private var inFlight = 0
  private val buffer = FixedSizeBuffer[Out](maxInFlight)

  private var fm: FlowMaterializer = _
  private var callback: AsyncCallback[Any] = _
  private var runnableFlow: RunnableFlow[(ActorRef, Future[Unit])] = _

  private var input: ActorRef = _

  private def connect() {
val (ref, future) = runnableFlow.run()(fm)
input = ref
future.onComplete( _ => callback.invoke(Reconnect))(fm.executionContext)
  }
  
  override def preStart(ctx: AsyncContext[Out, Any]) {
fm = ctx.materializer
callback = ctx.getAsyncCallback()
runnableFlow = Source.actorRef(maxInFlight, OverflowStrategy.fail)
  .via(transport).toMat(Sink.foreach(callback.invoke))(Keep.both)
  }
  ...
}

It's a bit involved, and I haven't yet finished -- so I guess I'm just here 
to check if I'm even on the right track :)

Cheers,
Peter

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.