@logbrew/react-native 0.1.38 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LogBrewReactNative.podspec +1 -1
- package/README.md +17 -15
- package/android-native-diagnostics.d.ts +15 -5
- package/android-native-diagnostics.js +170 -94
- package/apollo.cjs +435 -226
- package/apollo.d.cts +2 -70
- package/apollo.d.ts +24 -12
- package/apollo.js +438 -221
- package/apple-native-diagnostics-config.js +176 -0
- package/apple-native-diagnostics-context.js +232 -0
- package/apple-native-diagnostics-module.js +229 -0
- package/apple-native-diagnostics.d.ts +42 -11
- package/apple-native-diagnostics.js +60 -389
- package/expo.cjs +29 -16
- package/expo.d.ts +10 -9
- package/expo.js +4 -3
- package/fatal-admission.cjs +113 -0
- package/fatal-record.cjs +317 -0
- package/fatal-replay.cjs +297 -508
- package/global-errors-capture.cjs +369 -0
- package/global-errors-event.cjs +174 -0
- package/global-errors-support.cjs +128 -0
- package/global-errors.cjs +75 -351
- package/global-errors.d.cts +29 -278
- package/global-errors.d.ts +57 -27
- package/global-errors.js +7 -2
- package/global-errors.native.js +83 -48
- package/index-breadcrumbs.cjs +139 -0
- package/index-errors.cjs +167 -0
- package/index-events.cjs +365 -0
- package/index-navigation.cjs +327 -0
- package/index-random.cjs +120 -0
- package/index-react.cjs +189 -0
- package/index-spans.cjs +190 -0
- package/index-trace-fetch.cjs +322 -0
- package/index-trace.cjs +335 -0
- package/index-transport.cjs +201 -0
- package/index-values.cjs +232 -0
- package/index.cjs +95 -1220
- package/index.d.cts +2 -505
- package/index.d.ts +130 -469
- package/index.native.d.ts +146 -22
- package/index.native.js +184 -159
- package/instrumentation-fetch.cjs +113 -0
- package/instrumentation-install.cjs +241 -0
- package/instrumentation-runtime.cjs +65 -0
- package/instrumentation-teardown.cjs +93 -0
- package/instrumentation-values.cjs +282 -0
- package/instrumentation-xhr-request.cjs +298 -0
- package/instrumentation-xhr.cjs +233 -0
- package/instrumentation.cjs +110 -599
- package/instrumentation.d.cts +2 -84
- package/instrumentation.d.ts +22 -8
- package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngine.swift +7 -1
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngineAutomatic.swift +36 -4
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngineQueue.swift +17 -0
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryLifecycle.swift +2 -0
- package/ios/GeneratedAppleDiagnostics/LogBrew/Transport.swift +41 -6
- package/ios/GeneratedAppleDiagnostics/SOURCE-MANIFEST.json +5 -5
- package/lifecycle.cjs +181 -78
- package/lifecycle.d.cts +1 -50
- package/lifecycle.d.ts +17 -6
- package/metadata.cjs +279 -114
- package/metro-debug-id.cjs +296 -0
- package/metro-serializer.cjs +219 -0
- package/metro.cjs +153 -297
- package/metro.d.cts +2 -57
- package/metro.d.ts +27 -13
- package/metro.js +2 -3
- package/native-bridge-async.cjs +165 -0
- package/native-bridge-scope.cjs +148 -0
- package/native-bridge.cjs +83 -186
- package/native-bridge.d.cts +2 -94
- package/native-bridge.d.ts +28 -15
- package/native-client-bindings.js +230 -0
- package/native-diagnostics-values.js +120 -0
- package/native-module-lookup.js +31 -0
- package/native-null.js +3 -0
- package/null-value.cjs +4 -0
- package/package.json +44 -2
- package/persistent-delivery-store.js +247 -0
- package/persistent-delivery.native.js +183 -261
- package/promise-rejection-handlers.cjs +368 -0
- package/promise-rejections.cjs +169 -361
- package/react-native.config.js +3 -1
- package/release-artifacts-files.cjs +94 -0
- package/release-artifacts-options.cjs +182 -0
- package/release-artifacts.cjs +278 -207
- package/release-artifacts.d.cts +2 -55
- package/release-artifacts.d.ts +18 -7
- package/release-artifacts.js +2 -4
- package/resource-fetch-graphql.cjs +265 -0
- package/resource-fetch-size.cjs +156 -0
- package/resource-fetch.cjs +227 -428
- package/resource-fetch.d.cts +1 -60
- package/resource-fetch.d.ts +17 -6
- package/scripts/sync-apple-native-sources.mjs +101 -53
- package/types/capture.d.ts +95 -0
- package/types/config.d.ts +85 -0
- package/types/defaults.d.ts +55 -0
- package/types/events.d.ts +203 -0
- package/types/provider.d.ts +81 -0
- package/types/trace.d.ts +104 -0
package/instrumentation.d.cts
CHANGED
|
@@ -1,84 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
ReactNavigationContainerLike,
|
|
4
|
-
ReactNativeAppStateLike,
|
|
5
|
-
ReactNativePlatformLike,
|
|
6
|
-
ReactNativeTraceContext,
|
|
7
|
-
ReactNativeTraceInput,
|
|
8
|
-
TracePropagationTarget
|
|
9
|
-
} from "./index.cjs";
|
|
10
|
-
import type { LogBrewNativeBridgeLike, LogBrewNativeBridgeScope, LogBrewNativeBridgeScopeInput } from "./native-bridge.cjs";
|
|
11
|
-
import type { ReactNativeResourceFetchOptions } from "./resource-fetch.cjs";
|
|
12
|
-
|
|
13
|
-
export type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
14
|
-
appState?: ReactNativeAppStateLike;
|
|
15
|
-
captureInitialLifecycleState?: boolean;
|
|
16
|
-
captureInitialNavigationRoute?: boolean;
|
|
17
|
-
fetchImpl?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["fetchImpl"];
|
|
18
|
-
globalObject?: ReactNativeGlobalObjectLike;
|
|
19
|
-
includeRouteKey?: boolean;
|
|
20
|
-
instrumentGlobalFetch?: boolean;
|
|
21
|
-
instrumentGlobalXMLHttpRequest?: boolean;
|
|
22
|
-
logger?: string;
|
|
23
|
-
measureFetchResponseBodySize?: boolean;
|
|
24
|
-
measureXhrResponseBodySize?: boolean;
|
|
25
|
-
metadata?: Metadata;
|
|
26
|
-
metadataFactory?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["metadataFactory"];
|
|
27
|
-
nativeBridge?: LogBrewNativeBridgeLike;
|
|
28
|
-
navigation?: ReactNavigationContainerLike;
|
|
29
|
-
navigationContainer?: ReactNavigationContainerLike;
|
|
30
|
-
now?: () => string;
|
|
31
|
-
nowMs?: () => number;
|
|
32
|
-
onError?: (error: unknown) => void;
|
|
33
|
-
platform?: ReactNativePlatformLike;
|
|
34
|
-
randomValues?: (length: number) => ArrayLike<number>;
|
|
35
|
-
routeTemplate?: string;
|
|
36
|
-
routeTemplateFactory?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["routeTemplateFactory"];
|
|
37
|
-
screen?: string;
|
|
38
|
-
sessionId?: string;
|
|
39
|
-
trace?: ReactNativeTraceInput;
|
|
40
|
-
traceFlags?: string;
|
|
41
|
-
tracePropagationTargets?: TracePropagationTarget[];
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type ReactNativeGlobalObjectLike = {
|
|
45
|
-
fetch?: unknown;
|
|
46
|
-
XMLHttpRequest?: unknown;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type ReactNativeGlobalFetchInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
50
|
-
readonly fetch: (input: TInput, init?: TInit) => Promise<TResponse>;
|
|
51
|
-
remove(): void;
|
|
52
|
-
stop(): void;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type ReactNativeGlobalXMLHttpRequestInstrumentation = {
|
|
56
|
-
remove(): void;
|
|
57
|
-
stop(): void;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
61
|
-
readonly trace: ReactNativeTraceContext;
|
|
62
|
-
readonly globalFetch?: ReactNativeGlobalFetchInstrumentation<TInput, TInit, TResponse>;
|
|
63
|
-
readonly globalXMLHttpRequest?: ReactNativeGlobalXMLHttpRequestInstrumentation;
|
|
64
|
-
readonly resourceFetch: (input: TInput, init?: TInit) => Promise<TResponse>;
|
|
65
|
-
remove(): void;
|
|
66
|
-
stop(): void;
|
|
67
|
-
syncNativeBridgeScope(input?: LogBrewNativeBridgeScopeInput): LogBrewNativeBridgeScope | undefined;
|
|
68
|
-
withNativeBridgeScope<TResult>(callback: (scope: LogBrewNativeBridgeScope) => TResult): TResult;
|
|
69
|
-
withNativeBridgeScope<TResult>(
|
|
70
|
-
input: LogBrewNativeBridgeScopeInput,
|
|
71
|
-
callback: (scope: LogBrewNativeBridgeScope) => TResult
|
|
72
|
-
): TResult;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export declare function createLogBrewReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown>(
|
|
76
|
-
client: LogBrewClient,
|
|
77
|
-
options?: ReactNativeInstrumentationOptions<TInput, TInit, TResponse>
|
|
78
|
-
): ReactNativeInstrumentation<TInput, TInit, TResponse>;
|
|
79
|
-
|
|
80
|
-
declare const defaultExport: {
|
|
81
|
-
createLogBrewReactNativeInstrumentation: typeof createLogBrewReactNativeInstrumentation;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export default defaultExport;
|
|
1
|
+
export * from "./instrumentation.js";
|
|
2
|
+
export { default } from "./instrumentation.js";
|
package/instrumentation.d.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { LogBrewClient, Metadata } from "@logbrew/sdk";
|
|
2
2
|
import type {
|
|
3
|
-
|
|
3
|
+
LogBrewNativeBridgeLike,
|
|
4
|
+
LogBrewNativeBridgeScope,
|
|
5
|
+
LogBrewNativeBridgeScopeInput
|
|
6
|
+
} from "./native-bridge.js";
|
|
7
|
+
import type {
|
|
4
8
|
ReactNativeAppStateLike,
|
|
5
9
|
ReactNativePlatformLike,
|
|
6
10
|
ReactNativeTraceContext,
|
|
7
11
|
ReactNativeTraceInput,
|
|
12
|
+
ReactNavigationContainerLike,
|
|
8
13
|
TracePropagationTarget
|
|
9
14
|
} from "./index.js";
|
|
10
|
-
import type { LogBrewNativeBridgeLike, LogBrewNativeBridgeScope, LogBrewNativeBridgeScopeInput } from "./native-bridge.js";
|
|
11
15
|
import type { ReactNativeResourceFetchOptions } from "./resource-fetch.js";
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
14
18
|
appState?: ReactNativeAppStateLike;
|
|
15
19
|
captureInitialLifecycleState?: boolean;
|
|
16
20
|
captureInitialNavigationRoute?: boolean;
|
|
@@ -41,23 +45,23 @@ export type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown,
|
|
|
41
45
|
tracePropagationTargets?: TracePropagationTarget[];
|
|
42
46
|
};
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
type ReactNativeGlobalObjectLike = {
|
|
45
49
|
fetch?: unknown;
|
|
46
50
|
XMLHttpRequest?: unknown;
|
|
47
51
|
};
|
|
48
52
|
|
|
49
|
-
|
|
53
|
+
type ReactNativeGlobalFetchInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
50
54
|
readonly fetch: (input: TInput, init?: TInit) => Promise<TResponse>;
|
|
51
55
|
remove(): void;
|
|
52
56
|
stop(): void;
|
|
53
57
|
};
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
type ReactNativeGlobalXMLHttpRequestInstrumentation = {
|
|
56
60
|
remove(): void;
|
|
57
61
|
stop(): void;
|
|
58
62
|
};
|
|
59
63
|
|
|
60
|
-
|
|
64
|
+
type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
|
|
61
65
|
readonly trace: ReactNativeTraceContext;
|
|
62
66
|
readonly globalFetch?: ReactNativeGlobalFetchInstrumentation<TInput, TInit, TResponse>;
|
|
63
67
|
readonly globalXMLHttpRequest?: ReactNativeGlobalXMLHttpRequestInstrumentation;
|
|
@@ -72,7 +76,7 @@ export type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TRespo
|
|
|
72
76
|
): TResult;
|
|
73
77
|
};
|
|
74
78
|
|
|
75
|
-
|
|
79
|
+
declare function createLogBrewReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown>(
|
|
76
80
|
client: LogBrewClient,
|
|
77
81
|
options?: ReactNativeInstrumentationOptions<TInput, TInit, TResponse>
|
|
78
82
|
): ReactNativeInstrumentation<TInput, TInit, TResponse>;
|
|
@@ -81,4 +85,14 @@ declare const defaultExport: {
|
|
|
81
85
|
createLogBrewReactNativeInstrumentation: typeof createLogBrewReactNativeInstrumentation;
|
|
82
86
|
};
|
|
83
87
|
|
|
88
|
+
export type {
|
|
89
|
+
ReactNativeGlobalFetchInstrumentation,
|
|
90
|
+
ReactNativeGlobalObjectLike,
|
|
91
|
+
ReactNativeGlobalXMLHttpRequestInstrumentation,
|
|
92
|
+
ReactNativeInstrumentation,
|
|
93
|
+
ReactNativeInstrumentationOptions
|
|
94
|
+
};
|
|
95
|
+
export {
|
|
96
|
+
createLogBrewReactNativeInstrumentation
|
|
97
|
+
};
|
|
84
98
|
export default defaultExport;
|
|
@@ -3,7 +3,7 @@ import Foundation
|
|
|
3
3
|
@objc(LBRNAppleNativeDiagnostics)
|
|
4
4
|
public final class LBRNAppleNativeDiagnostics: NSObject, @unchecked Sendable {
|
|
5
5
|
private static let shared = LBRNAppleNativeDiagnostics()
|
|
6
|
-
private static let sdkVersion = "0.1.
|
|
6
|
+
private static let sdkVersion = "0.1.40"
|
|
7
7
|
|
|
8
8
|
private let lock = NSLock()
|
|
9
9
|
private let replayQueue = DispatchQueue(label: "co.logbrew.react-native.apple-diagnostics-replay")
|
|
@@ -27,10 +27,12 @@ final class DeliveryEngine: @unchecked Sendable {
|
|
|
27
27
|
case accepted(statusCode: Int, attempts: Int)
|
|
28
28
|
case retryable(attempts: Int, error: SdkError)
|
|
29
29
|
case terminal(reason: DeliveryPauseReason, attempts: Int, error: SdkError)
|
|
30
|
+
case rateLimited(retryAfter: TimeInterval?, attempts: Int, error: SdkError)
|
|
30
31
|
|
|
31
32
|
var attempts: Int {
|
|
32
33
|
switch self {
|
|
33
|
-
case let .accepted(_, attempts), let .retryable(attempts, _), let .terminal(_, attempts, _)
|
|
34
|
+
case let .accepted(_, attempts), let .retryable(attempts, _), let .terminal(_, attempts, _),
|
|
35
|
+
let .rateLimited(_, attempts, _):
|
|
34
36
|
attempts
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -104,6 +106,10 @@ final class DeliveryEngine: @unchecked Sendable {
|
|
|
104
106
|
consecutiveFailures: consecutiveFailures,
|
|
105
107
|
lastOutcome: lastOutcome,
|
|
106
108
|
pauseReason: pauseReason,
|
|
109
|
+
nextRetryDelay: state == .retrying ? nextWakeNanoseconds.map { deadline in
|
|
110
|
+
let now = DispatchTime.now().uptimeNanoseconds
|
|
111
|
+
return TimeInterval(deadline > now ? deadline - now : 0) / 1_000_000_000
|
|
112
|
+
} : nil,
|
|
107
113
|
)
|
|
108
114
|
}
|
|
109
115
|
}
|
|
@@ -135,9 +135,33 @@ extension DeliveryEngine {
|
|
|
135
135
|
case let .terminal(reason, _, _):
|
|
136
136
|
applyTerminalResultLocked(reason: reason)
|
|
137
137
|
return false
|
|
138
|
+
case let .rateLimited(retryAfter, _, _):
|
|
139
|
+
return applyRateLimitedResultLocked(retryAfter: retryAfter)
|
|
138
140
|
}
|
|
139
141
|
}
|
|
140
142
|
|
|
143
|
+
/// Schedules a timed retry of the same prefix after a rate-limited response instead of pausing.
|
|
144
|
+
func applyRateLimitedResultLocked(retryAfter: TimeInterval?) -> Bool {
|
|
145
|
+
consecutiveFailures += 1
|
|
146
|
+
lastOutcome = .retryableFailure
|
|
147
|
+
guard let options = automaticOptions, automaticTransport != nil, state != .shuttingDown, state != .closed
|
|
148
|
+
else {
|
|
149
|
+
return false
|
|
150
|
+
}
|
|
151
|
+
state = .retrying
|
|
152
|
+
pauseReason = .none
|
|
153
|
+
nextWakeNanoseconds = nil
|
|
154
|
+
let wait = retryAfter ?? retryBackoffDelay(options, attempt: consecutiveFailures)
|
|
155
|
+
return setWakeLocked(delay: Self.rateLimitDelay(wait), replaceLaterWake: true)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// Clamps a rate-limit wait to 1 second through 1 hour, then adds up to 10 percent jitter within 1 hour.
|
|
159
|
+
static func rateLimitDelay(_ delay: TimeInterval, jitter: Double = .random(in: 0 ... 1)) -> TimeInterval {
|
|
160
|
+
let maximum: TimeInterval = 60 * 60
|
|
161
|
+
let bounded = delay.isNaN ? 1 : min(max(delay, 1), maximum)
|
|
162
|
+
return min(bounded + bounded * 0.1 * jitter, maximum)
|
|
163
|
+
}
|
|
164
|
+
|
|
141
165
|
private func applyAcceptedResultLocked(_ prefix: FrozenPrefix) -> Bool {
|
|
142
166
|
acknowledgeLocked(prefix)
|
|
143
167
|
acceptedEvents += prefix.count
|
|
@@ -199,10 +223,13 @@ extension DeliveryEngine {
|
|
|
199
223
|
guard let options = automaticOptions else {
|
|
200
224
|
return false
|
|
201
225
|
}
|
|
202
|
-
|
|
226
|
+
return setWakeLocked(delay: retryBackoffDelay(options, attempt: retryAttempt), replaceLaterWake: true)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private func retryBackoffDelay(_ options: AutomaticDeliveryOptions, attempt: Int) -> TimeInterval {
|
|
230
|
+
let multiplier = pow(2, Double(max(0, attempt - 1)))
|
|
203
231
|
let ceiling = min(options.retryBaseDelay * multiplier, options.maxRetryDelay)
|
|
204
|
-
|
|
205
|
-
return setWakeLocked(delay: delay, replaceLaterWake: true)
|
|
232
|
+
return Double.random(in: (ceiling / 2) ... ceiling)
|
|
206
233
|
}
|
|
207
234
|
|
|
208
235
|
private func setWakeLocked(delay: TimeInterval, replaceLaterWake: Bool) -> Bool {
|
|
@@ -240,6 +267,9 @@ extension DeliveryEngine {
|
|
|
240
267
|
stateLock.unlock()
|
|
241
268
|
throw SdkError(code: "storage_corrupt", message: "durable delivery data requires explicit recovery")
|
|
242
269
|
}
|
|
270
|
+
if state == .retrying {
|
|
271
|
+
state = .running
|
|
272
|
+
}
|
|
243
273
|
nextWakeNanoseconds = nil
|
|
244
274
|
stateLock.unlock()
|
|
245
275
|
}
|
|
@@ -247,7 +277,9 @@ extension DeliveryEngine {
|
|
|
247
277
|
func rescheduleAfterManualOperation() {
|
|
248
278
|
stateLock.lock()
|
|
249
279
|
let shouldSchedule: Bool
|
|
250
|
-
if
|
|
280
|
+
if state == .retrying, nextWakeNanoseconds != nil {
|
|
281
|
+
shouldSchedule = true
|
|
282
|
+
} else if automaticTransport != nil, state != .paused, state != .shuttingDown, state != .closed {
|
|
251
283
|
state = .running
|
|
252
284
|
retryAttempt = 0
|
|
253
285
|
shouldSchedule = scheduleLiveQueueLocked()
|
|
@@ -109,6 +109,9 @@ extension DeliveryEngine {
|
|
|
109
109
|
case let .terminal(reason, _, error):
|
|
110
110
|
recordManualFailure(outcome: .terminalFailure, pauseReason: reason)
|
|
111
111
|
throw error
|
|
112
|
+
case let .rateLimited(retryAfter, _, error):
|
|
113
|
+
recordManualRateLimit(retryAfter: retryAfter)
|
|
114
|
+
throw error
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
throw SdkError(code: "transport_error", message: "exhausted retries")
|
|
@@ -254,6 +257,10 @@ extension DeliveryEngine {
|
|
|
254
257
|
return .accepted(statusCode: response.statusCode, attempts: attempts)
|
|
255
258
|
}
|
|
256
259
|
let error = transportError(statusCode: response.statusCode)
|
|
260
|
+
let rateLimitSignal = response.retryAfter != nil || response.errorCode == "rate_limited"
|
|
261
|
+
if response.statusCode == 429, rateLimitSignal, response.errorCode != "usage_limit_exceeded" {
|
|
262
|
+
return .rateLimited(retryAfter: response.retryAfter, attempts: attempts, error: error)
|
|
263
|
+
}
|
|
257
264
|
if response.statusCode == 408 || response.statusCode >= 500 {
|
|
258
265
|
return .retryable(attempts: attempts, error: error)
|
|
259
266
|
}
|
|
@@ -293,6 +300,16 @@ extension DeliveryEngine {
|
|
|
293
300
|
stateLock.unlock()
|
|
294
301
|
}
|
|
295
302
|
|
|
303
|
+
private func recordManualRateLimit(retryAfter: TimeInterval?) {
|
|
304
|
+
stateLock.lock()
|
|
305
|
+
inFlight = false
|
|
306
|
+
let shouldSchedule = applyRateLimitedResultLocked(retryAfter: retryAfter)
|
|
307
|
+
stateLock.unlock()
|
|
308
|
+
if shouldSchedule {
|
|
309
|
+
scheduleTimerUpdate()
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
296
313
|
private func recordManualAttempts(_ attempts: Int) {
|
|
297
314
|
stateLock.lock()
|
|
298
315
|
deliveryAttempts += attempts
|
|
@@ -67,4 +67,6 @@ public struct DeliveryHealth: Codable, Equatable, Sendable {
|
|
|
67
67
|
public let consecutiveFailures: Int
|
|
68
68
|
public let lastOutcome: DeliveryOutcome
|
|
69
69
|
public let pauseReason: DeliveryPauseReason
|
|
70
|
+
/// Seconds until the scheduled automatic retry, or `nil` when no retry is scheduled.
|
|
71
|
+
public let nextRetryDelay: TimeInterval?
|
|
70
72
|
}
|
|
@@ -42,10 +42,16 @@ public struct TransportError: Error, Equatable, Sendable, CustomStringConvertibl
|
|
|
42
42
|
public struct TransportResponse: Codable, Equatable, Sendable {
|
|
43
43
|
public let statusCode: Int
|
|
44
44
|
public let attempts: Int
|
|
45
|
+
/// Readable `Retry-After` delay in seconds from a `429` response.
|
|
46
|
+
public let retryAfter: TimeInterval?
|
|
47
|
+
/// String `code` field from a `429` JSON response body.
|
|
48
|
+
public let errorCode: String?
|
|
45
49
|
|
|
46
|
-
public init(statusCode: Int, attempts: Int) {
|
|
50
|
+
public init(statusCode: Int, attempts: Int, retryAfter: TimeInterval? = nil, errorCode: String? = nil) {
|
|
47
51
|
self.statusCode = statusCode
|
|
48
52
|
self.attempts = attempts
|
|
53
|
+
self.retryAfter = retryAfter
|
|
54
|
+
self.errorCode = errorCode
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
57
|
|
|
@@ -95,13 +101,38 @@ public final class HTTPTransport: Transport {
|
|
|
95
101
|
return try TransportResponse(statusCode: requester(request), attempts: 1)
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
return try
|
|
104
|
+
return try sendWithURLSession(request)
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
|
|
107
|
+
/// Reads only `Retry-After` and at most 4 KiB of body for its `code` field, and only for a `429`.
|
|
108
|
+
static func transportResponse(_ response: HTTPURLResponse, body: Data?, now: Date = Date()) -> TransportResponse {
|
|
109
|
+
guard response.statusCode == 429 else {
|
|
110
|
+
return TransportResponse(statusCode: response.statusCode, attempts: 1)
|
|
111
|
+
}
|
|
112
|
+
let code = body.flatMap { body in
|
|
113
|
+
body.count <= 4096 ? try? JSONDecoder().decode(RateLimitBody.self, from: body).code : nil
|
|
114
|
+
}
|
|
115
|
+
let retryAfter = response.value(forHTTPHeaderField: "Retry-After").flatMap { retryAfterSeconds($0, now: now) }
|
|
116
|
+
return TransportResponse(statusCode: 429, attempts: 1, retryAfter: retryAfter, errorCode: code)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// Parses delta-seconds or an IMF-fixdate; a past date waits 0 seconds and anything else is unreadable.
|
|
120
|
+
static func retryAfterSeconds(_ value: String, now: Date) -> TimeInterval? {
|
|
121
|
+
let value = value.trimmingCharacters(in: .whitespaces)
|
|
122
|
+
if !value.isEmpty, value.allSatisfy({ $0.isASCII && $0.isNumber }) {
|
|
123
|
+
return TimeInterval(value)
|
|
124
|
+
}
|
|
125
|
+
let formatter = DateFormatter()
|
|
126
|
+
formatter.locale = Locale(identifier: "en_US_POSIX")
|
|
127
|
+
formatter.timeZone = TimeZone(identifier: "GMT")
|
|
128
|
+
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
|
|
129
|
+
return formatter.date(from: value).map { max(0, $0.timeIntervalSince(now)) }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private func sendWithURLSession(_ request: URLRequest) throws -> TransportResponse {
|
|
102
133
|
let semaphore = DispatchSemaphore(value: 0)
|
|
103
134
|
let result = HTTPTransportResultBox()
|
|
104
|
-
let task = session.dataTask(with: request) {
|
|
135
|
+
let task = session.dataTask(with: request) { data, response, error in
|
|
105
136
|
defer {
|
|
106
137
|
semaphore.signal()
|
|
107
138
|
}
|
|
@@ -113,7 +144,7 @@ public final class HTTPTransport: Transport {
|
|
|
113
144
|
result.value = .failure(TransportError.network("http transport did not return an HTTP response"))
|
|
114
145
|
return
|
|
115
146
|
}
|
|
116
|
-
result.value = .success(httpResponse
|
|
147
|
+
result.value = .success(Self.transportResponse(httpResponse, body: data))
|
|
117
148
|
}
|
|
118
149
|
task.resume()
|
|
119
150
|
|
|
@@ -157,7 +188,11 @@ public final class HTTPTransport: Transport {
|
|
|
157
188
|
}
|
|
158
189
|
|
|
159
190
|
private final class HTTPTransportResultBox: @unchecked Sendable {
|
|
160
|
-
var value: Result<
|
|
191
|
+
var value: Result<TransportResponse, Error>?
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private struct RateLimitBody: Decodable {
|
|
195
|
+
let code: String?
|
|
161
196
|
}
|
|
162
197
|
|
|
163
198
|
public enum ScriptedTransportResponse: Equatable, Sendable {
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"sources": {
|
|
4
4
|
"swift/logbrew-swift/Sources/LogBrew/AutomaticTelemetryContext.swift": "76ab7f8a7c79db1d54ba13da8d61d96af62c509116b7df0f9d220d5c88364c00",
|
|
5
|
-
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngine.swift": "
|
|
6
|
-
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngineAutomatic.swift": "
|
|
5
|
+
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngine.swift": "6cbc2c52f5b39b9627fa707b023b84fb306a736bec9f82163d7ca4aa21b01b1c",
|
|
6
|
+
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngineAutomatic.swift": "cd05cd45aeea52cde3d228938acf9ff2e784c1e2350cf2473c3b72030a3abf64",
|
|
7
7
|
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngineDurable.swift": "b0d2ca07cce7fe01fc110238c7d10aa9c8ada0d28509e4c022e7fa950a4c79b9",
|
|
8
|
-
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngineQueue.swift": "
|
|
9
|
-
"swift/logbrew-swift/Sources/LogBrew/DeliveryLifecycle.swift": "
|
|
8
|
+
"swift/logbrew-swift/Sources/LogBrew/DeliveryEngineQueue.swift": "e187a737e18dd88a4be8719f1a3a07776c6d6eb0edb5b1904a82c5059a0aef57",
|
|
9
|
+
"swift/logbrew-swift/Sources/LogBrew/DeliveryLifecycle.swift": "af7b36649f9d07ac4567652f578c008e4ec8086f14c157b2d5144df93d211aaf",
|
|
10
10
|
"swift/logbrew-swift/Sources/LogBrew/DurableDeliveryStore.swift": "7c292dda68efc3de790daf44733b25e4392226793343c21ba5df4f51fc1b402b",
|
|
11
11
|
"swift/logbrew-swift/Sources/LogBrew/DurableDeliveryStoreRecovery.swift": "79fee27975b6571954bf0b3692a50e60bbbcf53b5ab47f653a474db5c56d68ba",
|
|
12
12
|
"swift/logbrew-swift/Sources/LogBrew/EventEncoding.swift": "b88b6b2d17f7d0cee9ac1a1f34ef35b192cd33d213ef3c9e1c2d21cbcc304a97",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"swift/logbrew-swift/Sources/LogBrew/TelemetryContext.swift": "ce3ac93a62a345f8c9a8f4c69ce84efc991bf02a688f49bad5e68edcff2f6e08",
|
|
26
26
|
"swift/logbrew-swift/Sources/LogBrew/TelemetryContextValidation.swift": "ab5811b48df308bf8f6435efee30a24f91a62bfe8b6dea612a2077c8ae0eb302",
|
|
27
27
|
"swift/logbrew-swift/Sources/LogBrew/TraceEvidence.swift": "40aaa42976164c53d9f4d9e9e30d236534238da43bbe90832a673c8809ad54b6",
|
|
28
|
-
"swift/logbrew-swift/Sources/LogBrew/Transport.swift": "
|
|
28
|
+
"swift/logbrew-swift/Sources/LogBrew/Transport.swift": "692c50da219f4b9a3f718bc9c9407a920f726b66a7b0381b85e4094e0596d6ae",
|
|
29
29
|
"swift/logbrew-swift/Sources/LogBrew/URLSessionTrace.swift": "d0076c72671716c41bd7498d0509e453b8182c418156e169196fee9f26fc66b1",
|
|
30
30
|
"swift/logbrew-swift/Sources/LogBrew/URLSessionTracer.swift": "8f45867e5ec48fc7be8f4c7a015e887ed7f49f3a0ffb11bd850e1bb7e937f9a0",
|
|
31
31
|
"swift/logbrew-swift/Sources/LogBrew/Validation.swift": "4ce069077dcca7899b15733e9aee921392da1e2136996ae3d4eb37bd73a5af07",
|