@@ -21,6 +21,7 @@ const models = require('./models')
21
21
// ot
22
22
const ot = require ( './ot' )
23
23
24
+ const { ProcessQueue } = require ( './processQueue' )
24
25
const { RealtimeClientConnection } = require ( './realtimeClientConnection' )
25
26
const { UpdateDirtyNoteJob } = require ( './realtimeUpdateDirtyNoteJob' )
26
27
@@ -69,6 +70,7 @@ function secure (socket, next) {
69
70
}
70
71
}
71
72
73
+ // TODO: only use in `updateDirtyNote`
72
74
function emitCheck ( note ) {
73
75
var out = {
74
76
title : note . title ,
@@ -85,6 +87,9 @@ function emitCheck (note) {
85
87
var users = { }
86
88
var notes = { }
87
89
90
+ const disconnectProcessQueue = new ProcessQueue ( 2000 , 500 )
91
+ disconnectProcessQueue . start ( )
92
+
88
93
const updateDirtyNoteJob = new UpdateDirtyNoteJob ( realtime )
89
94
updateDirtyNoteJob . start ( realtime )
90
95
@@ -173,8 +178,9 @@ setInterval(function () {
173
178
id : key
174
179
}
175
180
}
176
- disconnectSocketQueue . push ( socket )
177
- disconnect ( socket )
181
+ if ( ! disconnectProcessQueue . checkTaskIsInQueue ( socket . id ) ) {
182
+ exports . queueForDisconnect ( socket )
183
+ }
178
184
}
179
185
return callback ( null , null )
180
186
} , function ( err ) {
@@ -238,8 +244,8 @@ function getStatus (callback) {
238
244
distinctOnlineRegisteredUsers : distinctregaddresses . length ,
239
245
isConnectionBusy : isConnectionBusy ,
240
246
connectionSocketQueueLength : connectionSocketQueue . length ,
241
- isDisconnectBusy : isDisconnectBusy ,
242
- disconnectSocketQueueLength : disconnectSocketQueue . length
247
+ isDisconnectBusy : disconnectProcessQueue . lock ,
248
+ disconnectSocketQueueLength : disconnectProcessQueue . queue . length
243
249
} ) : null
244
250
} ) . catch ( function ( err ) {
245
251
return logger . error ( 'count user failed: ' + err )
@@ -253,7 +259,7 @@ function isReady () {
253
259
return realtime . io &&
254
260
Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
255
261
connectionSocketQueue . length === 0 && ! isConnectionBusy &&
256
- disconnectSocketQueue . length === 0 && ! isDisconnectBusy
262
+ disconnectProcessQueue . queue . length === 0 && ! disconnectProcessQueue . lock
257
263
}
258
264
259
265
function parseUrl ( data ) {
@@ -416,8 +422,6 @@ function checkViewPermission (req, note) {
416
422
417
423
var isConnectionBusy = false
418
424
var connectionSocketQueue = [ ]
419
- var isDisconnectBusy = false
420
- var disconnectSocketQueue = [ ]
421
425
422
426
function finishConnection ( socket , noteId , socketId ) {
423
427
// if no valid info provided will drop the client
@@ -562,71 +566,45 @@ function failConnection (code, err, socket) {
562
566
return socket . disconnect ( true )
563
567
}
564
568
565
- function disconnect ( socket ) {
566
- if ( isDisconnectBusy ) return
567
- isDisconnectBusy = true
568
-
569
- if ( config . debug ) {
570
- logger . info ( 'SERVER disconnected a client' )
571
- logger . info ( JSON . stringify ( users [ socket . id ] ) )
572
- }
573
-
574
- if ( users [ socket . id ] ) {
575
- delete users [ socket . id ]
576
- }
577
- var noteId = socket . noteId
578
- var note = notes [ noteId ]
579
- if ( note ) {
580
- // delete user in users
581
- if ( note . users [ socket . id ] ) {
582
- delete note . users [ socket . id ]
569
+ function queueForDisconnect ( socket ) {
570
+ disconnectProcessQueue . push ( socket . id , async function ( ) {
571
+ if ( users [ socket . id ] ) {
572
+ delete users [ socket . id ]
583
573
}
584
- // remove sockets in the note socks
585
- do {
586
- var index = note . socks . indexOf ( socket )
587
- if ( index !== - 1 ) {
588
- note . socks . splice ( index , 1 )
574
+ const noteId = socket . noteId
575
+ const note = notes [ noteId ]
576
+ if ( note ) {
577
+ // delete user in users
578
+ if ( note . users [ socket . id ] ) {
579
+ delete note . users [ socket . id ]
589
580
}
590
- } while ( index !== - 1 )
591
- // remove note in notes if no user inside
592
- if ( Object . keys ( note . users ) . length <= 0 ) {
593
- if ( note . server . isDirty ) {
594
- updateNote ( note , function ( err , _note ) {
595
- if ( err ) return logger . error ( 'disconnect note failed: ' + err )
596
- // clear server before delete to avoid memory leaks
597
- note . server . document = ''
598
- note . server . operations = [ ]
581
+ // remove sockets in the note socks
582
+ let index
583
+ do {
584
+ index = note . socks . indexOf ( socket )
585
+ if ( index !== - 1 ) {
586
+ note . socks . splice ( index , 1 )
587
+ }
588
+ } while ( index !== - 1 )
589
+ // remove note in notes if no user inside
590
+ if ( Object . keys ( note . users ) . length === 0 ) {
591
+ if ( note . server . isDirty ) {
592
+ exports . updateNote ( note , function ( err , _note ) {
593
+ if ( err ) return logger . error ( 'disconnect note failed: ' + err )
594
+ // clear server before delete to avoid memory leaks
595
+ note . server . document = ''
596
+ note . server . operations = [ ]
597
+ delete note . server
598
+ delete notes [ noteId ]
599
+ } )
600
+ } else {
599
601
delete note . server
600
602
delete notes [ noteId ]
601
- if ( config . debug ) {
602
- // logger.info(notes);
603
- getStatus ( function ( data ) {
604
- logger . info ( JSON . stringify ( data ) )
605
- } )
606
- }
607
- } )
608
- } else {
609
- delete note . server
610
- delete notes [ noteId ]
603
+ }
611
604
}
612
605
}
613
- }
614
- emitOnlineUsers ( socket )
615
-
616
- // clear finished socket in queue
617
- clearSocketQueue ( disconnectSocketQueue , socket )
618
- // seek for next socket
619
- isDisconnectBusy = false
620
- if ( disconnectSocketQueue . length > 0 ) {
621
- disconnect ( disconnectSocketQueue [ 0 ] )
622
- }
623
-
624
- if ( config . debug ) {
625
- // logger.info(notes);
626
- getStatus ( function ( data ) {
627
- logger . info ( JSON . stringify ( data ) )
628
- } )
629
- }
606
+ exports . emitOnlineUsers ( socket )
607
+ } )
630
608
}
631
609
632
610
function buildUserOutData ( user ) {
@@ -818,6 +796,11 @@ function connection (socket) {
818
796
socketClient . registerEventHandler ( )
819
797
}
820
798
799
+ function terminate ( ) {
800
+ disconnectProcessQueue . stop ( )
801
+ updateDirtyNoteJob . stop ( )
802
+ }
803
+
821
804
exports = module . exports = realtime
822
805
exports . extractNoteIdFromSocket = extractNoteIdFromSocket
823
806
exports . parseNoteIdFromSocket = parseNoteIdFromSocket
@@ -829,7 +812,6 @@ exports.updateUserData = updateUserData
829
812
exports . startConnection = startConnection
830
813
exports . emitRefresh = emitRefresh
831
814
exports . emitUserStatus = emitUserStatus
832
- exports . disconnect = disconnect
833
815
exports . emitOnlineUsers = emitOnlineUsers
834
816
exports . checkViewPermission = checkViewPermission
835
817
exports . getNoteFromNotePool = getNoteFromNotePool
@@ -838,6 +820,9 @@ exports.buildUserOutData = buildUserOutData
838
820
exports . getNotePool = getNotePool
839
821
exports . emitCheck = emitCheck
840
822
exports . disconnectSocketOnNote = disconnectSocketOnNote
823
+ exports . queueForDisconnect = queueForDisconnect
824
+ exports . terminate = terminate
825
+ exports . getUserPool = getUserPool
826
+ exports . disconnectProcessQueue = disconnectProcessQueue
841
827
exports . notes = notes
842
828
exports . users = users
843
- exports . disconnectSocketQueue = disconnectSocketQueue
0 commit comments