Worker Thread

Overview

IIS maintains a pool of threads to handle incoming HTTP requests. When all threads are in use, new requests will be rejected, which is, in general, good behavior. However, if all the threads are blocked in a wait state, perhaps querying a remote database or another DLL, IIS may reject incoming requests even though there is sufficient of CPU time to handle the new requests.

This sample is an implementation of a simple solution to this problem, the worker thread. Worker threads can accomplish a wide variety of tasks, but this worker thread implementation specifically offloads processing of certain types of special requests, such as database access, so that the primary IIS threads can remain available for HTTP requests.

Code Tour

When HttpExtensionProc is called by IIS, the extension creates a new thread with the Windows API CreateThread function and passes the extension's own ECB pointer to the new thread. In addition, the address of the thread's function, WorkerFunction in this example, is passed as a parameter as well. The primary IIS thread, executing HttpExtensionProc, then returns the status code HSE_STATUS_PENDING, which informs the server that there is still processing going on, and that the extension will notify the server when it is complete. The main server thread can then return back to the pool made available for new requests.

Meanwhile, the worker thread, immediately after CreateThread successfully returns, begins executing WorkerFunction. For the purposes of this example, this function simulates a computationally-intensive data access operation by causing the thread to sleep for 5 seconds. After the thread completes its simulated task, it then sends the server request HSE_REQ_DONE_WITH_SESSION, using the ServerSupportFunction method, to inform the server that all extended processing is now complete. The worker function then returns, which implicitly calls the Windows API ExitThread function and terminates the thread.

Location

This project is available in the ...isapi\extensions\WorkerThread subdirectory of the IIS samples directory.