# Druby - How to close connection from client?

**URL:** <https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753>\
**Category:** Ruby\
**Created:** [July 25, 2006, 10:26am UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753 "2006-07-25T10:26:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [July 25, 2006, 10:26am UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753/1 "2006-07-25T10:26:26Z")

</div>

I need to disconnect explicitly from a druby client.

My code is like:

DRb.start\_service  
@drb = DRbObject.new(nil, ‘druby://localhost:7777’)  
@drb.dosomething(param1, param2, param3)  
…(here I’d like to disconnect!!)…

Background:  
I am using druby to communicate between a Rails application running on  
lighttpd/FreeBSD6 and a server-script running on the same box with  
different  
user rights.

I noticed that the longer I run the webserver the more sockets stay  
open,  
“sockstat | grep ruby” gives me hundreds of:

> www ruby18 26179 10 tcp4 \*:51359 _:_  
> www ruby18 26179 11 tcp4 \*:57690 _:_  
> www ruby18 26179 12 tcp4 \*:53473 _:_

I assume that every time I run the druby client on the website a  
connection  
remains open. They get all cleared if I restart lighttpd.

Thanks.  
martin

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [July 25, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753/2 "2006-07-25T15:40:02Z")

</div>

On 7/25/06, Martin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to disconnect explicitly from a druby client.
> 
> My code is like:
> 
> DRb.start\_service  
> @drb = DRbObject.new(nil, ‘druby://localhost:7777’)  
> @drb.dosomething(param1, param2, param3)  
> …(here I’d like to disconnect!!)…

DRb.stop\_service

You can make another call to DRb.start\_service if you want to restart  
your DRb connection. You’ll have to create another DRbObject, though,  
since stop\_service terminates all connections.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [July 25, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753/3 "2006-07-25T17:52:49Z")

</div>

Martin B. wrote:

> Background:  
> I am using druby to communicate between a Rails application running on  
> lighttpd/FreeBSD6 and a server-script running on the same box with different  
> user rights.

Martin- I’m doing something similar but using Rinda as a middle man.  
What I did was create a singleton class to handle the conenction, one  
connection stays open and gets reused.

Here’s what the client looks like.

#!/usr/bin/env ruby -w

require ‘rinda/ring’  
require ‘uuidtools’  
require ‘rinda’  
require ‘search\_result’

class SearchClient  
private\_class\_method :new  
@@tuple\_space = nil

def self.create  
if @@tuple\_space.nil?  
DRb.start\_service  
@@tuple\_space = Rinda::TupleSpaceProxy.new(DRbObject.new(nil,  
RINDA\_MASTER))  
end  
@@tuple\_space  
end

def self.user\_search(opts)  
retries = 0  
begin  
opts[:uuid] = UUID.random\_create.to\_s  
puts “outgoing =\> uuid: #{opts[:uuid]}\nopts: #{opts}”  
SearchClient.create.write([:UserSearch, DRb.uri, opts],  
RINDA\_TIMEOUT)  
tuple = SearchClient.create.take([:UserSearchResults,  
opts[:uuid], nil], RINDA\_TIMEOUT)  
SearchResult.new tuple[2]  
rescue DRb::DRbConnError  
# handle connection closed  
rescue  
@@tuple\_space = nil  
raise ‘Connection Error’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [July 25, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753/4 "2006-07-25T20:15:18Z")

</div>

That’s great! I will for sure look into this plugin…

For now I just do DRb.stop\_service as suggested by Tim and it seems to  
work  
fine.

Thanks everybody for the nice help.

Martin

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [July 25, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/druby-how-to-close-connection-from-client/99753/5 "2006-07-25T19:13:06Z")

</div>

On Jul 25, 2006, at 1:25 AM, Martin B. wrote:

> Background:
> 
> > www ruby18 26179 11 tcp4 \*:57690 _:_  
> > www ruby18 26179 12 tcp4 \*:53473 _:_
> 
> I assume that every time I run the druby client on the website a  
> connection  
> remains open. They get all cleared if I restart lighttpd.
> 
> Thanks.  
> martin

He Martin-

```
You may want to have a look at a rails plugin I wrote called

```

BackgrounDRb [1]. Its built for this exact purpose. You can have  
worker classes that run in the drb server that you can start and stop  
from rails. It also has hooks and examples of how to create ajax  
progress bars while your workers do their task.

Cheers-  
-Ezra

[1] [http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)
