# Correct way to use DRb within a Rails Controller?

**URL:** <https://www.ruby-forum.com/t/correct-way-to-use-drb-within-a-rails-controller/50204>\
**Category:** Ruby\
**Created:** [December 14, 2005, 5:01am UTC](https://www.ruby-forum.com/t/correct-way-to-use-drb-within-a-rails-controller/50204 "2005-12-14T05:01:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [December 14, 2005, 5:01am UTC](https://www.ruby-forum.com/t/correct-way-to-use-drb-within-a-rails-controller/50204/1 "2005-12-14T05:01:32Z")

</div>

[not really rails specific]  
I’m unsure about what should be placed where or if it even matters.  
Here’s the basic rails controller:

-------please\_work\_controller.rb----------  
class PleaseWork \< ApplicationController  
def get\_name  
@name = retrieve.name  
end  
def get\_age  
@age = retrieve.age  
end  
end  
-------/please\_work\_controller.rb--------  
To create your typical client DRb connection, you’ll need the following:

```
require 'drb'
DRb.start_service
retrieve = DRbObject.new(nil,"druby://#{hostname}:#{port}")

```

Where should these definitions be placed? They seem to work just about  
anywhere but whats the optimal placement? Also, I realize retrieve  
does not _need_ to be defined but since it is used throughout the  
class in other methods as such…

Thanks as usual!

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [December 15, 2005, 3:33am UTC](https://www.ruby-forum.com/t/correct-way-to-use-drb-within-a-rails-controller/50204/2 "2005-12-15T03:33:58Z")

</div>

On Dec 13, 2005, at 7:59 PM, x1 wrote:

> ```
> @age = retrieve.age
> end
> 
> ```
> 
> end  
> -------/please\_work\_controller.rb--------  
> To create your typical client DRb connection, you’ll need the  
> following:
> 
> ```
> require 'drb'
> DRb.start_service
> retrieve = DRbObject.new(nil,"druby://#{hostname}:#{port}")
> 
> ```

Depending upon how long your DRb server lives you can probably make  
retrieve a global and put it in environment.rb. Score bonus points  
for using NamedIdConv.

> Where should these definitions be placed? They seem to work just about  
> anywhere but whats the optimal placement? Also, I realize retrieve  
> does not _need_ to be defined but since it is used throughout the  
> class in other methods as such…
> 
> Thanks as usual!

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [December 17, 2005, 4:29am UTC](https://www.ruby-forum.com/t/correct-way-to-use-drb-within-a-rails-controller/50204/3 "2005-12-17T04:29:20Z")

</div>

The reason I asked this originally, was because webrick would just  
come to a halt after about 10-15 http requests…

After eliminating ram/cpu/gc as the culprit, I did a netstat -a and  
found loads of open sessions…

Removing DRb.start\_server from the controller all together -fixed it.
