# DRb::DRbObject.alive?

**URL:** <https://rubytalk.org/t/drb-drbobject-alive/56373>\
**Category:** ruby-talk\
**Created:** [24 November 2009 23:04 UTC](https://rubytalk.org/t/drb-drbobject-alive/56373 "2009-11-24T23:04:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benoit\_Daloze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/benoit_daloze/32/1967_2.png) [@Benoit\_Daloze](https://rubytalk.org/u/Benoit_Daloze)\
**Post date:** [24 November 2009 23:04 UTC](https://rubytalk.org/t/drb-drbobject-alive/56373/1 "2009-11-24T23:04:43Z")

</div>

Hi,

I found a feature that seem to be missing to me in DRb.

I'm just creating a chat with a client and a server, using only DRb to  
interact between them.

My problem is I didn't found a nice way to verify if a client is alive.  
I would like to be able to update my clients' list, but I need to know if  
they are alive.

Currently, I'm getting around with catching the Exception DRb::DRbConnError,  
but that looks really ugly:  
@clients.each { |c|  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;c.respond\_to?(:some\_method\_defined\_in\_client) #Trying to use any method  
of c will raise the exception (except if not defined)  
&nbsp;&nbsp;rescue DRb::DRbConnError  
&nbsp;&nbsp;&nbsp;&nbsp;@clients.delete(c)  
&nbsp;&nbsp;end  
}

Using Exceptions would be meaningful when I try to send(it would raise the  
exception if I call #send), but then I would have to manage Exceptions  
everywhere.

The method alive? is defined for the DRbServer and DRbTCPSocket, but not for  
DRbObject! (in fact if the Client just got disconnected, alive? will still  
return true)

Does somebody already met this? (And does somebody knows what DRb is?^^  
Please have a look it's pretty cool, but the documentation could even be  
better...)

Benoit

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 November 2009 07:15 UTC](https://rubytalk.org/t/drb-drbobject-alive/56373/2 "2009-11-25T07:15:05Z")

</div>

> I found a feature that seem to be missing to me in DRb.
> 
> I'm just creating a chat with a client and a server, using only DRb to  
> interact between them.
> 
> My problem is I didn't found a nice way to verify if a client is alive.  
> I would like to be able to update my clients' list, but I need to know if  
> they are alive.
> 
> Currently, I'm getting around with catching the Exception DRb::DRbConnError,  
> but that looks really ugly:  
> @clients.each { |c|  
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;c.respond\_to?(:some\_method\_defined\_in\_client) #Trying to use any method  
> of c will raise the exception (except if not defined)  
> &nbsp;&nbsp;rescue DRb::DRbConnError  
> &nbsp;&nbsp;&nbsp;&nbsp;@clients.delete(c)  
> &nbsp;&nbsp;end  
> }
> 
> Using Exceptions would be meaningful when I try to send(it would raise the  
> exception if I call #send), but then I would have to manage Exceptions  
> everywhere.

Well, but this is the usual approach.

> The method alive? is defined for the DRbServer and DRbTCPSocket, but not for  
> DRbObject! (in fact if the Client just got disconnected, alive? will still  
> return true)

It should not be too difficult to add method alive? to DRbUndumped. However, there is a more fundamental problem that cannot be solved: even if you have method alive? and it is working properly what does it tell you? It tells you that the object was alive when you invoked that method. You have no guarantees that it will be alive when you make the next remote method call. Hence you need to handle exceptions anyway. Now you have exception handling plus additional code that checks liveness beforehand. Your code is complexer and slower.

> Does somebody already met this?

This is a common issue people stumble over all the time (typically with things that involve interprocess communication such as for database connections and the like). The issue is always the same: you do not gain anything with this method because you have to deal with failure anyway.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 11/25/2009 12:04 AM, Benoit Daloze wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Benoit\_Daloze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/benoit_daloze/32/1967_2.png) [@Benoit\_Daloze](https://rubytalk.org/u/Benoit_Daloze)\
**Post date:** [25 November 2009 21:48 UTC](https://rubytalk.org/t/drb-drbobject-alive/56373/3 "2009-11-25T21:48:18Z")

</div>

Yeah, you're right.

So here's how I did to not duplicate the code:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def try\_on\_clients  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@clients.each { |client|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield client  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue DRb::DRbConnError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;del\_drb\_client(client)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def receive\_message(msg)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try\_on\_clients { |client| client.show\_message(msg) }  
&nbsp;&nbsp;&nbsp;&nbsp;end

That looks already more beautiful, and doesn't spend time looking if alive.  
Anyway, it's a little too bad for the client, because any connection error  
will drop him away, but he can reconnect easily.

Regards,  
Benoit

> **···**
>
> 2009/11/25 Robert Klemme \<shortcutter@googlemail.com\>
> 
> > On 11/25/2009 12:04 AM, Benoit Daloze wrote:
> > 
> > > I found a feature that seem to be missing to me in DRb.
> > > 
> > > I'm just creating a chat with a client and a server, using only DRb to  
> > > interact between them.
> > > 
> > > My problem is I didn't found a nice way to verify if a client is alive.  
> > > I would like to be able to update my clients' list, but I need to know if  
> > > they are alive.
> > > 
> > > Currently, I'm getting around with catching the Exception  
> > > DRb::DRbConnError,  
> > > but that looks really ugly:  
> > > @clients.each { |c|  
> > > begin  
> > > &nbsp;&nbsp;&nbsp;c.respond\_to?(:some\_method\_defined\_in\_client) #Trying to use any method  
> > > of c will raise the exception (except if not defined)  
> > > rescue DRb::DRbConnError  
> > > &nbsp;&nbsp;&nbsp;@clients.delete(c)  
> > > end  
> > > }
> > > 
> > > Using Exceptions would be meaningful when I try to send(it would raise the  
> > > exception if I call #send), but then I would have to manage Exceptions  
> > > everywhere.
> > 
> > Well, but this is the usual approach.
> > 
> > The method alive? is defined for the DRbServer and DRbTCPSocket, but not
> > 
> > > for  
> > > DRbObject! (in fact if the Client just got disconnected, alive? will still  
> > > return true)
> > 
> > It should not be too difficult to add method alive? to DRbUndumped.  
> > However, there is a more fundamental problem that cannot be solved: even if  
> > you have method alive? and it is working properly what does it tell you? It  
> > tells you that the object was alive when you invoked that method. You have  
> > no guarantees that it will be alive when you make the next remote method  
> > call. Hence you need to handle exceptions anyway. Now you have exception  
> > handling plus additional code that checks liveness beforehand. Your code is  
> > complexer and slower.
> > 
> > Does somebody already met this?
> > 
> > > 
> > 
> > This is a common issue people stumble over all the time (typically with  
> > things that involve interprocess communication such as for database  
> > connections and the like). The issue is always the same: you do not gain  
> > anything with this method because you have to deal with failure anyway.
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> > 
> > --  
> > remember.guy do |as, often| as.you\_can - without end  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2009 09:16 UTC](https://rubytalk.org/t/drb-drbobject-alive/56373/4 "2009-11-26T09:16:43Z")

</div>

Well, you could place them in a closed list (or queue) or even return  
the array of closed / removed clients.

Kind regards

robert

> **···**
>
> 2009/11/25 Benoit Daloze \<eregontp@gmail.com\>:
> 
> > Yeah, you're right.
> > 
> > So here's how I did to not duplicate the code:
> > 
> > ```
> > def try\_on\_clients
> > @clients\.each \{ |client|
> > begin
> > yield client
> > rescue DRb::DRbConnError
> > del\_drb\_client\(client\)
> > end
> > \}
> > 
> > ```
> > 
> > end
> > 
> > def receive\_message(msg)  
> > try\_on\_clients { |client| client.show\_message(msg) }  
> > end
> > 
> > That looks already more beautiful, and doesn't spend time looking if alive.  
> > Anyway, it's a little too bad for the client, because any connection error  
> > will drop him away, but he can reconnect easily.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
