Module core

Source
Expand description

Core HTTP service implementation for handling downstream client connections.

This module provides a high-performance, asynchronous HTTP service that handles connections from downstream clients. It supports HTTP/1, HTTP/1.1, and HTTP/2 protocols, and is designed to work with monoio’s asynchronous runtime, providing fine-grained control over various timeouts.

§Key Components

  • HttpCoreService: The main service component responsible for handling HTTP connections from downstream clients. It can be composed of a stack of handlers implementing the HttpHandler trait.
  • HttpServerTimeout: Configuration for various timeout settings in the HTTP server.

§Features

  • Support for HTTP/1, HTTP/1.1, and HTTP/2 protocols
  • Composable design allowing a stack of HttpHandler implementations
  • Automatic protocol detection when combined with H2Detect
  • Efficient handling of concurrent requests using asynchronous I/O
  • Configurable timeout settings for different stages of request processing
  • Integration with service_async for easy composition in service stacks
  • Automatic response encoding and error handling

§Usage

HttpCoreService is typically used as part of a larger service stack, often in combination with H2Detect for automatic protocol detection. Here’s a basic example:

use service_async::{layer::FactoryLayer, stack::FactoryStack};

use crate::http::{HttpCoreService, H2Detect};

let config = Config { /* ... */ };
let stack = FactoryStack::new(config)
    .push(HttpCoreService::layer())
    .push(H2Detect::layer())
    // ... other handlers implementing HttpHandler ...
    ;

let service = stack.make_async().await.unwrap();
// Use the service to handle incoming HTTP connections from downstream clients

§Handler Composition

HttpCoreService can be composed of multiple handlers implementing the HttpHandler trait. This allows for a flexible and modular approach to request processing. Handlers can be chained together to form a processing pipeline, each handling a specific aspect of the HTTP request/response cycle.

§Automatic Protocol Detection

When used in conjunction with H2Detect, HttpCoreService can automatically detect whether an incoming connection is using HTTP/1, HTTP/1.1, or HTTP/2, and handle it appropriately. This allows for seamless support of multiple HTTP versions without the need for separate server configurations.

§Performance Considerations

  • Uses monoio’s efficient async I/O operations for improved performance
  • Implements connection keep-alive for HTTP/1.1 to reduce connection overhead
  • Supports HTTP/2 multiplexing for efficient handling of concurrent requests
  • Automatic protocol detection allows for optimized handling based on the client’s capabilities

Structs§

HttpCoreService
Core HTTP service handler supporting both HTTP/1.1 and HTTP/2 protocols.
HttpServerTimeout
Represents the timeout settings for the HTTP server.