Skip to content

Commit f4906cc

Browse files
authored
Merge pull request #97 from getsentry/feature/too-many-requests
Add rate limiting for requests
2 parents 7930513 + bd03489 commit f4906cc

File tree

8 files changed

+46
-28
lines changed

8 files changed

+46
-28
lines changed

SentrySwift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "SentrySwift"
3-
s.version = "1.3.1"
3+
s.version = "1.3.2"
44
s.summary = "Swift client for Sentry"
55
s.homepage = "https://github.com/getsentry/sentry-swift"
66
s.license = "mit"

SentrySwift/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.3.1</string>
18+
<string>1.3.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

Sources/BreadcrumbStore.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public final class BreadcrumbStore: NSObject {
3838
storeUpdated?(self)
3939
}
4040

41-
/// Clears the store for given type or all if none specified
41+
/// Clears the store if crumbs exist
4242
public func clear() {
43+
guard crumbs.count > 0 else { return }
4344
crumbs.removeAll()
4445
storeUpdated?(self)
4546
}

Sources/EventOffline.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ extension SentryClient {
3636
public func savedEvents() -> [SavedEvent] {
3737
do {
3838
guard let path = directory() else { return [] }
39+
let now = NSDate().timeIntervalSince1970
3940

4041
#if swift(>=3.0)
4142
return try FileManager.default
4243
.contentsOfDirectory(atPath: path)
44+
.filter { fileName in
45+
let splitName = fileName.characters.split { $0 == "-" }.map(String.init)
46+
guard let storedTime = Double(splitName[0]) else {
47+
return true
48+
}
49+
return now > storedTime
50+
}
4351
.flatMap { fileName in
4452
let absolutePath: String = (path as NSString).appendingPathComponent(fileName)
4553
guard let data = NSData(contentsOfFile: absolutePath) else { return nil }
@@ -56,10 +64,17 @@ extension SentryClient {
5664
#else
5765
return try NSFileManager.defaultManager()
5866
.contentsOfDirectoryAtPath(path)
67+
.filter { fileName in
68+
let splitName = fileName.characters.split { $0 == "-" }.map(String.init)
69+
guard let storedTime = Double(splitName[0]) else {
70+
return true
71+
}
72+
return now > storedTime
73+
}
5974
.flatMap { fileName in
6075
let absolutePath: String = (path as NSString).stringByAppendingPathComponent(fileName)
6176
guard let data = NSData(contentsOfFile: absolutePath) else { return nil }
62-
77+
6378
return (data, {
6479
do {
6580
try NSFileManager.defaultManager().removeItemAtPath(absolutePath)
@@ -110,13 +125,14 @@ extension SentryClient {
110125
*/
111126
private func writePath(_ event: Event) throws -> String? {
112127
guard let sentryDir = directory() else { return nil }
128+
let date = NSDate().timeIntervalSince1970 + 60 + Double(arc4random_uniform(10) + 1)
113129

114130
#if swift(>=3.0)
115131
try FileManager.default.createDirectory(atPath: sentryDir, withIntermediateDirectories: true, attributes: nil)
116-
return (sentryDir as NSString).appendingPathComponent(event.eventID)
132+
return (sentryDir as NSString).appendingPathComponent("\(date)-\(event.eventID)")
117133
#else
118134
try NSFileManager.defaultManager().createDirectoryAtPath(sentryDir, withIntermediateDirectories: true, attributes: nil)
119-
return (sentryDir as NSString).stringByAppendingPathComponent(event.eventID)
135+
return (sentryDir as NSString).stringByAppendingPathComponent("\(date)-\(event.eventID)")
120136
#endif
121137
}
122138

@@ -127,14 +143,15 @@ extension SentryClient {
127143
- Returns: Serialized string
128144
*/
129145
private func serializedString(_ event: Event) throws -> String? {
146+
let serializedEvent = event.serialized
130147
#if swift(>=3.0)
131-
if JSONSerialization.isValidJSONObject(event.serialized) {
132-
let data: NSData = try JSONSerialization.data(withJSONObject: event.serialized, options: []) as NSData
148+
if JSONSerialization.isValidJSONObject(serializedEvent) {
149+
let data: NSData = try JSONSerialization.data(withJSONObject: serializedEvent, options: []) as NSData
133150
return String(data: data as Data, encoding: String.Encoding.utf8)
134151
}
135152
#else
136-
if NSJSONSerialization.isValidJSONObject(event.serialized) {
137-
let data: NSData = try NSJSONSerialization.dataWithJSONObject(event.serialized, options: [])
153+
if NSJSONSerialization.isValidJSONObject(serializedEvent) {
154+
let data: NSData = try NSJSONSerialization.dataWithJSONObject(serializedEvent, options: [])
138155
return String(data: data, encoding: NSUTF8StringEncoding)
139156
}
140157
#endif

Sources/Request.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ extension SentryClient {
1717
*/
1818
internal func sendEvent(_ event: Event, finished: SentryEndpointRequestFinished? = nil) {
1919
#if swift(>=3.0)
20-
SentryEndpoint.store(event: event).send(dsn: dsn, finished: finished)
20+
SentryEndpoint.store(event: event).send(session: session, dsn: dsn, finished: finished)
2121
#else
22-
SentryEndpoint.store(event: event).send(dsn, finished: finished)
22+
SentryEndpoint.store(event: event).send(session, dsn: dsn, finished: finished)
2323
#endif
2424
}
2525

@@ -30,9 +30,9 @@ extension SentryClient {
3030
*/
3131
internal func sendEvent(_ event: SavedEvent, finished: SentryEndpointRequestFinished? = nil) {
3232
#if swift(>=3.0)
33-
SentryEndpoint.storeSavedEvent(event: event).send(dsn: dsn, finished: finished)
33+
SentryEndpoint.storeSavedEvent(event: event).send(session: session, dsn: dsn, finished: finished)
3434
#else
35-
SentryEndpoint.storeSavedEvent(event: event).send(dsn, finished: finished)
35+
SentryEndpoint.storeSavedEvent(event: event).send(session, dsn: dsn, finished: finished)
3636
#endif
3737
}
3838

@@ -46,12 +46,12 @@ extension SentryClient {
4646
userFeedback.event = lastSuccessfullySentEvent
4747
}
4848
#if swift(>=3.0)
49-
SentryEndpoint.userFeedback(userFeedback: userFeedback).send(dsn: dsn) { [weak self] success in
49+
SentryEndpoint.userFeedback(userFeedback: userFeedback).send(session: session, dsn: dsn) { [weak self] success in
5050
self?.sentUserFeedback()
5151
finished?(success)
5252
}
5353
#else
54-
SentryEndpoint.userFeedback(userFeedback: userFeedback).send(dsn) { [weak self] success in
54+
SentryEndpoint.userFeedback(userFeedback: userFeedback).send(session, dsn: dsn) { [weak self] success in
5555
self?.sentUserFeedback()
5656
finished?(success)
5757
}

Sources/Sentry.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import KSCrash
3939
internal typealias JSONSerialization = NSJSONSerialization
4040
internal typealias Bundle = NSBundle
4141
internal typealias URLQueryItem = NSURLQueryItem
42+
internal typealias URLSession = NSURLSession
4243
#endif
4344

4445
internal enum SentryError: Error {
@@ -68,7 +69,7 @@ internal enum SentryError: Error {
6869
// MARK: - Enums
6970

7071
internal struct Info {
71-
static let version: String = "1.3.1"
72+
static let version: String = "1.3.2"
7273
static let sentryVersion: Int = 7
7374
}
7475

@@ -79,6 +80,7 @@ internal enum SentryError: Error {
7980
// MARK: - Attributes
8081

8182
internal let dsn: DSN
83+
internal let session: URLSession
8284
internal(set) var crashHandler: CrashHandler? {
8385
didSet {
8486
crashHandler?.startCrashReporting()
@@ -117,7 +119,7 @@ internal enum SentryError: Error {
117119
}
118120
#else
119121
dispatch_async(dispatch_get_main_queue(), {
120-
self.delegate?.userFeedbackReady()
122+
self.delegate?.userFeedbackReady()
121123
})
122124
#endif
123125
}
@@ -149,8 +151,10 @@ internal enum SentryError: Error {
149151

150152
#if swift(>=3.0)
151153
self.releaseVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
154+
self.session = URLSession(configuration: URLSessionConfiguration.ephemeral)
152155
#else
153156
self.releaseVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String
157+
self.session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
154158
#endif
155159

156160
super.init()
@@ -208,11 +212,11 @@ internal enum SentryError: Error {
208212
/// Reports given event to Sentry
209213
@objc public func captureEvent(_ event: Event) {
210214
#if swift(>=3.0)
211-
DispatchQueue(label: SentryClient.queueName).sync {
215+
DispatchQueue(label: SentryClient.queueName).async {
212216
self.captureEvent(event, useClientProperties: true)
213217
}
214218
#else
215-
dispatch_sync(dispatch_queue_create(SentryClient.queueName, nil), {
219+
dispatch_async(dispatch_queue_create(SentryClient.queueName, nil), {
216220
self.captureEvent(event, useClientProperties: true)
217221
})
218222
#endif
@@ -309,7 +313,7 @@ internal enum SentryError: Error {
309313
event.extra.unionInPlace(extra)
310314
}
311315
}
312-
316+
313317
if nil == event.breadcrumbsSerialized { // we only want to set the breadcrumbs if there are non in the event
314318
event.breadcrumbsSerialized = breadcrumbs.serialized
315319
}

Sources/SentryEndpoint.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protocol Endpoint {
2020
var httpMethod: HttpMethod { get }
2121
func route(dsn dsn: DSN) -> NSURL?
2222
var payload: NSData { get }
23-
func send(dsn: DSN, finished: SentryEndpointRequestFinished?)
23+
func send(session: URLSession, dsn: DSN, finished: SentryEndpointRequestFinished?)
2424
}
2525

2626
enum SentryEndpoint: Endpoint {
@@ -134,7 +134,7 @@ enum SentryEndpoint: Endpoint {
134134
}
135135
}
136136

137-
func send(dsn: DSN, finished: SentryEndpointRequestFinished? = nil) {
137+
func send(session: URLSession, dsn: DSN, finished: SentryEndpointRequestFinished? = nil) {
138138
guard let url = route(dsn: dsn) else {
139139
SentryLog.Error.log("Cannot find route for \(self)")
140140
finished?(false)
@@ -150,8 +150,6 @@ enum SentryEndpoint: Endpoint {
150150
configureRequest(dsn: dsn, request: request)
151151

152152
#if swift(>=3.0)
153-
let config = URLSessionConfiguration.default
154-
let session = URLSession(configuration: config)
155153
session.dataTask(with: request as URLRequest) { data, response, error in
156154
var success = false
157155

@@ -171,8 +169,6 @@ enum SentryEndpoint: Endpoint {
171169
finished?(success)
172170
}.resume()
173171
#else
174-
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
175-
let session = NSURLSession(configuration: config)
176172
session.dataTaskWithRequest(request) { data, response, error in
177173
var success = false
178174

docs/sentry-doc-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
}
1515
},
1616
"vars": {
17-
"SENTRY_SWIFT_TAG": "1.3.1"
17+
"SENTRY_SWIFT_TAG": "1.3.2"
1818
}
1919
}

0 commit comments

Comments
 (0)