@@ -53,6 +53,8 @@ @interface SentryHttpTransport ()
53
53
@property (nonatomic , strong )
54
54
NSMutableDictionary <NSString *, SentryDiscardedEvent *> *discardedEvents;
55
55
56
+ @property (nonatomic , strong ) NSMutableArray <SentryEnvelope *> *notStoredEnvelopes;
57
+
56
58
/* *
57
59
* Synching with a dispatch queue to have concurrent reads and writes as barrier blocks is roughly
58
60
* 30% slower than using atomic here.
@@ -87,6 +89,7 @@ - (id)initWithOptions:(SentryOptions *)options
87
89
_isSending = NO ;
88
90
_isFlushing = NO ;
89
91
self.discardedEvents = [NSMutableDictionary new ];
92
+ self.notStoredEnvelopes = [NSMutableArray new ];
90
93
[self .envelopeRateLimit setDelegate: self ];
91
94
[self .fileManager setDelegate: self ];
92
95
@@ -132,7 +135,13 @@ - (void)sendEnvelope:(SentryEnvelope *)envelope
132
135
// thread, which could be the main thread.
133
136
__weak SentryHttpTransport *weakSelf = self;
134
137
[self .dispatchQueue dispatchAsyncWithBlock: ^{
135
- [weakSelf.fileManager storeEnvelope: envelopeToStore];
138
+ NSString *path = [weakSelf.fileManager storeEnvelope: envelopeToStore];
139
+ if (path == nil ) {
140
+ SENTRY_LOG_DEBUG (@" Could not store envelope. Schedule for sending." );
141
+ @synchronized (weakSelf.notStoredEnvelopes ) {
142
+ [weakSelf.notStoredEnvelopes addObject: envelopeToStore];
143
+ }
144
+ }
136
145
[weakSelf sendAllCachedEnvelopes ];
137
146
}];
138
147
}
@@ -280,24 +289,38 @@ - (void)sendAllCachedEnvelopes
280
289
self.isSending = YES ;
281
290
}
282
291
283
- SentryFileContents *envelopeFileContents = [self .fileManager getOldestEnvelope ];
284
- if (nil == envelopeFileContents) {
285
- SENTRY_LOG_DEBUG (@" No envelopes left to send." );
286
- [self finishedSending ];
287
- return ;
292
+ SentryEnvelope *envelope;
293
+ NSString *envelopeFilePath;
294
+
295
+ @synchronized (self.notStoredEnvelopes ) {
296
+ if (self.notStoredEnvelopes .count > 0 ) {
297
+ envelope = self.notStoredEnvelopes [0 ];
298
+ [self .notStoredEnvelopes removeObjectAtIndex: 0 ];
299
+ }
288
300
}
289
301
290
- SentryEnvelope *envelope = [SentrySerialization envelopeWithData: envelopeFileContents.contents];
291
- if (nil == envelope) {
292
- SENTRY_LOG_DEBUG (@" Envelope contained no deserializable data." );
293
- [self deleteEnvelopeAndSendNext: envelopeFileContents.path];
294
- return ;
302
+ if (envelope == nil ) {
303
+ SentryFileContents *envelopeFileContents = [self .fileManager getOldestEnvelope ];
304
+ if (nil == envelopeFileContents) {
305
+ SENTRY_LOG_DEBUG (@" No envelopes left to send." );
306
+ [self finishedSending ];
307
+ return ;
308
+ }
309
+
310
+ envelopeFilePath = envelopeFileContents.path ;
311
+
312
+ envelope = [SentrySerialization envelopeWithData: envelopeFileContents.contents];
313
+ if (nil == envelope) {
314
+ SENTRY_LOG_DEBUG (@" Envelope contained no deserializable data." );
315
+ [self deleteEnvelopeAndSendNext: envelopeFilePath];
316
+ return ;
317
+ }
295
318
}
296
319
297
320
SentryEnvelope *rateLimitedEnvelope = [self .envelopeRateLimit removeRateLimitedItems: envelope];
298
321
if (rateLimitedEnvelope.items .count == 0 ) {
299
322
SENTRY_LOG_DEBUG (@" Envelope had no rate-limited items, nothing to send." );
300
- [self deleteEnvelopeAndSendNext: envelopeFileContents.path ];
323
+ [self deleteEnvelopeAndSendNext: envelopeFilePath ];
301
324
return ;
302
325
}
303
326
@@ -309,22 +332,24 @@ - (void)sendAllCachedEnvelopes
309
332
dsn: self .options.parsedDsn
310
333
didFailWithError: &requestError];
311
334
312
- if (nil != requestError) {
313
- SENTRY_LOG_DEBUG (@" Failed to build request: %@ ." , requestError);
335
+ if (nil == request || nil != requestError) {
336
+ if (nil != requestError) {
337
+ SENTRY_LOG_DEBUG (@" Failed to build request: %@ ." , requestError);
338
+ }
314
339
[self recordLostEventFor: rateLimitedEnvelope.items];
315
- [self deleteEnvelopeAndSendNext: envelopeFileContents.path ];
340
+ [self deleteEnvelopeAndSendNext: envelopeFilePath ];
316
341
return ;
317
342
} else {
318
- [self sendEnvelope: rateLimitedEnvelope
319
- envelopePath: envelopeFileContents.path
320
- request: request];
343
+ [self sendEnvelope: rateLimitedEnvelope envelopePath: envelopeFilePath request: request];
321
344
}
322
345
}
323
346
324
- - (void )deleteEnvelopeAndSendNext : (NSString *)envelopePath
347
+ - (void )deleteEnvelopeAndSendNext : (nullable NSString *)envelopePath
325
348
{
326
349
SENTRY_LOG_DEBUG (@" Deleting envelope and sending next." );
327
- [self .fileManager removeFileAtPath: envelopePath];
350
+ if (envelopePath != nil ) {
351
+ [self .fileManager removeFileAtPath: envelopePath];
352
+ }
328
353
@synchronized (self) {
329
354
self.isSending = NO ;
330
355
}
@@ -340,7 +365,7 @@ - (void)deleteEnvelopeAndSendNext:(NSString *)envelopePath
340
365
}
341
366
342
367
- (void )sendEnvelope : (SentryEnvelope *)envelope
343
- envelopePath : (NSString *)envelopePath
368
+ envelopePath : (NSString *_Nullable )envelopePath
344
369
request : (NSURLRequest *)request
345
370
{
346
371
__weak SentryHttpTransport *weakSelf = self;
0 commit comments