Remove fsrepo refcounting
No code attempts to open the same repo multiple times. Error handling is still buggy, but it's starting to get clearer.
Tommi Virtanen committed
Mar 13, 2015 at 15:24 UTC
431cf1c902ce7f25036e6c50236b09898aa39d8f
2 files changed
+7
-69
repo/fsrepo/counter/openers.go
deleted
-43
@@ -1,43 +0,0 @@
1
-package counter
2
-
3
-import "path"
4
-
5
-// TODO this could be made into something more generic.
6
-
7
-type Openers struct {
8
- // repos maps repo paths to the number of openers holding an FSRepo handle
9
- // to it
10
- repos map[string]int
11
-}
12
-
13
-func NewOpenersCounter() *Openers {
14
- return &Openers{
15
- repos: make(map[string]int),
16
- }
17
-}
18
-
19
-// NumOpeners returns the number of FSRepos holding a handle to the repo at
20
-// this path. This method is not thread-safe. The caller must have this object
21
-// locked.
22
-func (l *Openers) NumOpeners(repoPath string) int {
23
- return l.repos[key(repoPath)]
24
-}
25
-
26
-// AddOpener messages that an FSRepo holds a handle to the repo at this path.
27
-// This method is not thread-safe. The caller must have this object locked.
28
-func (l *Openers) AddOpener(repoPath string) error {
29
- l.repos[key(repoPath)]++
30
- return nil
31
-}
32
-
33
-// RemoveOpener messgaes that an FSRepo no longer holds a handle to the repo at
34
-// this path. This method is not thread-safe. The caller must have this object
35
-// locked.
36
-func (l *Openers) RemoveOpener(repoPath string) error {
37
- l.repos[key(repoPath)]--
38
- return nil
39
-}
40
-
41
-func key(repoPath string) string {
42
- return path.Clean(repoPath)
43
-}
repo/fsrepo/fsrepo.go
+7
-26
@@ -14,7 +14,6 @@ import (
14
repo "github.com/jbenet/go-ipfs/repo"
15
"github.com/jbenet/go-ipfs/repo/common"
16
config "github.com/jbenet/go-ipfs/repo/config"
17
- counter "github.com/jbenet/go-ipfs/repo/fsrepo/counter"
17
lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
18
serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
19
dir "github.com/jbenet/go-ipfs/thirdparty/dir"
@@ -33,14 +32,7 @@ var (
32
33
// packageLock must be held to while performing any operation that modifies an
34
// FSRepo's state field. This includes Init, Open, Close, and Remove.
36
- packageLock sync.Mutex // protects openersCounter
37
- // openersCounter ensures that the Init is atomic.
38
- //
39
- // packageLock also protects numOpenedRepos
40
- //
41
- // If an operation is used when repo is Open and the operation does not
42
- // change the repo's state, the package lock does not need to be acquired.
43
- openersCounter *counter.Openers
35
+ packageLock sync.Mutex
36
37
// onlyOne keeps track of open FSRepo instances.
38
//
@@ -57,10 +49,6 @@ var (
49
onlyOne repo.OnlyOne
50
)
51
60
-func init() {
61
- openersCounter = counter.NewOpenersCounter()
62
-}
63
-
52
// FSRepo represents an IPFS FileSystem Repo. It is safe for use by multiple
53
// callers.
54
type FSRepo struct {
@@ -444,27 +432,20 @@ func isInitializedUnsynced(repoPath string) bool {
432
// the package mutex.
433
func (r *FSRepo) transitionToOpened() error {
434
r.state = opened
447
- if countBefore := openersCounter.NumOpeners(r.path); countBefore == 0 { // #first
448
- closer, err := lockfile.Lock(r.path)
449
- if err != nil {
450
- return err
451
- }
452
- r.lockfile = closer
435
+ closer, err := lockfile.Lock(r.path)
436
+ if err != nil {
437
+ return err
438
}
454
- return openersCounter.AddOpener(r.path)
439
+ r.lockfile = closer
440
+ return nil
441
}
442
443
// transitionToClosed manages the state transition to |closed|. Caller must
444
// hold the package mutex.
445
func (r *FSRepo) transitionToClosed() error {
446
r.state = closed
461
- if err := openersCounter.RemoveOpener(r.path); err != nil {
447
+ if err := r.lockfile.Close(); err != nil {
448
return err
449
}
464
- if countAfter := openersCounter.NumOpeners(r.path); countAfter == 0 {
465
- if err := r.lockfile.Close(); err != nil {
466
- return err
467
- }
468
- }
450
return nil
451
}