1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 // Increment increaes the ref count for the given id and returns the current count
func (c *RefCounter) Increment(path string) int { func (c *RefCounter) Increment(path string) int {
c.mu.Lock() return c.incdec(path, func(minfo *minfo) {
m := c.counts[path] minfo.count++
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
} }
// Decrement decreases the ref count for the given id and returns the current count // Decrement decreases the ref count for the given id and returns the current count
func (c *RefCounter) Decrement(path string) int { 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() c.mu.Lock()
m := c.counts[path] m := c.counts[path]
if m == nil { if m == nil {
@ -62,7 +52,7 @@ func (c *RefCounter) Decrement(path string) int {
m.count++ m.count++
} }
} }
m.count-- infoOp(m)
count := m.count count := m.count
c.mu.Unlock() c.mu.Unlock()
return count return count

View file

@ -68,26 +68,14 @@ func (d *graphDriverProxy) String() string {
} }
func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error { func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error {
args := &graphDriverRequest{ return d.create("GraphDriver.CreateReadWrite", id, parent, opts)
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
} }
func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error { 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{ args := &graphDriverRequest{
ID: id, ID: id,
Parent: parent, Parent: parent,
@ -97,7 +85,7 @@ func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
args.StorageOpt = opts.StorageOpt args.StorageOpt = opts.StorageOpt
} }
var ret graphDriverResponse 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 return err
} }
if ret.Err != "" { if ret.Err != "" {