@@ -19,6 +19,7 @@ internal class CachingTransport : ITransport, IAsyncDisposable, IDisposable
1919 private readonly ITransport _innerTransport ;
2020 private readonly SentryOptions _options ;
2121 private readonly string _isolatedCacheDirectoryPath ;
22+ private readonly int _keepCount ;
2223
2324 // When a file is getting processed, it's moved to a child directory
2425 // to avoid getting picked up by other threads.
@@ -43,6 +44,10 @@ public CachingTransport(ITransport innerTransport, SentryOptions options)
4344 _innerTransport = innerTransport ;
4445 _options = options ;
4546
47+ _keepCount = _options . MaxQueueItems >= 1
48+ ? _options . MaxQueueItems - 1
49+ : 0 ; // just in case MaxQueueItems is set to an invalid value somehow (shouldn't happen)
50+
4651 _isolatedCacheDirectoryPath = ! string . IsNullOrWhiteSpace ( options . CacheDirectoryPath )
4752 ? _isolatedCacheDirectoryPath = Path . Combine (
4853 options . CacheDirectoryPath ,
@@ -113,17 +118,14 @@ private void EnsureFreeSpaceInCache()
113118 // [f1] [f2] [f3] [f4] [f5]
114119 // |-------| <- keep these ones
115120 // |------------| <- delete these ones
116- var keepCount = _options . MaxQueueItems >= 1
117- ? _options . MaxQueueItems - 1
118- : 0 ; // just in case MaxQueueItems is set to an invalid value somehow (shouldn't happen)
119-
120- var excessCacheFilePaths = GetCacheFilePaths ( ) . SkipLast ( keepCount ) . ToArray ( ) ;
121+ var excessCacheFilePaths = GetCacheFilePaths ( ) . SkipLast ( _keepCount ) . ToArray ( ) ;
121122
122123 foreach ( var filePath in excessCacheFilePaths )
123124 {
124125 try
125126 {
126127 File . Delete ( filePath ) ;
128+ _options . DiagnosticLogger ? . LogDebug ( "Deleted cached file {0}." , filePath ) ;
127129 }
128130 catch ( FileNotFoundException )
129131 {
@@ -221,6 +223,7 @@ exception is OperationCanceledException // Timed-out or Shutdown triggered
221223 var filePath = GetCacheFilePaths ( ) . FirstOrDefault ( ) ;
222224 if ( string . IsNullOrWhiteSpace ( filePath ) )
223225 {
226+ _options . DiagnosticLogger ? . LogDebug ( "No cached file to process." ) ;
224227 return null ;
225228 }
226229
@@ -236,10 +239,6 @@ private async Task StoreToCacheAsync(
236239 Envelope envelope ,
237240 CancellationToken cancellationToken = default )
238241 {
239- using var lockClaim = await _cacheDirectoryLock . AcquireAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
240-
241- EnsureFreeSpaceInCache ( ) ;
242-
243242 // Envelope file name can be either:
244243 // 1604679692_b2495755f67e4bb8a75504e5ce91d6c1_17754019.envelope
245244 // 1604679692__17754019.envelope
@@ -252,6 +251,12 @@ private async Task StoreToCacheAsync(
252251 $ ".{ EnvelopeFileExt } "
253252 ) ;
254253
254+ _options . DiagnosticLogger ? . LogDebug ( "Storing file {0}." , envelopeFilePath ) ;
255+
256+ using var lockClaim = await _cacheDirectoryLock . AcquireAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
257+
258+ EnsureFreeSpaceInCache ( ) ;
259+
255260 Directory . CreateDirectory ( _isolatedCacheDirectoryPath ) ;
256261
257262 using ( var stream = File . Create ( envelopeFilePath ) )
0 commit comments