The library(http/thread_httpd.pl)
provides the
infrastructure to manage multiple clients using a pool of worker-threads.
This realises a popular server design, also seen in Java Tomcat and
Microsoft .NET. As a single persistent server process maintains
communication to all clients startup time is not an important issue and
the server can easily maintain state-information for all clients.
In addition to the functionality provided by the inetd server, the
threaded server can also be used to realise an HTTPS server exploiting
the library(ssl)
library. See option ssl(+SSLOptions)
below.
- http_server(:Goal,
+Options)
- Create the server. Options must provide the
port(?Port)
option to specify the port the server should listen to. If Port
is unbound an arbitrary free port is selected and Port is
unified to this port-number. The server consists of a small Prolog
thread accepting new connection on Port and dispatching these
to a pool of workers. Defined Options are:
- port(?Address)
- Address to bind to. Address is either a port (integer) or a
term
Host:Port. The port may be a variable, causing the
system to select a free port and unify the variable with the selected
port. See also tcp_bind/2.
- workers(+N)
- Defines the number of worker threads in the pool. Default is to use
five workers. Choosing the optimal value for best performance
is a difficult task depending on the number of CPUs in your system and
how much resources are required for processing a request. Too high
numbers makes your system switch too often between threads or even swap
if there is not enough memory to keep all threads in memory, while a too
low number causes clients to wait unnecessary for other clients to
complete. See also http_workers/2.
- timeout(+SecondsOrInfinite)
- Determines the maximum period of inactivity handling a request. If no
data arrives within the specified time since the last data arrived, the
connection raises an exception, and the worker discards the client and
returns to the pool-queue for a new client. If it is
infinite
,
a worker may wait forever on a client that doesn't complete its request.
Default is 60 seconds.
- keep_alive_timeout(+SecondsOrInfinite)
- Maximum time to wait for new activity on Keep-Alive
connections. Choosing the correct value for this parameter is hard.
Disabling Keep-Alive is bad for performance if the clients request
multiple documents for a single page. This may ---for example-- be
caused by HTML frames, HTML pages with images, associated CSS files,
etc. Keeping a connection open in the threaded model however prevents
the thread servicing the client servicing other clients. The default is
2 seconds.
- local(+KBytes)
- Size of the local-stack for the workers. Default is taken from the
commandline option.
- global(+KBytes)
- Size of the global-stack for the workers. Default is taken from the
commandline option.
- trail(+KBytes)
- Size of the trail-stack for the workers. Default is taken from the
commandline option.
- ssl(+SSLOptions)
- Use SSL (Secure Socket Layer) rather than plain TCP/IP. A server created
this way is accessed using the
https://
protocol. SSL
allows for encrypted communication to avoid others from tapping the wire
as well as improved authentication of client and server. The SSLOptions
option list is passed to ssl_context/3.
The port option of the main option list is forwarded to the SSL layer.
See the library(ssl)
library for details.
- http_server_property(?Port,
?Property)
- True if Property is a property of the HTTP server running at
Port. Defined properties are:
- goal(:Goal)
- Goal used to start the server. This is often http_dispatch/1.
- scheme(-Scheme)
- Scheme is one of
http
or https
.
- start_time(-Time)
- Time-stamp when the server was created. See format_time/3
for creating a human-readable representation.
- http_workers(+Port,
?Workers)
- Query or manipulate the number of workers of the server identified by
Port. If Workers is unbound it is unified with the
number of running servers. If it is an integer greater than the current
size of the worker pool new workers are created with the same
specification as the running workers. If the number is less than the
current size of the worker pool, this predicate inserts a number of‘quit’requests
in the queue, discarding the excess workers as they finish their jobs
(i.e. no worker is abandoned while serving a client).
This can be used to tune the number of workers for performance.
Another possible application is to reduce the pool to one worker to
facilitate easier debugging.
- http_add_worker(+Port,
+Options)
- Add a new worker to the HTTP server for port Port. Options
overrule the default queue options. The following additional options are
processed:
- max_idle_time(+Seconds)
- The created worker will automatically terminate if there is no new work
within Seconds.
- http_stop_server(+Port,
+Options)
- Stop the HTTP server at Port. Halting a server is done
gracefully, which means that requests being processed are not
abandoned. The Options list is for future refinements of this
predicate such as a forced immediate abort of the server, but is
currently ignored.
- http_current_worker(?Port,
?ThreadID)
- True if ThreadID is the identifier of a Prolog thread serving
Port. This predicate is motivated to allow for the use of
arbitrary interaction with the worker thread for development and
statistics.
- http_spawn(:Goal,
+Spec)
- Continue handling this request in a new thread running Goal.
After
http_spawn/2,
the worker returns to the pool to process new requests. In its simplest
form, Spec is the name of a thread pool as defined by
thread_pool_create/3.
Alternatively it is an option list, whose options are passed to thread_create_in_pool/4
if Spec contains
pool(Pool)
or to thread_create/3
of the pool option is not present. If the dispatch module is used (see section
3.2), spawning is normally specified as an option to the http_handler/3
registration.
We recomment the use of thread pools. They allow registration of a
set of threads using common characteristics, specify how many can be
active and what to do if all threads are active. A typical application
may define a small pool of threads with large stacks for computation
intensive tasks, and a large pool of threads with small stacks to serve
media. The declaration could be the one below, allowing for max 3
concurrent solvers and a maximum backlog of 5 and 30 tasks creating
image thumbnails.
:- use_module(library(thread_pool)).
:- thread_pool_create(compute, 3,
[ local(20000), global(100000), trail(50000),
backlog(5)
]).
:- thread_pool_create(media, 30,
[ local(100), global(100), trail(100),
backlog(100)
]).
:- http_handler('/solve', solve, [spawn(compute)]).
:- http_handler('/thumbnail', thumbnail, [spawn(media)]).