Auto-reconnect?

58 views
Skip to first unread message

Vito Botta

unread,
May 6, 2011, 4:39:32 AM5/6/11
to ruby-brb
Hi!

I am using brb or a socked-based communication between various Ruby
processes, and it works great - I have already used it in the past.
The only issue that I am having in the case as well is that at times I
need to shut down and restart the server (for example because of
changes in the logic).
As it is now, clients will of course lose the connection with the
server and to re-establish this connection I need to manually restart
the clients as well otherwise they won't see the server.

Is there anything in brb which I may have missed, and that can ensure
clients automatically reconnect (or periodically try to) whenever the
connection to the server is lost?
Or, if this is something currently not available in brb itself, do you
have any suggestions on how to achieve this kind of behaviour?

Thanks!

Guillaume Luccisano

unread,
May 6, 2011, 6:22:38 PM5/6/11
to ruby...@googlegroups.com
Hi Vito,

I'm glad some people are using BrB ! So thanks for that !
But long time I haven't used the gem :/ But from memory yes, there is no reconnect functionnality.

I the client doesn't raise, you can might handle it using the callback :unregister:

        @core = BrB::Tunnel.create(self, p.ip) do |type, tunnel|
          if type == :unregister
            Error.create(:backtrace => server_status, :type => 'Connection to core lost', :url => "#{@proc.type}:#{@proc.port}-#{server_id}")
            raise 'Connection to core has been lost'
          end
        end


So you can probably use: tunne.reconnect => it's an event machine method: http://eventmachine.rubyforge.org/EventMachine/Connection.html#M000310

Tell me if you can handle this, I'm curious !

Thanks

2011/5/6 Vito Botta <ma...@vitobotta.com>

Guillaume Luccisano

unread,
May 6, 2011, 6:23:45 PM5/6/11
to ruby...@googlegroups.com
Ah, and if you want, you can definitely fork the project and try to implement it and ask me after to merge your changes.
That would be great !

2011/5/6 Guillaume Luccisano <guillaume...@gmail.com>

Vito Botta

unread,
May 7, 2011, 2:40:45 PM5/7/11
to ruby...@googlegroups.com
Hi Guillaume,

thanks to you! BrB is very useful indeed and I am using it quite often actually. 
I am currently looking to replace some communication between several machines that others have implemented in a client-server fashion with Sinatra, with BrB based communication since it's a lot faster and lighter.

I did find a very easy workaround (based in fact on the :unregister) for the automatic reconnection which works great, but I will also try your suggestion regarding EventMachine's reconnect to see if it works better. I have forked the project and will try this one first and then send a pull request.
I need to check how to add relevant specs first according to how you've already written some existing specs.

There's actually another change I have made in my code but I could instead move into BrB itself  since it could also be useful: keeping a track of the active clients, and recognising which clients disconnect. I find this useful since it can help find out the status of clients when you have (like in my case) many clients on many computers, interacting with a BrB server.

I'll let you know how this works and if I can move this functionality into BrB in a way you agree with :)
In the meantime, thanks a lot for BrB. The first time I used it was already ~one year ago and it has helped implement in a much better way a number of things, and since then I have used a number of times again because I work on a lot of projects with distributed processing and stuff like that. So I should have come long before to thank you since it saved me the time to code something similar ;)

merci beaucoup!
Vito

Guillaume Luccisano

unread,
May 10, 2011, 12:45:14 PM5/10/11
to ruby...@googlegroups.com
Hi Vito,

I'm very glad to hear about real case usage of BrB !

Thanks for forking and trying to improve the project, you can change the specs if needed as they are pretty old!

And thanks again for the very good feedback, I'm excited to see your changes !

De rien :)
Guillaume

2011/5/7 Vito Botta <vi...@botta.name>

dpree

unread,
May 13, 2011, 10:21:54 AM5/13/11
to ruby-brb
Hi Vito,

any progress here? I stumbled across BrB a few hours ago and now i am
trying to integrate it within a larger rails app. I also came across
the reconnect issue and so im wondering if you have already some code
to share..

By the way, thanks Guillaume for creating this :)

--dpree

On 7 Mai, 20:40, Vito Botta <v...@botta.name> wrote:
> Hi Guillaume,
>
> thanks to you! BrB is very useful indeed and I am using it quite often actually.
> I am currently looking to replace some communication between several machines that others have implemented in a client-server fashion with Sinatra, with BrB based communication since it's a lot faster and lighter.
>
> I did find a very easy workaround (based in fact on the :unregister) for the automatic reconnection which works great, but I will also try your suggestion regarding EventMachine's reconnect to see if it works better. I have forked the project and will try this one first and then send a pull request.
> I need to check how to add relevant specs first according to how you've already written some existing specs.
>
> There's actually another change I have made in my code but I could instead move into BrB itself since it could also be useful: keeping a track of the active clients, and recognising which clients disconnect. I find this useful since it can help find out the status of clients when you have (like in my case) many clients on many computers, interacting with a BrB server.
>
> I'll let you know how this works and if I can move this functionality into BrB in a way you agree with :)
> In the meantime, thanks a lot for BrB. The first time I used it was already ~one year ago and it has helped implement in a much better way a number of things, and since then I have used a number of times again because I work on a lot of projects with distributed processing and stuff like that. So I should have come long before to thank you since it saved me the time to code something similar ;)
>
> merci beaucoup!
> Vito
>
>
>
> On Friday, 6 May 2011 at 23:22, Guillaume Luccisano wrote:
> > Hi Vito,
>
> > I'm glad some people are using BrB ! So thanks for that !
> > But long time I haven't used the gem :/ But from memory yes, there is no reconnect functionnality.
>
> > I the client doesn't raise, you can might handle it using the callback :unregister:
>
> >  @core = BrB::Tunnel.create(self, p.ip) do |type, tunnel|
> >  if type == :unregister
> >  Error.create(:backtrace => server_status, :type => 'Connection to core lost', :url => "#...@proc.type}:#...@proc.port}-#{server_id}")
> >  raise 'Connection to core has been lost'
> >  end
> >  end
>
> > So you can probably use: tunne.reconnect => it's an event machine method:http://eventmachine.rubyforge.org/EventMachine/Connection.html#M000310
>
> > Tell me if you can handle this, I'm curious !
>
> > Thanks
>
> > 2011/5/6 Vito Botta <m...@vitobotta.com>

dpree

unread,
May 13, 2011, 10:28:44 AM5/13/11
to ruby-brb
Ah ok, the awesomeness of github :)

If just found a Gist from Vito :)

https://gist.github.com/970623

Vito Botta

unread,
May 13, 2011, 10:29:54 AM5/13/11
to ruby...@googlegroups.com
Hi dpree,

unfortunately I haven't had a chance yet to update brb directly to integrate this and other small changes. I have just started a new job and am already in the middle of my first project, so I don't have much time right now.
However I will be using BrB A LOT for this project so I will soon anyway need to tidy up this stuff.

So in the meantime, you can find a quick workaround in this gist: https://gist.github.com/970623
I will be looking into EventMachine's reconnect feature as suggested by Guillaume, but this workaround is so simple and just works!
So for the time being you may want to use this quick and dirty trick.

Cheers
Vito

Vito Botta

unread,
May 13, 2011, 10:30:18 AM5/13/11
to ruby...@googlegroups.com
LOL, I just created it :D

Guillaume Luccisano

unread,
May 13, 2011, 10:31:34 AM5/13/11
to ruby...@googlegroups.com
Nice ! There are activities going on here :)

2011/5/13 Vito Botta <vi...@botta.name>

Vito Botta

unread,
May 13, 2011, 10:35:28 AM5/13/11
to ruby...@googlegroups.com
hello there Guillaume!

I'll take this opportunity to ask you a quick one: do you think a single BrB "server" would be ok by itself to keep potentially a few hundreds or thousands of connections with clients?
There would be quite infrequent activity with the clients, the only thing is that they should be able to keep a connection for monitoring.
Do you think that BrB would be OK for this purpose since it's based on EventMachine, or do you think it wouldn't easily manage many connections open (regardless of the infrequent activity)?

Merci!

Gonzalo Rodríguez-Baltanás Díaz

unread,
May 13, 2011, 10:41:44 AM5/13/11
to ruby...@googlegroups.com

Hi,

There is a test suite that specifically test that :)


You could try changing the number to 10000 and see what happens. Let us know :)

Guillaume Luccisano

unread,
May 13, 2011, 10:39:41 AM5/13/11
to ruby...@googlegroups.com
Hmmm, I would say that the BrB layer is pretty small on top of EventMachine and EM is well known to handle a big number of connections, so you can probably easily handle a few thousands :)

De rien

2011/5/13 Vito Botta <vi...@botta.name>

Vito Botta

unread,
May 13, 2011, 10:45:41 AM5/13/11
to ruby...@googlegroups.com
Oh yes Gonzalo, I have seen that spec time ago but I didn't remember about it!
Cheers
Vito

Vito Botta

unread,
May 13, 2011, 10:46:21 AM5/13/11
to ruby...@googlegroups.com
Thanks for confirming this. I am pretty sure BrB is going to be very helpful for this new project I am working on!

Vito

Guillaume Luccisano

unread,
May 13, 2011, 10:47:06 AM5/13/11
to ruby...@googlegroups.com
Eheh, let us know how it goes !

2011/5/13 Vito Botta <vi...@botta.name>

Vito Botta

unread,
May 13, 2011, 10:48:01 AM5/13/11
to ruby...@googlegroups.com
Will do ;)

dpree

unread,
May 13, 2011, 10:48:25 AM5/13/11
to ruby-brb
@Vito ;-D

Thanks for the gist, already tried it. On the first look it worked,
but actually didnt :)

def connect!(host, port)
url = "brb://#{host}:#{port}"
Rails.logger.info "Trying to connect #{url} ..."
BrB::Tunnel.create(nil, url, :verbose => true) do |type, tunnel|
if type == :unregister
Rails.logger.info "[#{type}] Could not connect #{url}, Retry in
2sec."
sleep 2
connect!
else
Rails.logger.info "[#{type}] Successfully connected to #{url}"
end
end
end

First i tried this while running the server, then i switched it
offline. No matter, output was both times the same:

Trying to connect brb://localhost:5555 ...
[register] Successfully connected to brb://localhost:5555
[unregister] Could not connect brb://localhost:5555, Retry in 2sec.


I will do further investigations.. Thanks again and good luck with
your new job ;)

--dpree

Guillaume Luccisano

unread,
May 13, 2011, 10:45:36 AM5/13/11
to ruby...@googlegroups.com
Hi Gonzalo, 4 people on this group! WOh !

Ehehe, True, thanks for finding it, I forgot about it!
But this one send a lot of commands an top of that ;)

2011/5/13 Gonzalo Rodríguez-Baltanás Díaz <sio...@gmail.com>

Vito Botta

unread,
May 13, 2011, 10:53:25 AM5/13/11
to ruby...@googlegroups.com
Thanks for the "good luck" :)

Unless I am missing something, in your code you are calling connect! but without passing again the host and port. The example in the gist uses instance variables instead.
So - again, unless I am missing something - you should just pass host, port to the connect! call

Vito

Vito Botta

unread,
May 13, 2011, 10:55:20 AM5/13/11
to ruby...@googlegroups.com
BTW the example in the gist is extracted by actual code I am already using, and I have done some "stability tests" to see if the auto reconnect works, and it seems to be working fine.
Basically I have a single server shutting down and restarting every few minutes, and 10 test clients reconnecting each time soon after the server is back up, so yeah it seems to be working for me :)

dpree

unread,
May 13, 2011, 11:02:57 AM5/13/11
to ruby-brb
Urgh.. just that simple :) Thanks again!

However, when the server goes offline then there a callbacks logged
that say:
unregistered-registered-....sleep2...unregistered-
registered-....sleep2......
That goes until the server is online again. Then the logs stay quite.

What i would expect is just:
unregistered-....sleep2....-unregistered-.....sleep2....

But for the moment this is ok. I will have a look on this later..

--dpree

Vito Botta

unread,
May 13, 2011, 11:05:51 AM5/13/11
to ruby...@googlegroups.com
yep that is normal because it just does the connection again, so it also invokes the register code.
However you can easily avoid that by just adding a flag for example like "already_connected = false" that is set to true on the first connection. Then your register block doesn't do anything if already_connected = true.
Perhaps ugly but it would work
Reply all
Reply to author
Forward
0 new messages