NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_writer(n) 4.99.30 naviserver "NaviServer Built-in Commands"

Name

ns_writer - Manage files with writer thread

Table Of Contents

Synopsis

Description

This command allows one to submit data or file to be returned to the client connection using writer thread instead of current connection thread. Writer thread processes multiple sockets using async socket operations which consumes less resources than returning multiple files to the slow clients in separate thread.

COMMANDS

ns_writer list ?-server s?

Returns list of all currently submitted files. Every list entry contains the following elements:

  1. start time of the initiating request,

  2. name of the thread,

  3. driver,

  4. pool,

  5. IP address of the client,

  6. file descriptor,

  7. remaining size,

  8. bytes already sent,

  9. current transfer rate

  10. rate limit, and

  11. the client data as provided via [ns_conn clientdata].

If -server is specified, only the submitted file entries from the specified server are returned. When NaviServer is running in reverse proxy mode, the client IP address is taken form the value as provided by the reverse proxy server.

ns_writer submit data

Submit arbitrary data to be returned via writer thread, data can be binary. On success the function returns 1, otherwise 0.

ns_writer submitfile ?-headers? ?-offset offset? ?-size size? filename

Submits file to be returned via writer thread. For partial requests, ns_writer does NOT change status code or sets content-range. On success the function returns 1, otherwise 0. Optionally the following arguments can be used:

-headers Tells the writer to provide required HTTP headers, like content size, type and status. When this option is not used, make sure that the headers are provided (e.g. via ns_headers) from your application.

-offset If specified, file will be returned starting with this offset. The value can be specified in memory units (kB, MB, GB, KiB, MiB, GiB).

-size If not specified, the whole file will be returned, otherwise only given part. Can be combined with -offset. The value can be specified in memory units (kB, MB, GB, KiB, MiB, GiB).

ns_writer submitfiles ?-headers? filespecs

Submits multiple files to be submitted in a single chunk. This call is quite similar to ns_writer submitfile, butit accepts a list of dicts as argument. Every dict element has to contain a filename element and optionally an -offset element and/or a -size element.

This function is e.g. useful for video streaming applications, where multiple video file chunks should be transferred (sometimes) concatenated in a single chunk. Therefore, this function avoids the necessity to concatenate these files in advance (causing increased latency and disk usage).

 ns_writer submitfiles {{filename /tmp/f1} {filename /tmp/f2 -offset 10}}
ns_writer size ?-driver driver? ?size?

Query or set configuration parameter writersize of the writer(s) of the current or specified driver. When -driver is not specified the driver of the current connection thread is used. When the size is not provided, the actual setting is returned. When the size is provided, it can be specified in memory units (kB, MB, GB, KiB, MiB, GiB).

ns_writer streaming ?-driver driver? ?value?

Query or set configuration parameter writerstreaming of the writer(s) of the current or specified driver. When -driver is not specified the driver of the current connection thread is used. When the value is specified, it has to be of the form of a valid Boolean value.

CONFIGURATION

Basic configuration options:

 ns_section "ns/server/default/module/nssock" {
    ns_param writerthreads 1
    ns_param writersize 4kB
    ns_param writerstreaming true
 }

writerthreads parameter specified how many writer threads will be used, multiple threads will be rotated to spread the load across all threads

writersize specifies the minimal size of the file to be considered for writer thread, this parameter is used by connection thread when returning file and if size exceeds configured value, file will be returned via writer thread and connection thread will be released to handle other requests.

writerstreaming specifies whether or not streaming HTML output (e.g. sent via ns_write) should be sent via the writer. When sending server responses over slow connections, this option reduces the blocking time of the connection threads and can therefore reduce the damage of slow-read attacks.

EXAMPLES

 ns_writer submitfile /tmp/biglist.txt

See Also

ns_conn, ns_headers, ns_returnfile, ns_write

Keywords

configuration, reverseproxy, server built-in, writer