|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if os(macOS) |
| 16 | +import Darwin.C |
| 17 | +#elseif canImport(Glibc) |
| 18 | +import Glibc |
| 19 | +#elseif canImport(Musl) |
| 20 | +import Musl |
| 21 | +#elseif os(Windows) |
| 22 | +import ucrt |
| 23 | +#else |
| 24 | +#error("Unsupported platform") |
| 25 | +#endif |
| 26 | + |
| 27 | +/// A clock implementation based on Unix epoch time for AWS Lambda runtime operations. |
| 28 | +/// |
| 29 | +/// `LambdaClock` provides millisecond-precision timing based on the Unix epoch |
| 30 | +/// (January 1, 1970, 00:00:00 UTC). This clock is designed for Lambda runtime |
| 31 | +/// operations where precise wall-clock time is required. |
| 32 | +/// |
| 33 | +/// ## Usage |
| 34 | +/// |
| 35 | +/// ```swift |
| 36 | +/// let clock = LambdaClock() |
| 37 | +/// let now = clock.now |
| 38 | +/// let deadline = now.advanced(by: .seconds(30)) |
| 39 | +/// |
| 40 | +/// // Sleep until deadline |
| 41 | +/// try await clock.sleep(until: deadline) |
| 42 | +/// ``` |
| 43 | +/// |
| 44 | +/// ## Performance |
| 45 | +/// |
| 46 | +/// This clock uses `clock_gettime(CLOCK_REALTIME)` on Unix systems for |
| 47 | +/// high-precision wall-clock time measurement with millisecond resolution. |
| 48 | +/// |
| 49 | +/// ## TimeZone Handling |
| 50 | +/// |
| 51 | +/// The Lambda execution environment uses UTC as a timezone, |
| 52 | +/// `LambdaClock` operates in UTC and does not account for time zones. |
| 53 | +/// see: TZ in https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html |
| 54 | +public struct LambdaClock: Clock { |
| 55 | + public typealias Duration = Swift.Duration |
| 56 | + |
| 57 | + /// A moment in time represented as milliseconds since the Unix epoch. |
| 58 | + /// |
| 59 | + /// `Instant` represents a specific point in time as the number of milliseconds |
| 60 | + /// that have elapsed since January 1, 1970, 00:00:00 UTC (Unix epoch). |
| 61 | + /// |
| 62 | + /// ## Thread Safety |
| 63 | + /// |
| 64 | + /// `Instant` is a value type and is inherently thread-safe. |
| 65 | + public struct Instant: InstantProtocol { |
| 66 | + /// The number of milliseconds since the Unix epoch. |
| 67 | + let instant: Int64 |
| 68 | + |
| 69 | + public typealias Duration = Swift.Duration |
| 70 | + |
| 71 | + /// Creates a new instant by adding a duration to this instant. |
| 72 | + /// |
| 73 | + /// - Parameter duration: The duration to add to this instant. |
| 74 | + /// - Returns: A new instant advanced by the specified duration. |
| 75 | + /// |
| 76 | + /// ## Example |
| 77 | + /// |
| 78 | + /// ```swift |
| 79 | + /// let now = LambdaClock().now |
| 80 | + /// let future = now.advanced(by: .seconds(30)) |
| 81 | + /// ``` |
| 82 | + public func advanced(by duration: Duration) -> Instant { |
| 83 | + .init(millisecondsSinceEpoch: Int64(instant + Int64(duration / .milliseconds(1)))) |
| 84 | + } |
| 85 | + |
| 86 | + /// Calculates the duration between this instant and another instant. |
| 87 | + /// |
| 88 | + /// - Parameter other: The target instant to calculate duration to. |
| 89 | + /// - Returns: The duration from this instant to the other instant. |
| 90 | + /// Positive if `other` is in the future, negative if in the past. |
| 91 | + /// |
| 92 | + /// ## Example |
| 93 | + /// |
| 94 | + /// ```swift |
| 95 | + /// let start = LambdaClock().now |
| 96 | + /// // ... some work ... |
| 97 | + /// let end = LambdaClock().now |
| 98 | + /// let elapsed = start.duration(to: end) |
| 99 | + /// ``` |
| 100 | + public func duration(to other: Instant) -> Duration { |
| 101 | + .milliseconds(other.instant - self.instant) |
| 102 | + } |
| 103 | + |
| 104 | + /// Compares two instants for ordering. |
| 105 | + /// |
| 106 | + /// - Parameters: |
| 107 | + /// - lhs: The left-hand side instant. |
| 108 | + /// - rhs: The right-hand side instant. |
| 109 | + /// - Returns: `true` if `lhs` represents an earlier time than `rhs`. |
| 110 | + public static func < (lhs: Instant, rhs: Instant) -> Bool { |
| 111 | + lhs.instant < rhs.instant |
| 112 | + } |
| 113 | + |
| 114 | + /// Returns this instant as the number of milliseconds since the Unix epoch. |
| 115 | + /// - Returns: The number of milliseconds since the Unix epoch. |
| 116 | + public func millisecondsSinceEpoch() -> Int64 { |
| 117 | + self.instant |
| 118 | + } |
| 119 | + |
| 120 | + /// Creates an instant from milliseconds since the Unix epoch. |
| 121 | + /// - Parameter milliseconds: The number of milliseconds since the Unix epoch. |
| 122 | + public init(millisecondsSinceEpoch milliseconds: Int64) { |
| 123 | + self.instant = milliseconds |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /// The current instant according to this clock. |
| 128 | + /// |
| 129 | + /// This property returns the current wall-clock time as milliseconds |
| 130 | + /// since the Unix epoch. |
| 131 | + /// This method uses `clock_gettime(CLOCK_REALTIME)` to obtain high-precision |
| 132 | + /// wall-clock time. |
| 133 | + /// |
| 134 | + /// - Returns: An `Instant` representing the current time. |
| 135 | + public var now: Instant { |
| 136 | + var ts = timespec() |
| 137 | + clock_gettime(CLOCK_REALTIME, &ts) |
| 138 | + return .init(millisecondsSinceEpoch: Int64(ts.tv_sec) * 1000 + Int64(ts.tv_nsec) / 1_000_000) |
| 139 | + } |
| 140 | + |
| 141 | + /// The minimum resolution of this clock. |
| 142 | + /// |
| 143 | + /// `LambdaClock` provides millisecond resolution. |
| 144 | + public var minimumResolution: Duration { |
| 145 | + .milliseconds(1) |
| 146 | + } |
| 147 | + |
| 148 | + /// Suspends the current task until the specified deadline. |
| 149 | + /// |
| 150 | + /// - Parameters: |
| 151 | + /// - deadline: The instant until which to sleep. |
| 152 | + /// - tolerance: The allowed tolerance for the sleep duration. Currently unused. |
| 153 | + /// |
| 154 | + /// - Throws: `CancellationError` if the task is cancelled during sleep. |
| 155 | + /// |
| 156 | + /// ## Example |
| 157 | + /// |
| 158 | + /// ```swift |
| 159 | + /// let clock = LambdaClock() |
| 160 | + /// let deadline = clock.now.advanced(by: .seconds(5)) |
| 161 | + /// try await clock.sleep(until: deadline) |
| 162 | + /// ``` |
| 163 | + public func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws { |
| 164 | + let now = self.now |
| 165 | + let sleepDuration = now.duration(to: deadline) |
| 166 | + if sleepDuration > .zero { |
| 167 | + try await ContinuousClock().sleep(for: sleepDuration) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /// Hardcoded maximum execution time for a Lambda function. |
| 172 | + public static var maxLambdaExecutionTime: Duration { |
| 173 | + // 15 minutes in milliseconds |
| 174 | + // see https://docs.aws.amazon.com/lambda/latest/dg/configuration-timeout.html |
| 175 | + .milliseconds(15 * 60 * 1000) |
| 176 | + } |
| 177 | + |
| 178 | + /// Returns the maximum deadline for a Lambda function execution. |
| 179 | + /// This is the current time plus the maximum execution time. |
| 180 | + /// This function is only used by the local server for testing purposes. |
| 181 | + public static var maxLambdaDeadline: Instant { |
| 182 | + LambdaClock().now.advanced(by: maxLambdaExecutionTime) |
| 183 | + } |
| 184 | +} |
0 commit comments