@@ -21,6 +21,7 @@ const models = require('./models')
2121// ot
2222const ot = require ( './ot' )
2323
24+ const { ProcessQueue } = require ( './processQueue' )
2425const { RealtimeClientConnection } = require ( './realtimeClientConnection' )
2526const { UpdateDirtyNoteJob } = require ( './realtimeUpdateDirtyNoteJob' )
2627
@@ -69,6 +70,7 @@ function secure (socket, next) {
6970 }
7071}
7172
73+ // TODO: only use in `updateDirtyNote`
7274function emitCheck ( note ) {
7375 var out = {
7476 title : note . title ,
@@ -85,6 +87,9 @@ function emitCheck (note) {
8587var users = { }
8688var notes = { }
8789
90+ const disconnectProcessQueue = new ProcessQueue ( 2000 , 500 )
91+ disconnectProcessQueue . start ( )
92+
8893const updateDirtyNoteJob = new UpdateDirtyNoteJob ( realtime )
8994updateDirtyNoteJob . start ( realtime )
9095
@@ -173,8 +178,9 @@ setInterval(function () {
173178 id : key
174179 }
175180 }
176- disconnectSocketQueue . push ( socket )
177- disconnect ( socket )
181+ if ( ! disconnectProcessQueue . checkTaskIsInQueue ( socket . id ) ) {
182+ exports . queueForDisconnect ( socket )
183+ }
178184 }
179185 return callback ( null , null )
180186 } , function ( err ) {
@@ -238,8 +244,8 @@ function getStatus (callback) {
238244 distinctOnlineRegisteredUsers : distinctregaddresses . length ,
239245 isConnectionBusy : isConnectionBusy ,
240246 connectionSocketQueueLength : connectionSocketQueue . length ,
241- isDisconnectBusy : isDisconnectBusy ,
242- disconnectSocketQueueLength : disconnectSocketQueue . length
247+ isDisconnectBusy : disconnectProcessQueue . lock ,
248+ disconnectSocketQueueLength : disconnectProcessQueue . queue . length
243249 } ) : null
244250 } ) . catch ( function ( err ) {
245251 return logger . error ( 'count user failed: ' + err )
@@ -253,7 +259,7 @@ function isReady () {
253259 return realtime . io &&
254260 Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
255261 connectionSocketQueue . length === 0 && ! isConnectionBusy &&
256- disconnectSocketQueue . length === 0 && ! isDisconnectBusy
262+ disconnectProcessQueue . queue . length === 0 && ! disconnectProcessQueue . lock
257263}
258264
259265function parseUrl ( data ) {
@@ -416,8 +422,6 @@ function checkViewPermission (req, note) {
416422
417423var isConnectionBusy = false
418424var connectionSocketQueue = [ ]
419- var isDisconnectBusy = false
420- var disconnectSocketQueue = [ ]
421425
422426function finishConnection ( socket , noteId , socketId ) {
423427 // if no valid info provided will drop the client
@@ -562,71 +566,45 @@ function failConnection (code, err, socket) {
562566 return socket . disconnect ( true )
563567}
564568
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 ]
583573 }
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 ]
589580 }
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 {
599601 delete note . server
600602 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+ }
611604 }
612605 }
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+ } )
630608}
631609
632610function buildUserOutData ( user ) {
@@ -818,6 +796,11 @@ function connection (socket) {
818796 socketClient . registerEventHandler ( )
819797}
820798
799+ function terminate ( ) {
800+ disconnectProcessQueue . stop ( )
801+ updateDirtyNoteJob . stop ( )
802+ }
803+
821804exports = module . exports = realtime
822805exports . extractNoteIdFromSocket = extractNoteIdFromSocket
823806exports . parseNoteIdFromSocket = parseNoteIdFromSocket
@@ -829,7 +812,6 @@ exports.updateUserData = updateUserData
829812exports . startConnection = startConnection
830813exports . emitRefresh = emitRefresh
831814exports . emitUserStatus = emitUserStatus
832- exports . disconnect = disconnect
833815exports . emitOnlineUsers = emitOnlineUsers
834816exports . checkViewPermission = checkViewPermission
835817exports . getNoteFromNotePool = getNoteFromNotePool
@@ -838,6 +820,9 @@ exports.buildUserOutData = buildUserOutData
838820exports . getNotePool = getNotePool
839821exports . emitCheck = emitCheck
840822exports . disconnectSocketOnNote = disconnectSocketOnNote
823+ exports . queueForDisconnect = queueForDisconnect
824+ exports . terminate = terminate
825+ exports . getUserPool = getUserPool
826+ exports . disconnectProcessQueue = disconnectProcessQueue
841827exports . notes = notes
842828exports . users = users
843- exports . disconnectSocketQueue = disconnectSocketQueue
0 commit comments