website/docs/transport.md

2.4 KiB

Handy-Http Transport

DDoc Reference

Using Primitives, you can easily define your own request handler, but how does it actually get invoked when a client sends a request? That's where the Transport component comes in. It provides the HTTP server implementation that listens for requests, turns those into a ServerHttpRequest and ServerHttpResponse, and ultimately passes those onto its configured request handler.

Interface

All HTTP transport implementations extend from the top-level HttpTransport interface, which is defined simply as

interface HttpTransport {
    void start();
    void stop();
}

A helper method is also provided, startInNewThread, that will take a given HttpTransport, spawn a new thread, and call the transport's start() method in that thread, to allow you to keep working in the main thread.

Usage

Practically speaking, all production web applications should be served behind some well-trusted reverse proxy like Nginx which handles SSL, so that you only need to use HTTP/1.1 for your own code. To that end, Handy-Http's Transport component provides the TaskPoolHttp1Transport.

Task Pool HTTP-1 Transport

This transport implementation uses D's standard-library TaskPool to achieve parallel request processing. When a request is received on the main listening thread, a task is created and passed to the task pool so that a worker thread handles parsing the request, invoking the transport's configured request handler, and ultimately flushing the response back to the client.

The TaskPoolHttp1Transport takes the following as input via an Http1TransportConfig struct in addition to the request handler:

  • A host to bind to. Defaults to "127.0.0.1".
  • A port to bind to. Defaults to 8080.
  • The number of workers to initialize the task pool with. Defaults to 5.