diff --git a/Source/AwsCommonRuntimeKit/http/HTTP2StreamManager.swift b/Source/AwsCommonRuntimeKit/http/HTTP2StreamManager.swift index e8af6e168..ebc8f81a8 100644 --- a/Source/AwsCommonRuntimeKit/http/HTTP2StreamManager.swift +++ b/Source/AwsCommonRuntimeKit/http/HTTP2StreamManager.swift @@ -42,6 +42,21 @@ public class HTTP2StreamManager { }) } + /// Fetch the current manager metrics from connection manager. + public func fetchMetrics() throws -> HTTPClientConnectionManagerMetrics { + do { + var cManagerMetrics = aws_http_manager_metrics() + aws_http2_stream_manager_fetch_metrics(rawValue, &cManagerMetrics) + return HTTPClientConnectionManagerMetrics( + availableConcurrency: cManagerMetrics.available_concurrency, + pendingConcurrencyAcquires: cManagerMetrics.pending_concurrency_acquires, + leasedConcurrency: cManagerMetrics.leased_concurrency + ) + } catch { + throw CommonRunTimeError.crtError(.makeFromLastError()) + } + } + deinit { aws_http2_stream_manager_release(rawValue) } diff --git a/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManager.swift b/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManager.swift index 3eb8e243d..1462db899 100644 --- a/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManager.swift +++ b/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManager.swift @@ -39,6 +39,21 @@ public class HTTPClientConnectionManager { } } + /// Fetch the current manager metrics from connection manager. + public func fetchMetrics() throws -> HTTPClientConnectionManagerMetrics { + do { + var cManagerMetrics = aws_http_manager_metrics() + aws_http_connection_manager_fetch_metrics(rawValue, &cManagerMetrics) + return HTTPClientConnectionManagerMetrics( + availableConcurrency: cManagerMetrics.available_concurrency, + pendingConcurrencyAcquires: cManagerMetrics.pending_concurrency_acquires, + leasedConcurrency: cManagerMetrics.leased_concurrency + ) + } catch { + throw CommonRunTimeError.crtError(.makeFromLastError()) + } + } + deinit { aws_http_connection_manager_release(rawValue) } diff --git a/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManagerMetrics.swift b/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManagerMetrics.swift new file mode 100644 index 000000000..38760c89a --- /dev/null +++ b/Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManagerMetrics.swift @@ -0,0 +1,26 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct HTTPClientConnectionManagerMetrics { + /// The number of additional concurrent requests that can be supported by the HTTP manager without needing to + /// establish additional connections to the target server. + /// + /// For connection manager, it equals to connections that's idle. + /// For stream manager, it equals to the number of streams that are possible to be made without creating new + /// connection, although the implementation can create new connection without fully filling it. + public var availableConcurrency: Int + /// The number of requests that are awaiting concurrency to be made available from the HTTP manager. + public var pendingConcurrencyAcquires: Int + /// The number of connections (http/1.1) or streams (for h2 via. stream manager) currently vended to user. + public var leasedConcurrency: Int + + public init( + availableConcurrency: Int = 0, + pendingConcurrencyAcquires: Int = 0, + leasedConcurrency: Int = 0 + ) { + self.availableConcurrency = availableConcurrency + self.pendingConcurrencyAcquires = pendingConcurrencyAcquires + self.leasedConcurrency = leasedConcurrency + } +}