Reduce duplication in graphdriver

Removes some duplication in counter.go and proxy.go

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-02-28 11:12:02 +01:00
parent cb6832c6d3
commit 2028d8698d
No known key found for this signature in database
GPG Key ID: 083CC6FD6EB699A3
2 changed files with 17 additions and 39 deletions

View File

@ -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
}
}

View File

@ -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 != "" {