Significance of bool IsReusable in http handler interface

24,452

Solution 1

The normal entry point for a handler is the ProcessRequest method. However you may have code in the class constructor which puts together some instance values which are expensive to build.

If you specify Reusable to be true the application can cache the instance and reuse it in another request by simply calling its ProcessRequest method again and again, without having to reconstruct it each time.

The application will instantiate as many of these handlers as are need to handle the current load.

The downside is that if the number of instances needed is larger than the instances currently present, they cause more memory to be used. Conversely they can also reduce apparent memory uses since their instance value will survive GC cycles and do not need to be frequently re-allocated.

Another caveat is you need to be sure that at the end of the ProcessRequest execution the object state is as you would want for another request to reuse the object.

Solution 2

Further to AnthonyWJones's answer, if your HTTP handler returns true for IsReusable then you should ensure that it is fully thread-safe.

There's nothing in the documentation to indicate that reusable handlers can't be reused concurrently, although the current Microsoft implementations only appear to reuse them consecutively. But, at least in theory, a single handler instance could be reused simultaneously by multiple requests, so you shouldn't rely on any data which might be modified by other concurrent threads.

Solution 3

If you don't store any state in that instance (i.e.: you don't have any fields (aka "class variables")) then you should be safe reusing it.

It's by default false to be on the safe side.

Share:
24,452
GurdeepS
Author by

GurdeepS

Updated on September 18, 2020

Comments

  • GurdeepS
    GurdeepS almost 4 years

    When writing a http handler/module, there is an interface member to implement called - bool IsReusable.

    What is the significance of this member? If I set it to false (or true), what does this mean for the rest of the web app?

  • zod
    zod about 13 years
    Sorry for being thick, but could somebody please elaborate on what is meant by “context switch”. If you access things from the sesson or query string (content.Request.QueryString) is that reusable or not?
  • Larry Dukek
    Larry Dukek over 12 years
    Context switching is when a CPU stops processing on one thread and starts processing on another. I.E. the CPU switched it's context from one thread to another. This happens constantly in PCs, it gave us the illusion of multitasking before there were multicore computers.
  • Admin
    Admin about 11 years
    I could not understand when u said There could be a context switch at any time. When we type url and press enter, this executes one request at a time. right ?
  • Ishmael Smyrnow
    Ishmael Smyrnow about 11 years
    The context switching will be an issue when multiple users are using an application at the same time. For example, if two users want to update the same record at the same time, the context switch could cause problems.
  • Frédéric
    Frédéric almost 10 years
    This statement about IsReusable requiring thread safety seems in contradiction with AnthonyWJones response. As I understand its third paragraph (The application will instantiate as many of...), a reusable handler instance will not be reused concurrently, but only after having finish its current processing. If this is true, then there is no need to be thread safe.
  • Ian
    Ian over 8 years
    @Frederic - You are right in the sense that the class itself need not be thread-safe since there is 1 thread per instance (assuming that's what AWJ means by "The application will instantiate as many of these handlers as are needed to handle the current load"). However, I think Luke just meant that the shared resources accessed from within the ProcessRequest execution need to be thread-safe; like if you are reading and writing to static/global variables. I.E. - if a thread checks a DB field at the start of Process Request, don't assume it won't be changed before the thread exits the handler.
  • Ian
    Ian over 8 years
    You mention object reuse as being the key optimization achieved by specifying IsReusable=True. Will setting IsReusable=False result in the server not instantiating multiple instances of the handler ever. I.E. - does it eliminate concurrent requests?
  • Frédéric
    Frédéric over 8 years
    @Ian, whatever the value of IsReusable, shared resources must be handled in a thread safe way. So even if you guess right about what he wanted to mean, it looks wrong anyway.
  • LukeH
    LukeH over 8 years
    @Frederic, @Ian: The documentation for IHttpHandler and IHttpHandlerFactory is extremely light on detail. @Frederic is correct that the default Microsoft implementations only appear to reuse handlers consecutively, not concurrently [...]
  • LukeH
    LukeH over 8 years
    [...] but it's not difficult to imagine (or write) a IHttpHandlerFactory implementation that always dishes out the same singleton handler instance if IsReusable is true. Such a factory wouldn't be breaking the interface contract, as far as I can tell, and in that situation the handler could definitely be reused concurrently and so would need to be fully thread-safe.
  • Frédéric
    Frédéric over 8 years
    You're right for IHttpHandler, looking at the documentation, nothing guarantees it will not be used concurrently (interface doc and IsReusable property doc). And relying on undocumented features is unsafe. On IHttpHandlerFactory side, ReleaseHandler method suggests that this undocumented feature is the only usage pattern 'forecast' by those interfaces designers. This remains quite a weak hint.
  • Ian
    Ian over 8 years
    I eventually read this question (stackoverflow.com/questions/5500950/…). The accepted answer by Mr. Branislov implies that multiple threads concurrently executing the same instance of the handler is possible. So I'll just code assuming that. I'm guessing MS makes the documentation vague so they can change their implementation later.
  • DavidScherer
    DavidScherer over 5 years
    @Ian I believe it then functions as a normal page when reusable is false, though I may be wrong. When it's reusable it may be able to handle 6 concurrent requests with 2 or 3 instances (example only) whereas when it is not reusable it would need 1:1 instances/processes. I can't imagine .Net would let requests pile up as 1 worker thread tried to handle it all while continuously refreshing/reinitialising its state.