Skip to main content
ConnectInfo<T> is an extractor that provides access to connection information from the underlying transport.

Basic usage

To use ConnectInfo, your application must be served using into_make_service_with_connect_info:
If you try to use ConnectInfo without into_make_service_with_connect_info, the extractor will fail at runtime with a missing extension error.

Custom connection info

You can provide custom connection information by implementing the Connected trait:

Testing with MockConnectInfo

For testing, use MockConnectInfo to avoid needing a real network connection:
If both MockConnectInfo and into_make_service_with_connect_info are used, the real connection info takes precedence.

Type signature

type parameter
The type of connection information to extract. Must implement Clone + Send + Sync + 'static.

Rejection

Rejects with MissingExtension if:
  • The application wasn’t started with into_make_service_with_connect_info
  • MockConnectInfo middleware wasn’t added

Common use cases

  • Rate limiting by IP: Track request rates per client IP address
  • Geolocation: Determine user location from IP for content personalization
  • Security logging: Record source IP for audit trails
  • Access control: Restrict endpoints based on client IP ranges

See also

  • State - Recommended way to share application state
  • Extension - For other request extensions