Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Accessing a session globally

Received: by 10.101.8.27 with SMTP id l27mr977151ani.16.1272436765173;
        Tue, 27 Apr 2010 23:39:25 -0700 (PDT)
X-BeenThere: sinatrarb@googlegroups.com
Received: by 10.101.145.21 with SMTP id x21ls46684673ann.3.p; Tue, 27 Apr 2010 
	23:39:23 -0700 (PDT)
Received: by 10.101.141.3 with SMTP id t3mr5005696ann.42.1272436763799;
        Tue, 27 Apr 2010 23:39:23 -0700 (PDT)
Received: by 10.101.141.3 with SMTP id t3mr5005695ann.42.1272436763772;
        Tue, 27 Apr 2010 23:39:23 -0700 (PDT)
Return-Path: <phr...@mac.com>
Received: from mail-yw0-f167.google.com (mail-yw0-f167.google.com [209.85.211.167])
        by gmr-mx.google.com with ESMTP id 11si690548gxk.9.2010.04.27.23.39.23;
        Tue, 27 Apr 2010 23:39:23 -0700 (PDT)
Received-SPF: softfail (google.com: domain of transitioning phr...@mac.com does not designate 209.85.211.167 as permitted sender) client-ip=209.85.211.167;
Received: by mail-yw0-f167.google.com with SMTP id 39so10847699ywh.24
        for <sinatrarb@googlegroups.com>; Tue, 27 Apr 2010 23:39:23 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.101.180.37 with SMTP id h37mr960038anp.64.1272436763708; Tue, 
	27 Apr 2010 23:39:23 -0700 (PDT)
Received: by f6g2000yqa.googlegroups.com with HTTP; Tue, 27 Apr 2010 23:39:23 
	-0700 (PDT)
Date: Tue, 27 Apr 2010 23:39:23 -0700 (PDT)
In-Reply-To: <CD7D022F-791E-4850-95A0-98FD881574D8@finn.de>
X-IP: 204.144.179.235
References: <2f0931ea-5bbf-4552-b99e-450e051602a9@29g2000yqp.googlegroups.com> 
	<CD7D022F-791E-4850-95A0-98FD881574D8@finn.de>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) 
	AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7,gzip(gfe)
Message-ID: <5499b693-4331-4440-837f-186901ee5dff@f6g2000yqa.googlegroups.com>
Subject: Re: Accessing a session globally
From: Phrogz <phr...@mac.com>
To: sinatrarb <sinatrarb@googlegroups.com>
X-Original-Authentication-Results: gmr-mx.google.com; spf=softfail 
	(google.com: domain of transitioning phr...@mac.com does not designate 
	209.85.211.167 as permitted sender) smtp.mail=phr...@mac.com
X-Original-Sender: phr...@mac.com
Reply-To: sinatrarb@googlegroups.com
Precedence: list
Mailing-list: list sinatrarb@googlegroups.com; contact sinatrarb+owners@googlegroups.com
List-ID: <sinatrarb.googlegroups.com>
List-Post: <http://groups.google.com/group/sinatrarb/post?hl=en_US>, 
	<mailto:sinatrarb@googlegroups.com>
List-Help: <http://groups.google.com/support/?hl=en_US>, <mailto:sinatrarb+help@googlegroups.com>
List-Archive: <http://groups.google.com/group/sinatrarb?hl=en_US>
Sender: sinatrarb@googlegroups.com
List-Subscribe: <http://groups.google.com/group/sinatrarb/subscribe?hl=en_US>, 
	<mailto:sinatrarb+subscribe@googlegroups.com>
List-Unsubscribe: <http://groups.google.com/group/sinatrarb/subscribe?hl=en_US>, 
	<mailto:sinatrarb+unsubscribe@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1

On Apr 28, 12:10 am, Konstantin Haase <k.ha...@finn.de> wrote:
> On Apr 28, 2010, at 04:46 , Phrogz wrote:
> > How can I get access to the session "globally" from outside my route
> > in Sinatra?
>
> If that was possible, Sinatra would not be able to run in multithreaded environments (what happens if two sessions are active at the same time?)

I'm not sure. Does Thin, for example, run multiple threads off of the
same instances? I note that each request from Thin has a new
Sinatra::Application instance. I really don't understand well the
implementation details of Thin/Sinatra/Rack in terms of what is
instantiated in the same thread/process and what is (or can be)
created in a new Thread or new process.


> Why don't you pass the session (or better: just the values you need) from your route to your model?

In part because this is gross (but of course not the only way):
  def tag_names=( desired_tags, user_id ) ... end
  ...
  model.send(:tag_names=,"foo bar",session[:user_id])

But moreover because I can't send the value I need to the #before_save
and #after_create callbacks in Sequel. Or at least I don't think I
can. Perhaps I could do something like the following?
  class Bug < Sequel::Model
    attr_accessor :last_touched_by
    def before_save
      ...
      BugLogEntry << { ... user_id: @last_touched_by }
    end
  end
  ...
  my_bug.last_touched_by = session[:user_id]
  my_bug.tag_names = "foo bar"
  my_bug.save

Is that thread-safe? I don't know enough to be sure.


> module Sinatra
>   module GlobalSession
>     attr_accessor :current_session
>     def self.registered(base)
>       base.before { base.current_session = session }
>       base.enable :lock, :session
>     end
>   end
>   register GlobalSession
> end

The above looks promising, based on the presence of the word "lock".
Is this safe in a multithreaded environment?


> Note that accessing your session from your models makes them unusable outside of a request context.

A good note, but not relevant to my particular case.

Thank you for your help!

-- 
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sinatrarb@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.