Skip to content

Commit 083666d

Browse files
ankit-thanekar007athanekarclaude
authored
Add thread-safe access to custom headers (#1235)
* Add thread-safe access to custom headers Implement thread-safe custom headers to prevent race conditions when accessing headers from multiple threads. Uses a serial dispatch queue to synchronize all custom header operations. Changes: - Add customHeaderQueue dispatch queue for synchronization - Add private _customHeaders backing storage - Convert customHeaders to computed property with thread-safe getter/setter - Update addCustomHeader() to use sync for thread-safe writes - Update clearCustomHeaders() to use sync for thread-safe clearing - Add getCustomHeaders() method for safe external access - Update PrebidServerConnection to use getCustomHeaders() getter The computed property approach maintains backward compatibility while ensuring all access (direct or via methods) goes through the synchronization queue. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * PR Comments --------- Co-authored-by: athanekar <ankitrajendra.thanekar@yahooinc.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 0b0dc99 commit 083666d

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

PrebidMobile/Swift/ConfigurationAndTargeting/Prebid.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,21 @@ public class Prebid: NSObject {
4444
/// Enables or disables debug mode.
4545
/// ORTB: bidRequest.test
4646
public var pbsDebug = false
47-
47+
4848
/// Custom HTTP headers to be sent with requests.
49-
public var customHeaders: [String: String] = [:]
49+
///
50+
/// Thread-safe: All access is synchronized via a serial dispatch queue.
51+
/// The getter returns a snapshot copy of the headers dictionary.
52+
public var customHeaders: [String: String] {
53+
get {
54+
customHeaderQueue.sync { _customHeaders }
55+
}
56+
set {
57+
customHeaderQueue.sync {
58+
self._customHeaders = newValue
59+
}
60+
}
61+
}
5062

5163
/// Stored bid responses identified by bidder names.
5264
public var storedBidResponses: [String: String] = [:]
@@ -138,6 +150,12 @@ public class Prebid: NSObject {
138150
*/
139151
public var shouldDisableStatusCheck: Bool = false
140152

153+
/// Serial dispatch queue for thread-safe custom header access
154+
private let customHeaderQueue = DispatchQueue(label: "com.prebid.customHeaderQ")
155+
156+
/// Backing storage for custom HTTP headers
157+
private var _customHeaders: [String: String] = [:]
158+
141159
// MARK: - Public Methods
142160

143161
// MARK: - Stored Bid Response
@@ -170,20 +188,24 @@ public class Prebid: NSObject {
170188
}
171189

172190
// MARK: - Custom Headers
173-
174-
/// Adds a custom HTTP header.
191+
192+
/// Adds a custom HTTP header in a thread-safe manner.
175193
/// - Parameters:
176194
/// - name: The name of the header.
177195
/// - value: The value of the header.
178196
public func addCustomHeader(name: String, value: String) {
179-
customHeaders[name] = value
197+
customHeaderQueue.sync {
198+
self._customHeaders[name] = value
199+
}
180200
}
181201

182-
/// Clears all custom HTTP headers.
202+
/// Clears all custom HTTP headers in a thread-safe manner.
183203
public func clearCustomHeaders() {
184-
customHeaders.removeAll()
204+
customHeaderQueue.sync {
205+
self._customHeaders.removeAll()
206+
}
185207
}
186-
208+
187209
/// Checks the status of Prebid Server. The `customStatusEndpoint` property is used as server status endpoint.
188210
/// If `customStatusEndpoint` property is not provided, the SDK will use default endpoint - `host` + `/status`.
189211
///

0 commit comments

Comments
 (0)