Parcourir la source

Reduce duplication in graphdriver

Removes some duplication in counter.go and proxy.go

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Vincent Demeester il y a 8 ans
Parent
commit
2028d8698d
2 fichiers modifiés avec 17 ajouts et 39 suppressions
  1. 11 21
      daemon/graphdriver/counter.go
  2. 6 18
      daemon/graphdriver/proxy.go

+ 11 - 21
daemon/graphdriver/counter.go

@@ -24,29 +24,19 @@ func NewRefCounter(c Checker) *RefCounter {
 
 // Increment increaes the ref count for the given id and returns the current count
 func (c *RefCounter) Increment(path string) int {
-	c.mu.Lock()
-	m := c.counts[path]
-	if m == nil {
-		m = &minfo{}
-		c.counts[path] = m
-	}
-	// if we are checking this path for the first time check to make sure
-	// if it was already mounted on the system and make sure we have a correct ref
-	// count if it is mounted as it is in use.
-	if !m.check {
-		m.check = true
-		if c.checker.IsMounted(path) {
-			m.count++
-		}
-	}
-	m.count++
-	count := m.count
-	c.mu.Unlock()
-	return count
+	return c.incdec(path, func(minfo *minfo) {
+		minfo.count++
+	})
 }
 
 // Decrement decreases the ref count for the given id and returns the current count
 func (c *RefCounter) Decrement(path string) int {
+	return c.incdec(path, func(minfo *minfo) {
+		minfo.count--
+	})
+}
+
+func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int {
 	c.mu.Lock()
 	m := c.counts[path]
 	if m == nil {
@@ -62,8 +52,8 @@ func (c *RefCounter) Decrement(path string) int {
 			m.count++
 		}
 	}
-	m.count--
+	infoOp(m)
 	count := m.count
 	c.mu.Unlock()
 	return count
-}
+}

+ 6 - 18
daemon/graphdriver/proxy.go

@@ -68,26 +68,14 @@ func (d *graphDriverProxy) String() string {
 }
 
 func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error {
-	args := &graphDriverRequest{
-		ID:     id,
-		Parent: parent,
-	}
-	if opts != nil {
-		args.MountLabel = opts.MountLabel
-		args.StorageOpt = opts.StorageOpt
-	}
-
-	var ret graphDriverResponse
-	if err := d.p.Client().Call("GraphDriver.CreateReadWrite", args, &ret); err != nil {
-		return err
-	}
-	if ret.Err != "" {
-		return errors.New(ret.Err)
-	}
-	return nil
+	return d.create("GraphDriver.CreateReadWrite", id, parent, opts)
 }
 
 func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
+	return d.create("GraphDriver.Create", id, parent, opts)
+}
+
+func (d *graphDriverProxy) create(method, id, parent string, opts *CreateOpts) error {
 	args := &graphDriverRequest{
 		ID:     id,
 		Parent: parent,
@@ -97,7 +85,7 @@ func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
 		args.StorageOpt = opts.StorageOpt
 	}
 	var ret graphDriverResponse
-	if err := d.p.Client().Call("GraphDriver.Create", args, &ret); err != nil {
+	if err := d.p.Client().Call(method, args, &ret); err != nil {
 		return err
 	}
 	if ret.Err != "" {