3
3
const mortice = require ( 'mortice' )
4
4
const pull = require ( 'pull-stream' )
5
5
const EventEmitter = require ( 'events' )
6
- const log = require ( 'debug' ) ( 'ipfs:gc:lock ')
6
+ const debug = require ( 'debug' )
7
7
8
- class GCLock extends EventEmitter {
9
- constructor ( repoOwner ) {
8
+ class Lock extends EventEmitter {
9
+ constructor ( repoOwner , debugName ) {
10
10
super ( )
11
11
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
15
13
const randId = ( ~ ~ ( Math . random ( ) * 1e9 ) ) . toString ( 36 ) + Date . now ( )
16
14
this . mutex = mortice ( randId , {
17
15
singleProcess : repoOwner
18
16
} )
19
17
20
18
this . lockId = 0
19
+ this . log = debug ( debugName || 'lock' )
21
20
}
22
21
23
22
readLock ( lockedFn , cb ) {
@@ -37,14 +36,14 @@ class GCLock extends EventEmitter {
37
36
}
38
37
39
38
const lockId = this . lockId ++
40
- log ( `[${ lockId } ] ${ type } requested` )
39
+ this . log ( `[${ lockId } ] ${ type } requested` )
41
40
this . emit ( `${ type } request` , lockId )
42
41
const locked = ( ) => new Promise ( ( resolve , reject ) => {
43
42
this . emit ( `${ type } start` , lockId )
44
- log ( `[${ lockId } ] ${ type } started` )
43
+ this . log ( `[${ lockId } ] ${ type } started` )
45
44
lockedFn ( ( err , res ) => {
46
45
this . emit ( `${ type } release` , lockId )
47
- log ( `[${ lockId } ] ${ type } released` )
46
+ this . log ( `[${ lockId } ] ${ type } released` )
48
47
err ? reject ( err ) : resolve ( res )
49
48
} )
50
49
} )
@@ -62,7 +61,7 @@ class GCLock extends EventEmitter {
62
61
}
63
62
64
63
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 )
66
65
67
66
return pull (
68
67
pullLocker . take ( ) ,
@@ -73,11 +72,12 @@ class GCLock extends EventEmitter {
73
72
}
74
73
75
74
class PullLocker {
76
- constructor ( emitter , mutex , type , lockId ) {
75
+ constructor ( emitter , mutex , type , lockId , log ) {
77
76
this . emitter = emitter
78
77
this . mutex = mutex
79
78
this . type = type
80
79
this . lockId = lockId
80
+ this . log = log
81
81
82
82
// This Promise resolves when the mutex gives us permission to start
83
83
// running the locked piece of code
@@ -91,7 +91,7 @@ class PullLocker {
91
91
return new Promise ( ( resolve , reject ) => {
92
92
this . releaseLock = ( err ) => err ? reject ( err ) : resolve ( )
93
93
94
- log ( `[${ this . lockId } ] ${ this . type } (pull) started` )
94
+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) started` )
95
95
this . emitter . emit ( `${ this . type } start` , this . lockId )
96
96
97
97
// The locked piece of code is ready to start, so resolve the
@@ -106,7 +106,7 @@ class PullLocker {
106
106
return pull (
107
107
pull . asyncMap ( ( i , cb ) => {
108
108
if ( ! this . lock ) {
109
- log ( `[${ this . lockId } ] ${ this . type } (pull) requested` )
109
+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) requested` )
110
110
this . emitter . emit ( `${ this . type } request` , this . lockId )
111
111
// Request the lock
112
112
this . lock = this . mutex [ this . type ] ( ( ) => this . locked ( ) )
@@ -125,11 +125,11 @@ class PullLocker {
125
125
// Releases the lock
126
126
release ( ) {
127
127
return pull . through ( null , ( err ) => {
128
- log ( `[${ this . lockId } ] ${ this . type } (pull) released` )
128
+ this . log ( `[${ this . lockId } ] ${ this . type } (pull) released` )
129
129
this . emitter . emit ( `${ this . type } release` , this . lockId )
130
130
this . releaseLock ( err )
131
131
} )
132
132
}
133
133
}
134
134
135
- module . exports = GCLock
135
+ module . exports = Lock
0 commit comments