@@ -147,9 +147,8 @@ Collector.prototype.networkError = function (briefcase, error) {
147147 }
148148 } , levels . ERROR )
149149
150- if ( root ) {
151- this . _flushCache ( cacheId )
152- }
150+ this . _removeLockFromCache ( cacheId )
151+ this . _flushUnlockedCache ( cacheId )
153152
154153 return { briefcase : briefcase }
155154}
@@ -158,10 +157,10 @@ Collector.prototype.end = function (briefcase, options) {
158157 var communicationId = briefcase . communication . id
159158
160159 if ( options && options . skip ) {
161- this . _deleteCache ( communicationId )
162- } else {
163- this . _flushCache ( communicationId )
160+ this . _skipCache ( communicationId )
164161 }
162+ this . _removeLockFromCache ( communicationId )
163+ this . _flushUnlockedCache ( communicationId )
165164}
166165
167166Collector . prototype . clientSend = function ( payload , briefcase , options ) {
@@ -207,9 +206,7 @@ Collector.prototype.clientSend = function (payload, briefcase, options) {
207206 d : payload . data
208207 } , payload . severity != null ? payload . severity : this . defaultSeverity )
209208
210- if ( root ) {
211- this . _lockCache ( cacheId )
212- }
209+ this . _addLockToCache ( cacheId )
213210
214211 var severity = this . _getSeverityHintFromCache ( cacheId )
215212
@@ -228,7 +225,6 @@ Collector.prototype.clientSend = function (payload, briefcase, options) {
228225}
229226
230227Collector . prototype . serverRecv = function ( payload , duffelBag , options ) {
231- briefcase = briefcase || { }
232228 var timestamp = microtime . now ( )
233229
234230 var communication = {
@@ -257,7 +253,7 @@ Collector.prototype.serverRecv = function (payload, duffelBag, options) {
257253 d : payload . data
258254 } , duffelBag . severity )
259255
260- this . _lockCache ( communication . id )
256+ this . _addLockToCache ( communication . id )
261257
262258 return { briefcase : briefcase }
263259}
@@ -276,7 +272,7 @@ Collector.prototype.serverSend = function (payload, briefcase, options) {
276272 var transactionId = briefcase . communication . transactionId
277273
278274 if ( options && options . skip ) {
279- this . _deleteCache ( communicationId )
275+ this . _skipCache ( communicationId )
280276 } else {
281277 this . _cache ( communicationId , {
282278 t : EVENT_TYPE . SERVER_SEND ,
@@ -287,9 +283,11 @@ Collector.prototype.serverSend = function (payload, briefcase, options) {
287283 s : payload . status ,
288284 d : payload . data
289285 } , payload . severity != null ? payload . severity : this . defaultSeverity )
290- this . _flushCache ( communicationId )
291286 }
292287
288+ this . _removeLockFromCache ( communicationId )
289+ this . _flushUnlockedCache ( communicationId )
290+
293291 var severity = this . _getSeverityHintFromCache ( communicationId )
294292
295293 var duffelBag = {
@@ -318,7 +316,7 @@ Collector.prototype.clientRecv = function (payload, duffelBag, briefcase) {
318316
319317 var root = ! communication . parentId
320318
321- var cacheId = root ? 'root-' + communication . id : communication . parentId
319+ var cacheId = root ? 'root-' + briefcase . csCtx . communicationId : communication . parentId
322320
323321 this . _cache ( cacheId , {
324322 t : EVENT_TYPE . CLIENT_RECV ,
@@ -333,9 +331,8 @@ Collector.prototype.clientRecv = function (payload, duffelBag, briefcase) {
333331 d : payload . data
334332 } , duffelBag . severity )
335333
336- if ( root ) {
337- this . _flushCache ( cacheId )
338- }
334+ this . _removeLockFromCache ( cacheId )
335+ this . _flushUnlockedCache ( cacheId )
339336
340337 return { briefcase : briefcase }
341338}
@@ -347,41 +344,56 @@ Collector.prototype._cache = function (cacheId, data, severity) {
347344 }
348345 if ( this . _eventBuffers [ cacheId ] == null ) {
349346 this . _eventBuffers [ cacheId ] = {
350- locked : false ,
347+ skipped : false ,
348+ locks : 0 ,
351349 severityBuf : new ExpiringBuffer ( this . _eventTtl ) ,
352350 eventBuf : new ExpiringBuffer ( this . _eventTtl )
353351 }
352+ } else if ( this . _eventBuffers [ cacheId ] . skipped ) {
353+ return
354354 }
355355 this . _eventBuffers [ cacheId ] . eventBuf . push ( data )
356356 this . _eventBuffers [ cacheId ] . severityBuf . push ( severity )
357357}
358358
359+ Collector . prototype . _skipCache = function ( cacheId ) {
360+ cacheId = cacheId || 'root'
361+ if ( this . _eventBuffers [ cacheId ] ) {
362+ this . _eventBuffers [ cacheId ] . severityBuf . clear ( )
363+ this . _eventBuffers [ cacheId ] . eventBuf . clear ( )
364+ this . _eventBuffers [ cacheId ] . skipped = true
365+ }
366+ }
367+
359368Collector . prototype . _deleteCache = function ( cacheId ) {
360369 cacheId = cacheId || 'root'
361370 delete this . _eventBuffers [ cacheId ]
362371}
363372
364- Collector . prototype . _lockCache = function ( cacheId ) {
373+ Collector . prototype . _addLockToCache = function ( cacheId ) {
365374 cacheId = cacheId || 'root'
366375 if ( this . _eventBuffers [ cacheId ] ) {
367- this . _eventBuffers [ cacheId ] . locked = true
376+ ++ this . _eventBuffers [ cacheId ] . locks
368377 }
369378}
370379
371- Collector . prototype . _unlockCache = function ( cacheId ) {
380+ Collector . prototype . _removeLockFromCache = function ( cacheId ) {
372381 cacheId = cacheId || 'root'
373382 if ( this . _eventBuffers [ cacheId ] ) {
374- this . _eventBuffers [ cacheId ] . locked = false
383+ if ( this . _eventBuffers [ cacheId ] . locks > 0 ) {
384+ -- this . _eventBuffers [ cacheId ] . locks
385+ }
375386 }
376387}
377388
378- Collector . prototype . _flushCache = function ( cacheId ) {
389+ Collector . prototype . _flushUnlockedCache = function ( cacheId ) {
379390 cacheId = cacheId || 'root'
380391 var self = this
381- if ( this . _eventBuffers [ cacheId ] ) {
382- if ( some ( this . _eventBuffers [ cacheId ] . severityBuf . elements ( ) , function ( severity ) {
383- return levels . gte ( severity , self . mustCollectSeverity )
384- } ) ) {
392+ if ( this . _eventBuffers [ cacheId ] && this . _eventBuffers [ cacheId ] . locks === 0 ) {
393+ if ( ! this . _eventBuffers [ cacheId ] . skipped &&
394+ some ( this . _eventBuffers [ cacheId ] . severityBuf . elements ( ) , function ( severity ) {
395+ return levels . gte ( severity , self . mustCollectSeverity )
396+ } ) ) {
385397 var events = this . _eventBuffers [ cacheId ] . eventBuf . flush ( )
386398 if ( events . length ) {
387399 this . _sampler . add ( events )
@@ -404,12 +416,13 @@ Collector.prototype._getSeverityHintFromCache = function (cacheId) {
404416Collector . prototype . collect = function ( ) {
405417 var self = this
406418 Object . keys ( this . _eventBuffers ) . forEach ( function ( key ) {
407- if ( self . _eventBuffers [ key ] . locked === true ) {
419+ if ( self . _eventBuffers [ key ] . locks > 0 ) {
408420 return
409421 } else {
410- if ( some ( self . _eventBuffers [ key ] . severityBuf . elements ( ) , function ( severity ) {
411- return levels . gte ( severity , self . mustCollectSeverity )
412- } ) ) {
422+ if ( ! self . _eventBuffers [ key ] . skipped &&
423+ some ( self . _eventBuffers [ key ] . severityBuf . elements ( ) , function ( severity ) {
424+ return levels . gte ( severity , self . mustCollectSeverity )
425+ } ) ) {
413426 var events = self . _eventBuffers [ key ] . eventBuf . flush ( )
414427 if ( events . length ) {
415428 self . _sampler . add ( events )
@@ -418,7 +431,8 @@ Collector.prototype.collect = function () {
418431 self . _eventBuffers [ key ] . eventBuf . expire ( )
419432 self . _eventBuffers [ key ] . severityBuf . expire ( )
420433
421- if ( self . _eventBuffers [ key ] . severityBuf . isEmpty ( ) ) {
434+ if ( self . _eventBuffers [ key ] . skipped ||
435+ self . _eventBuffers [ key ] . severityBuf . isEmpty ( ) ) {
422436 delete self . _eventBuffers [ key ]
423437 }
424438 }
0 commit comments