From 2028d8698d95fb73f0c59a548b8f2adbdf5057a4 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 28 Feb 2017 11:12:02 +0100 Subject: [PATCH] Reduce duplication in graphdriver Removes some duplication in counter.go and proxy.go Signed-off-by: Vincent Demeester --- daemon/graphdriver/counter.go | 32 +++++++++++--------------------- daemon/graphdriver/proxy.go | 24 ++++++------------------ 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/daemon/graphdriver/counter.go b/daemon/graphdriver/counter.go index b1cf953c7e..e72a097228 100644 --- a/daemon/graphdriver/counter.go +++ b/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 -} +} \ No newline at end of file diff --git a/daemon/graphdriver/proxy.go b/daemon/graphdriver/proxy.go index bfe74cc6f9..a74ef8c472 100644 --- a/daemon/graphdriver/proxy.go +++ b/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 != "" {