-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDataUploadDelay.swift
39 lines (32 loc) · 1.14 KB
/
DataUploadDelay.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
import DatadogInternal
internal protocol Delay {
var current: TimeInterval { get }
mutating func decrease()
mutating func increase()
}
/// Mutable interval used for periodic data uploads.
internal struct DataUploadDelay: Delay {
private let minDelay: TimeInterval
private let maxDelay: TimeInterval
private let changeRate: Double
private var delay: TimeInterval
init(performance: UploadPerformancePreset) {
self.minDelay = performance.minUploadDelay
self.maxDelay = performance.maxUploadDelay
self.changeRate = performance.uploadDelayChangeRate
self.delay = performance.initialUploadDelay
}
var current: TimeInterval { delay }
mutating func decrease() {
delay = max(minDelay, delay * (1.0 - changeRate))
}
mutating func increase() {
delay = min(delay * (1.0 + changeRate), maxDelay)
}
}