Files
Cory Benfield 3b57e00556 Support selecting a specific local address. (#899)
Motivation

In machines with more complex network topologies it is possible for us
to have multiple possible NICs we might want to use for a request. Users
may wish to vary this on a per-request or even a per-client basis.

This control can typically be expressed by offering a local address to
bind to before making the connection attempt.

Modifications

Allow users to express a preferred local address at request or client
scope.
Make this part of the connection pool key.
Bind the local address when specified.
Test all of this.

Results

More capable clients.
2026-03-23 14:48:45 +00:00

51 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
struct RequestOptions {
/// The maximal `TimeAmount` that is allowed to pass between `channelRead`s from the Channel.
var idleReadTimeout: TimeAmount?
/// The maximal `TimeAmount` that is allowed to pass between `write`s into the Channel.
var idleWriteTimeout: TimeAmount?
/// DNS overrides.
var dnsOverride: [String: String]
/// The local IP address to bind outgoing connections to. This is typically used on multi-NIC
/// systems where we want to control where traffic goes.
var localAddress: String?
init(
idleReadTimeout: TimeAmount?,
idleWriteTimeout: TimeAmount?,
dnsOverride: [String: String],
localAddress: String? = nil
) {
self.idleReadTimeout = idleReadTimeout
self.idleWriteTimeout = idleWriteTimeout
self.dnsOverride = dnsOverride
self.localAddress = localAddress
}
}
extension RequestOptions {
static func fromClientConfiguration(_ configuration: HTTPClient.Configuration) -> Self {
RequestOptions(
idleReadTimeout: configuration.timeout.read,
idleWriteTimeout: configuration.timeout.write,
dnsOverride: configuration.dnsOverride,
localAddress: configuration.localAddress
)
}
}