You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by David Idol <da...@gmail.com> on 2012/11/26 21:11:26 UTC

Subjects with NIO (single thread - multiple Subjects)

I would like to use Shiro with an asynchronous, NIO-based server (JBoss Netty). Because this server is based on NIO, it does not use the single thread per request model (one selecting thread can be used to service multiple requests, or "Channels" in Netty terms). As such, we cannot assume that any thread is servicing only a single client.

I would like to associate a Shiro "Subject" with each Netty "Channel"; does anyone have any advice on how to do this successfully? The best thing I have been able to come up with (although I have not tested it) is building Subject instances manually whenever a new Channel is initiated and then looking up the Subject in whatever thread is executing via the sessionId (which gets sent initially to the client and then received by the server with every request).

The application is not a web application (uses persistent socket connections instead of HTTP) if that matters.

Thanks,
David



Re: Subjects with NIO (single thread - multiple Subjects)

Posted by David Idol <da...@gmail.com>.
Hi Menelaos, 

My understanding is that most web servers using HTTP keep-alive will dedicate only one thread per request, unless the application developer explicitly creates new worker threads to delegate to (and Shiro has a facility for this scenario). Once the socket connection terminates and a new request is made then obviously there is no guarantee that the same thread will be used (likely not), but in the case of Shiro as long as the session information is sent with each new request then whatever Subject instance is returned does not matter because it will be logged in as that user anyway.

My use case does not involve HTTP or a web container, so it is irrelevant to me, but still interesting to understand how it works.

I believe that the best bet for my situation (multiple Subjects on one thread - bound to the Netty Channel instead of a ThreadLocal) is to build my own data structure and use it to hold onto Subject instances for the course of each Netty Channel lifetime (which has its own lifecycle observer interface). This would closely mimic the behavior of the default implementation, only for an NIO-based server instead of a thread pool based one, at least as I understand it. I will post additional updates as I continue to experiment with this approach in my free time.

Best,
David

On Wednesday, November 28, 2012 at 6:12 AM, Menelaos Perdikeas wrote:

> Actually I was looking at the implementation of ThreadContext#getSubject today (which is what SecurityUtils#getSubject 
> calls).
> 
> It seems that ultimately getting the right subject is hinged on having a separate thread for each executing user. Which also begs another question: Even setting NIO aside, how does Shiro find the right subject in the course of a long user interaction with a web application? E.g. user logins on view A.xhtml and then clicks on view B.xhtml and continues navigating inside the application. Is there any guarantee that the web container will use the same Thread for all subsequent requests ? Otherwise how is Shiro able to consistently return the same subject in the course of a user's interaction with the application ?
> 
> As regards to your proposed approach of building the Subjects yourself via sessionId, I suppose like this:
> 
> Subject subject = (new Subject.Builder()).sessionId(sessionId).buildSubject();
> 
> From what I understand, the above is only possible when using Shiro native sessions (not container-managed).
> 
> KR,
> Menelaos.
> 
> On Mon, Nov 26, 2012 at 10:11 PM, David Idol <david.idol@gmail.com (mailto:david.idol@gmail.com)> wrote:
> > I would like to use Shiro with an asynchronous, NIO-based server (JBoss Netty). Because this server is based on NIO, it does not use the single thread per request model (one selecting thread can be used to service multiple requests, or "Channels" in Netty terms). As such, we cannot assume that any thread is servicing only a single client. 
> > 
> > I would like to associate a Shiro "Subject" with each Netty "Channel"; does anyone have any advice on how to do this successfully? The best thing I have been able to come up with (although I have not tested it) is building Subject instances manually whenever a new Channel is initiated and then looking up the Subject in whatever thread is executing via the sessionId (which gets sent initially to the client and then received by the server with every request). 
> > 
> > The application is not a web application (uses persistent socket connections instead of HTTP) if that matters.
> > 
> > Thanks,
> > David
> > 
> > 
> 
> 
> 


Re: Subjects with NIO (single thread - multiple Subjects)

Posted by Menelaos Perdikeas <mp...@gmail.com>.
Actually I was looking at the implementation of ThreadContext#getSubject
today (which is what SecurityUtils#getSubject
calls).

It seems that ultimately getting the right subject is hinged on having a
separate thread for each executing user. Which also begs another question:
Even setting NIO aside, how does Shiro find the right subject in the course
of a long user interaction with a web application? E.g. user logins on view
A.xhtml and then clicks on view B.xhtml and continues navigating inside the
application. Is there any guarantee that the web container will use the
same Thread for all subsequent requests ? Otherwise how is Shiro able to
consistently return the same subject in the course of a user's interaction
with the application ?

As regards to your proposed approach of building the Subjects yourself via
sessionId, I suppose like this:

Subject subject = (new
Subject.Builder()).sessionId(sessionId).buildSubject();

>From what I understand, the above is only possible when using Shiro native
sessions (not container-managed).

KR,
Menelaos.

On Mon, Nov 26, 2012 at 10:11 PM, David Idol <da...@gmail.com> wrote:

>  I would like to use Shiro with an asynchronous, NIO-based server (JBoss
> Netty). Because this server is based on NIO, it does not use the single
> thread per request model (one selecting thread can be used to service
> multiple requests, or "Channels" in Netty terms). As such, we cannot assume
> that any thread is servicing only a single client.
>
> I would like to associate a Shiro "Subject" with each Netty "Channel";
> does anyone have any advice on how to do this successfully? The best thing
> I have been able to come up with (although I have not tested it) is
> building Subject instances manually whenever a new Channel is initiated and
> then looking up the Subject in whatever thread is executing via the
> sessionId (which gets sent initially to the client and then received by the
> server with every request).
>
> The application is not a web application (uses persistent socket
> connections instead of HTTP) if that matters.
>
> Thanks,
> David
>

Re: Subjects with NIO (single thread - multiple Subjects)

Posted by Charlie Qiu <qc...@gmail.com>.
Hi David,
You may want to extend the netty Channelgroups class. For channels related
to the same Subject, you can group them in your own CHannelGroup. This will
give you better control when you want to logout(). You can send a future to
all channels.You can create a static HashMap<SubjectProperty,
YourChannelGroup> to maintain your channelgroups.

2012/11/27 David Idol <da...@gmail.com>

>  I saw this response by Les, but it seems to focus on using one Subject
> for multiple threads. Netty has a thread-pool based implementation, but I'd
> like the performance gains of having the asynchronous NIO-based socket
> reads, considering I may have many, many clients connected at once. I was
> hoping someone out there would have encountered this kind of thing before
> and I could learn from their knowledge.
>
>  On Monday, November 26, 2012 at 4:45 PM, Cemo wrote:
>
> I am also interested in this topic. This is not exact answer of your
> question but seems that there is not any work for non single thread models.
> You can check details from here:
>
>
> http://mail-archives.apache.org/mod_mbox/shiro-user/201211.mbox/%3CCAAtvD4WLA_Gu_1qOFJBLoo7UbYOLmSSA2yRxCH-5Tq-yZut0ZA@mail.gmail.com%3E
>
>
>
>
> On 26 November 2012 22:11, David Idol <da...@gmail.com> wrote:
>
> I would like to use Shiro with an asynchronous, NIO-based server (JBoss
> Netty). Because this server is based on NIO, it does not use the single
> thread per request model (one selecting thread can be used to service
> multiple requests, or "Channels" in Netty terms). As such, we cannot assume
> that any thread is servicing only a single client.
>
> I would like to associate a Shiro "Subject" with each Netty "Channel";
> does anyone have any advice on how to do this successfully? The best thing
> I have been able to come up with (although I have not tested it) is
> building Subject instances manually whenever a new Channel is initiated and
> then looking up the Subject in whatever thread is executing via the
> sessionId (which gets sent initially to the client and then received by the
> server with every request).
>
> The application is not a web application (uses persistent socket
> connections instead of HTTP) if that matters.
>
> Thanks,
> David
>
>
>
>

Re: Subjects with NIO (single thread - multiple Subjects)

Posted by David Idol <da...@gmail.com>.
I saw this response by Les, but it seems to focus on using one Subject for multiple threads. Netty has a thread-pool based implementation, but I'd like the performance gains of having the asynchronous NIO-based socket reads, considering I may have many, many clients connected at once. I was hoping someone out there would have encountered this kind of thing before and I could learn from their knowledge.  


On Monday, November 26, 2012 at 4:45 PM, Cemo wrote:

> I am also interested in this topic. This is not exact answer of your question but seems that there is not any work for non single thread models. You can check details from here: 
> 
> http://mail-archives.apache.org/mod_mbox/shiro-user/201211.mbox/%3CCAAtvD4WLA_Gu_1qOFJBLoo7UbYOLmSSA2yRxCH-5Tq-yZut0ZA@mail.gmail.com%3E 
> 
> 
> 
> 
> On 26 November 2012 22:11, David Idol <david.idol@gmail.com (mailto:david.idol@gmail.com)> wrote:
> > I would like to use Shiro with an asynchronous, NIO-based server (JBoss Netty). Because this server is based on NIO, it does not use the single thread per request model (one selecting thread can be used to service multiple requests, or "Channels" in Netty terms). As such, we cannot assume that any thread is servicing only a single client. 
> > 
> > I would like to associate a Shiro "Subject" with each Netty "Channel"; does anyone have any advice on how to do this successfully? The best thing I have been able to come up with (although I have not tested it) is building Subject instances manually whenever a new Channel is initiated and then looking up the Subject in whatever thread is executing via the sessionId (which gets sent initially to the client and then received by the server with every request). 
> > 
> > The application is not a web application (uses persistent socket connections instead of HTTP) if that matters.
> > 
> > Thanks,
> > David
> > 
> 
> 
> 


Re: Subjects with NIO (single thread - multiple Subjects)

Posted by Cemo <ce...@gmail.com>.
I am also interested in this topic. This is not exact answer of your
question but seems that there is not any work for non single thread models.
You can check details from here:

http://mail-archives.apache.org/mod_mbox/shiro-user/201211.mbox/%3CCAAtvD4WLA_Gu_1qOFJBLoo7UbYOLmSSA2yRxCH-5Tq-yZut0ZA@mail.gmail.com%3E




On 26 November 2012 22:11, David Idol <da...@gmail.com> wrote:

> I would like to use Shiro with an asynchronous, NIO-based server (JBoss
> Netty). Because this server is based on NIO, it does not use the single
> thread per request model (one selecting thread can be used to service
> multiple requests, or "Channels" in Netty terms). As such, we cannot assume
> that any thread is servicing only a single client.
>
> I would like to associate a Shiro "Subject" with each Netty "Channel";
> does anyone have any advice on how to do this successfully? The best thing
> I have been able to come up with (although I have not tested it) is
> building Subject instances manually whenever a new Channel is initiated and
> then looking up the Subject in whatever thread is executing via the
> sessionId (which gets sent initially to the client and then received by the
> server with every request).
>
> The application is not a web application (uses persistent socket
> connections instead of HTTP) if that matters.
>
> Thanks,
> David
>