Michael S. Fischer
2007-Feb-24 12:38 UTC
[Eventmachine-talk] Passing data to connection handlers
Hi everyone, I''ve created a number of services that exist as subclasses of EventMachine::Connection, each of which is started by passing them as arguments to EventMachine::start_server. For example: class HTTPConnHandler < EventMachine::Connection ... end EventMachine::run { EventMachine::start_server("127.0.0.1", 8080, HTTPConnHandler) [...] } My problem: I would like to pass arbitrary data, e.g. a debugging flag, to the connection handler so I can have it perform additional logging if necessary. Is this possible? I know I can do it with environment variables, but this is probably suboptimal in terms of performance. Best regards, --Michael
Tony Arcieri
2007-Feb-24 13:49 UTC
[Eventmachine-talk] Passing data to connection handlers
You could use a class variable - Tony On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote:> > Hi everyone, > > I''ve created a number of services that exist as subclasses of > EventMachine::Connection, each of which is started by passing them as > arguments to EventMachine::start_server. > > For example: > > class HTTPConnHandler < EventMachine::Connection > ... > end > > EventMachine::run { > EventMachine::start_server("127.0.0.1", 8080, HTTPConnHandler) > [...] > } > > My problem: I would like to pass arbitrary data, e.g. a debugging > flag, to the connection handler so I can have it perform additional > logging if necessary. > > Is this possible? I know I can do it with environment variables, but > this is probably suboptimal in terms of performance. > > Best regards, > > --Michael > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-- Tony Arcieri ClickCaster, Inc. tony at clickcaster.com (970) 232-4208 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070224/8ff5f6fd/attachment.html
Michael S. Fischer
2007-Feb-24 14:46 UTC
[Eventmachine-talk] Passing data to connection handlers
Great! Example code would be helpful. It''s critical that the value originate outside the class definition, though. Best regards, --Michael On 2/24/07, Tony Arcieri <tony at clickcaster.com> wrote:> You could use a class variable > > - Tony > > > On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote: > > > > Hi everyone, > > > > I''ve created a number of services that exist as subclasses of > > EventMachine::Connection, each of which is started by passing them as > > arguments to EventMachine::start_server. > > > > For example: > > > > class HTTPConnHandler < EventMachine::Connection > > ... > > end > > > > EventMachine::run { > > EventMachine::start_server("127.0.0.1", 8080, HTTPConnHandler) > > [...] > > } > > > > My problem: I would like to pass arbitrary data, e.g. a debugging > > flag, to the connection handler so I can have it perform additional > > logging if necessary. > > > > Is this possible? I know I can do it with environment variables, but > > this is probably suboptimal in terms of performance. > > > > Best regards, > > > > --Michael > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > -- > Tony Arcieri > ClickCaster, Inc. > tony at clickcaster.com > (970) 232-4208 > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
Tony Arcieri
2007-Feb-25 12:56 UTC
[Eventmachine-talk] Passing data to connection handlers
You can initialize a class variable with a global - Tony On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote:> > Great! Example code would be helpful. It''s critical that the value > originate outside the class definition, though. > > Best regards, > > --Michael > > On 2/24/07, Tony Arcieri <tony at clickcaster.com> wrote: > > You could use a class variable > > > > - Tony > > > > > > On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote: > > > > > > Hi everyone, > > > > > > I''ve created a number of services that exist as subclasses of > > > EventMachine::Connection, each of which is started by passing them as > > > arguments to EventMachine::start_server. > > > > > > For example: > > > > > > class HTTPConnHandler < EventMachine::Connection > > > ... > > > end > > > > > > EventMachine::run { > > > EventMachine::start_server("127.0.0.1", 8080, HTTPConnHandler) > > > [...] > > > } > > > > > > My problem: I would like to pass arbitrary data, e.g. a debugging > > > flag, to the connection handler so I can have it perform additional > > > logging if necessary. > > > > > > Is this possible? I know I can do it with environment variables, but > > > this is probably suboptimal in terms of performance. > > > > > > Best regards, > > > > > > --Michael > > > _______________________________________________ > > > Eventmachine-talk mailing list > > > Eventmachine-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > > > > > > -- > > Tony Arcieri > > ClickCaster, Inc. > > tony at clickcaster.com > > (970) 232-4208 > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-- Tony Arcieri ClickCaster, Inc. tony at clickcaster.com (970) 232-4208 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070225/cdf66f02/attachment.html
Francis Cianfrocca
2007-Feb-25 19:15 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote: Try this: EventMachine::run { EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| # conn is a newly-accepted instance of class HTTPConnHandler. # This block is called via instance_eval AFTER initialize, # and BEFORE Connection#post_init. This execution order is documented # and you can depend on it not changing. } } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070225/9b654450/attachment.html
Michael S. Fischer
2007-Feb-28 14:00 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/25/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> On 2/24/07, Michael S. Fischer <michael at dynamine.net> wrote: > > Try this: > > EventMachine::run { > EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| > # conn is a newly-accepted instance of class HTTPConnHandler. > # This block is called via instance_eval AFTER initialize, > # and BEFORE Connection#post_init. This execution order is > documented > # and you can depend on it not changing. > } > }It looks like the block is executed in the context of the ConnHandler object, not the caller (i.e. it doesn''t appear to be a true closure). Thus, the following code will fail, because DEBUG isn''t defined within the scope of the Handler: DEBUG=true EventMachine::run { EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| @debug=DEBUG } } Is there any other way to do this than to use a global (e.g. $DEBUG)? If not, I suppose I can live with using a global, but it''s not my preference. Best regards, --Michael
Francis Cianfrocca
2007-Feb-28 15:42 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/28/07, Michael S. Fischer <michael at dynamine.net> wrote:> > > It looks like the block is executed in the context of the ConnHandler > object, not the caller (i.e. it doesn''t appear to be a true closure). > Thus, the following code will fail, because DEBUG isn''t defined within > the scope of the Handler: > > DEBUG=true > EventMachine::run { > EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| > @debug=DEBUG > } > }This turned out to be an interesting question. You can see the relevant code in eventmachine.rb at line 810, where the block gets called. And you''re right, it''s not called in the context of the created object, but rather the created object (the new connection) is passed in as a parameter. Seemed to me when I was writing this, that this is the typical style for objects that take a block to an initialize method. But I''m not seeing the closure problem. This code worked as expected for me: require ''rubygems'' require ''eventmachine'' class X < EventMachine::Connection def receive_data data puts "Debug flag: #{@debug}" end end DEBUG = true EventMachine.run { EventMachine.start_server( host, port, X ) {|conn| conn.instance_eval {@debug = DEBUG} } } ------------------------------------------- alternatively, require ''rubygems'' require ''eventmachine'' class X < EventMachine::Connection attr_accessor :debug def receive_data data puts "Debug flag: #{debug}" end end DEBUG = true EventMachine.run { EventMachine.start_server( host, port, X ) {|conn| conn.debug = DEBUG } } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070228/a0195bd4/attachment.html
Michael S. Fischer
2007-Feb-28 16:07 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/28/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> But I''m not seeing the closure problem. This code worked as expected for me: > > require ''rubygems'' > require ''eventmachine'' > > class X < EventMachine::Connection > def receive_data data > puts "Debug flag: #{@debug}" > end > end > > DEBUG = true > EventMachine.run { > EventMachine.start_server( host, port, X ) {|conn| > conn.instance_eval {@debug = DEBUG} > } > }Okay, the reason why that worked is because you explicitly called conn.instance_eval inside the block, whereas I did not. Compare my posted sample:> > DEBUG=true > > EventMachine::run { > > EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| > > @debug=DEBUG > > } > > }I guess I took your instructions too literally when you said: # This block is called via instance_eval AFTER initialize, # and BEFORE Connection#post_init. This execution order is documented # and you can depend on it not changing. I thought what you meant by this was that calling instance_eval was not necessary, as it would be done on my behalf. --Michael
Francis Cianfrocca
2007-Feb-28 20:46 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/28/07, Michael S. Fischer <michael at dynamine.net> wrote:> > Okay, the reason why that worked is because you explicitly called > conn.instance_eval inside the block, whereas I did not. Compare my > posted sample: > > > > DEBUG=true > > > EventMachine::run { > > > EventMachine::start_server( host, port, HTTPConnHandler ) {|conn| > > > @debug=DEBUG > > > } > > > } > > I guess I took your instructions too literally when you said: > > # This block is called via instance_eval AFTER initialize, > # and BEFORE Connection#post_init. This execution order is > documented > # and you can depend on it not changing. > > I thought what you meant by this was that calling instance_eval was > not necessary, as it would be done on my behalf.As I said, I was trying to copy what I think is the typical style of the standard libraries when an #initialize method takes a block. Maybe I''m wrong about that! The code line in EM is eventmachine.rb, line 810, which calls the block, passing the new object as a parameter. So obviously instance_eval is required if you want to touch the innards of the connection object. That documentation about the execution order is very important, because there is a lot of code that depends on the execution order of those three items (the constructor [which may be subclassed], post_init, and the block. There is a lot of code which depends on that execution order. I thought it was essential to guarantee a documented order because some people may feel that the block should intuitively be called before post_init, not after. But note: nothing in that documentation refers to the context in which the block is called! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070228/ed14506c/attachment.html
Michael S. Fischer
2007-Mar-01 09:05 UTC
[Eventmachine-talk] Passing data to connection handlers
On 2/28/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> But note: nothing in that documentation refers to the context in which the > block is called!How amenable are you to accepting documentation patches from third parties? --Michael
Francis Cianfrocca
2007-Mar-02 08:44 UTC
[Eventmachine-talk] Passing data to connection handlers
On 3/1/07, Michael S. Fischer <michael at dynamine.net> wrote:> > On 2/28/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote: > > > But note: nothing in that documentation refers to the context in which > the > > block is called! > > How amenable are you to accepting documentation patches from third > parties?Totally amenable and grateful. As long as you accept that I may edit them. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070302/65f01ead/attachment.html