33const mortice = require ( 'mortice' )
44const pull = require ( 'pull-stream' )
55const EventEmitter = require ( 'events' )
6- const log = require ( 'debug' ) ( 'ipfs:gc:lock ')
6+ const debug = require ( 'debug' )
77
8- class GCLock extends EventEmitter {
9- constructor ( repoOwner ) {
8+ class Lock extends EventEmitter {
9+ constructor ( repoOwner , debugName ) {
1010 super ( )
1111
12- // Ensure that we get a different mutex for each instance of GCLock
13- // (There should only be one GCLock instance per IPFS instance, but
14- // there may be multiple IPFS instances, eg in unit tests)
12+ // Ensure that we get a different mutex for each instance of Lock
1513 const randId = ( ~ ~ ( Math . random ( ) * 1e9 ) ) . toString ( 36 ) + Date . now ( )
1614 this . mutex = mortice ( randId , {
1715 singleProcess : repoOwner
1816 } )
1917
2018 this . lockId = 0
19+ this . log = debug ( debugName || 'lock' )
2120 }
2221
2322 readLock ( lockedFn , cb ) {
@@ -37,14 +36,14 @@ class GCLock extends EventEmitter {
3736 }
3837
3938 const lockId = this . lockId ++
40- log ( `[${ lockId } ] ${ type } requested` )
39+ this . log ( `[${ lockId } ] ${ type } requested` )
4140 this . emit ( `${ type } request` , lockId )
4241 const locked = ( ) => new Promise ( ( resolve , reject ) => {
4342 this . emit ( `${ type } start` , lockId )
44- log ( `[${ lockId } ] ${ type } started` )
43+ this . log ( `[${ lockId } ] ${ type } started` )
4544 lockedFn ( ( err , res ) => {
4645 this . emit ( `${ type } release` , lockId )
47- log ( `[${ lockId } ] ${ type } released` )
46+ this . log ( `[${ lockId } ] ${ type } released` )
4847 err ? reject ( err ) : resolve ( res )
4948 } )
5049 } )
@@ -62,7 +61,7 @@ class GCLock extends EventEmitter {
6261 }
6362
6463 pullLock ( type , lockedPullFn ) {
65- const pullLocker = new PullLocker ( this , this . mutex , type , this . lockId ++ )
64+ const pullLocker = new PullLocker ( this , this . mutex , type , this . lockId ++ , this . log )
6665
6766 return pull (
6867 pullLocker . take ( ) ,
@@ -73,11 +72,12 @@ class GCLock extends EventEmitter {
7372}
7473
7574class PullLocker {
76- constructor ( emitter , mutex , type , lockId ) {
75+ constructor ( emitter , mutex , type , lockId , log ) {
7776 this . emitter = emitter
7877 this . mutex = mutex
7978 this . type = type
8079 this . lockId = lockId
80+ this . log = log
8181
8282 // This Promise resolves when the mutex gives us permission to start
8383 // running the locked piece of code
@@ -91,7 +91,7 @@ class PullLocker {
9191 return new Promise ( ( resolve , reject ) => {
9292 this . releaseLock = ( err ) => err ? reject ( err ) : resolve ( )
9393
94- log ( `[${ this . lockId } ] ${ this . type } (pull) started` )
94+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) started` )
9595 this . emitter . emit ( `${ this . type } start` , this . lockId )
9696
9797 // The locked piece of code is ready to start, so resolve the
@@ -106,7 +106,7 @@ class PullLocker {
106106 return pull (
107107 pull . asyncMap ( ( i , cb ) => {
108108 if ( ! this . lock ) {
109- log ( `[${ this . lockId } ] ${ this . type } (pull) requested` )
109+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) requested` )
110110 this . emitter . emit ( `${ this . type } request` , this . lockId )
111111 // Request the lock
112112 this . lock = this . mutex [ this . type ] ( ( ) => this . locked ( ) )
@@ -125,11 +125,11 @@ class PullLocker {
125125 // Releases the lock
126126 release ( ) {
127127 return pull . through ( null , ( err ) => {
128- log ( `[${ this . lockId } ] ${ this . type } (pull) released` )
128+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) released` )
129129 this . emitter . emit ( `${ this . type } release` , this . lockId )
130130 this . releaseLock ( err )
131131 } )
132132 }
133133}
134134
135- module . exports = GCLock
135+ module . exports = Lock
0 commit comments