@@ -47,11 +47,6 @@ function Collector (options) {
4747
4848Collector . prototype . LEVELS = levels
4949
50- Collector . prototype . CACHE_MODES = {
51- NORMAL : 0 ,
52- RETAIN_UNTIL_SS : 1
53- }
54-
5550Collector . prototype . userSentEvent = function ( briefcase , name , payload ) {
5651 briefcase = briefcase || { }
5752 var communicationId = briefcase . communication && briefcase . communication . id
@@ -144,7 +139,11 @@ Collector.prototype.networkError = function (briefcase, error) {
144139
145140 var self = this
146141
147- this . _withCache ( communicationId , function ( cache ) {
142+ var root = ! communicationId
143+
144+ var cacheId = root ? briefcase . csCtx . communicationId : communicationId
145+
146+ this . _withCache ( cacheId , function ( cache ) {
148147 cache . buffer . push ( {
149148 t : EVENT_TYPE . ERROR ,
150149 r : briefcase . csCtx . transactionId ,
@@ -162,18 +161,36 @@ Collector.prototype.networkError = function (briefcase, error) {
162161 } )
163162 cache . severity = severity = levels . greater ( cache . severity , severity )
164163 } )
164+
165+ if ( root ) {
166+ this . _flushCache ( cacheId )
167+ }
168+
165169 return { briefcase : assign ( { severity : severity } , briefcase ) }
166170}
167171
168- Collector . prototype . clientSend = function ( payload , briefcase ) {
172+ Collector . prototype . end = function ( briefcase , options ) {
173+ var communicationId = briefcase . communication . id
174+
175+ if ( options && options . skip ) {
176+ this . _deleteCache ( communicationId )
177+ } else {
178+ this . _flushCache ( communicationId )
179+ }
180+ }
181+
182+ Collector . prototype . clientSend = function ( payload , briefcase , options ) {
169183 briefcase = briefcase || { }
170184 var timestamp = microtime . now ( )
171185
172186 var parentCommunicationId = briefcase . communication && briefcase . communication . id
173187
174188 var transactionId = briefcase . communication && briefcase . communication . transactionId
175189
190+ var root = ! parentCommunicationId
191+
176192 if ( ! transactionId ) {
193+ root = true
177194 transactionId = uuid . v4 ( )
178195 }
179196
@@ -192,7 +209,7 @@ Collector.prototype.clientSend = function (payload, briefcase) {
192209 transactionId : transactionId
193210 }
194211
195- var cacheId = communication . parentId
212+ var cacheId = root ? communication . id : communication . parentId
196213
197214 this . _withCache ( cacheId , function ( cache ) {
198215 cache . buffer . push ( {
@@ -210,6 +227,10 @@ Collector.prototype.clientSend = function (payload, briefcase) {
210227 cache . severity = severity = levels . greater ( cache . severity , severity )
211228 } )
212229
230+ if ( root ) {
231+ this . _lockCache ( cacheId )
232+ }
233+
213234 var duffelBag = {
214235 transactionId : transactionId ,
215236 timestamp : timestamp ,
@@ -258,7 +279,9 @@ Collector.prototype.serverRecv = function (payload, duffelBag, options) {
258279 d : payload . data
259280 } )
260281 cache . severity = severity
261- } , { mode : options && options . cacheMode } )
282+ } )
283+
284+ this . _lockCache ( communication . id )
262285
263286 return { briefcase : briefcase }
264287}
@@ -284,11 +307,11 @@ Collector.prototype.serverSend = function (payload, briefcase, options) {
284307 targetServiceKey : this . serviceKey
285308 }
286309
287- var self = this
288-
289- this . _withCache ( communicationId , function ( cache ) {
290- cache . severity = severity = levels . greater ( severity , cache . severity )
291- if ( ! options || ! options . skip ) {
310+ if ( options && options . skip ) {
311+ this . _deleteCache ( communicationId )
312+ } else {
313+ this . _withCache ( communicationId , function ( cache ) {
314+ cache . severity = severity = levels . greater ( severity , cache . severity )
292315 cache . buffer . push ( {
293316 t : EVENT_TYPE . SERVER_SEND ,
294317 r : transactionId ,
@@ -298,22 +321,20 @@ Collector.prototype.serverSend = function (payload, briefcase, options) {
298321 s : payload . status ,
299322 d : payload . data
300323 } )
301- if ( severity <= self . mustCollectSeverity ) {
302- Array . prototype . push . apply ( self . _mustCollectBuffer , cache . buffer . elements ( ) )
303- }
304- }
305- } )
324+ } )
325+ this . _flushCache ( communicationId )
326+ }
306327
307- this . _deleteCache ( communicationId )
308328 return { briefcase : assign ( { severity : severity } , briefcase ) , duffelBag : duffelBag }
309329}
310330
311331Collector . prototype . clientRecv = function ( payload , duffelBag , briefcase ) {
312332 briefcase = briefcase || { }
313333 var timestamp = microtime . now ( )
314334 if ( ! briefcase . csCtx ) {
315- debug ( this . name , 'Error: cannot collect CR event without a CS context. Ignoring' )
316- return { briefcase : briefcase }
335+ var err = new Error ( 'cannot collect CR event without a CS context. Ignoring' )
336+ debug ( '#clientRecv' , '[Warning]' , err . toString ( ) )
337+ return { briefcase : briefcase , error : err }
317338 }
318339
319340 var communication = {
@@ -324,7 +345,11 @@ Collector.prototype.clientRecv = function (payload, duffelBag, briefcase) {
324345
325346 var severity = levels . greater ( briefcase . severity , duffelBag . severity )
326347
327- this . _withCache ( communication . parentId , function ( cache ) {
348+ var root = ! communication . parentId
349+
350+ var cacheId = root ? communication . id : communication . parentId
351+
352+ this . _withCache ( cacheId , function ( cache ) {
328353 cache . buffer . push ( {
329354 t : EVENT_TYPE . CLIENT_RECV ,
330355 r : communication . transactionId ,
@@ -340,16 +365,17 @@ Collector.prototype.clientRecv = function (payload, duffelBag, briefcase) {
340365 cache . severity = severity = levels . greater ( severity , cache . severity )
341366 } )
342367
368+ if ( root ) {
369+ this . _flushCache ( cacheId )
370+ }
371+
343372 return { briefcase : assign ( { severity : severity } , briefcase ) }
344373}
345374
346375Collector . prototype . _withCache = function ( cacheId , cb , options ) {
347376 cacheId = cacheId || 'root'
348377 options = options || { }
349378 if ( this . _eventBuffers [ cacheId ] == null ) {
350- if ( options . noCreate ) {
351- return
352- }
353379 this . _eventBuffers [ cacheId ] = {
354380 severity : this . defaultSeverity ,
355381 buffer : new ExpiringBuffer ( this . _eventTtl )
@@ -358,9 +384,6 @@ Collector.prototype._withCache = function (cacheId, cb, options) {
358384 delete this . _eventBuffers [ cacheId ] . zombieTtl
359385 this . _eventBuffers [ cacheId ] . buffer = new ExpiringBuffer ( this . _eventTtl )
360386 }
361- if ( options . mode === this . CACHE_MODES . RETAIN_UNTIL_SS ) {
362- this . _eventBuffers [ cacheId ] . locked = true
363- }
364387 cb ( this . _eventBuffers [ cacheId ] )
365388}
366389
@@ -369,6 +392,32 @@ Collector.prototype._deleteCache = function (cacheId) {
369392 delete this . _eventBuffers [ cacheId ]
370393}
371394
395+ Collector . prototype . _lockCache = function ( cacheId ) {
396+ cacheId = cacheId || 'root'
397+ if ( this . _eventBuffers [ cacheId ] ) {
398+ this . _eventBuffers [ cacheId ] . locked = true
399+ }
400+ }
401+
402+ Collector . prototype . _unlockCache = function ( cacheId ) {
403+ cacheId = cacheId || 'root'
404+ if ( this . _eventBuffers [ cacheId ] ) {
405+ delete this . _eventBuffers [ cacheId ] . locked
406+ }
407+ }
408+
409+ Collector . prototype . _flushCache = function ( cacheId ) {
410+ cacheId = cacheId || 'root'
411+ if ( this . _eventBuffers [ cacheId ] ) {
412+ if ( this . _eventBuffers [ cacheId ] . severity <= this . mustCollectSeverity ) {
413+ Array . prototype . push . apply (
414+ this . _mustCollectBuffer ,
415+ this . _eventBuffers [ cacheId ] . buffer . elements ( ) )
416+ }
417+ delete this . _eventBuffers [ cacheId ]
418+ }
419+ }
420+
372421Collector . prototype . collect = function ( ) {
373422 var result = [ ]
374423 var self = this
@@ -397,20 +446,6 @@ Collector.prototype.collect = function () {
397446 return result
398447}
399448
400- Collector . prototype . end = function ( briefcase , options ) {
401- var communicationId = briefcase . communication . id
402-
403- var self = this
404- this . _withCache ( communicationId , function ( cache ) {
405- if ( ! options || ! options . skip ) {
406- if ( cache . severity <= self . mustCollectSeverity ) {
407- Array . prototype . push . apply ( self . _mustCollectBuffer , cache . buffer . elements ( ) )
408- }
409- }
410- } , { noCreate : true } )
411- this . _deleteCache ( communicationId )
412- }
413-
414449Collector . prototype . getTransactionId = function ( briefcase ) {
415450 return briefcase && briefcase . communication && briefcase . communication . transactionId
416451}
0 commit comments