ConnectInfo<T> is an extractor that provides access to connection information from the underlying transport.
Basic usage
To useConnectInfo, 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 theConnected trait:
Testing with MockConnectInfo
For testing, useMockConnectInfo 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
The type of connection information to extract. Must implement
Clone + Send + Sync + 'static.Rejection
Rejects withMissingExtension if:
- The application wasn’t started with
into_make_service_with_connect_info MockConnectInfomiddleware 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