The current interface only allows Incrementing and Decrementing, but does not allow getting the current reference-count. As a result, we need to do implement confusing code to get the current count;
// Check if the refcount is non-zero.
m.rc.Increment(target)
if m.rc.Decrement(target) > 0 {
// it's in use
}
We should look at a cleaner way to get this information without having to increment/decrement. Perhaps we don't need the actual count (only if in use?), but we can discuss that when working on this.
|
// RefCounter is a generic counter for use by graphdriver Get/Put calls |
|
type RefCounter struct { |
|
counts map[string]*minfo |
|
mu sync.Mutex |
|
checker Checker |
|
} |
|
|
|
// NewRefCounter returns a new RefCounter |
|
func NewRefCounter(c Checker) *RefCounter { |
|
return &RefCounter{ |
|
checker: c, |
|
counts: make(map[string]*minfo), |
|
} |
|
} |
|
|
|
// Increment increases the ref count for the given id and returns the current count |
|
func (c *RefCounter) Increment(path string) int { |
|
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-- |
|
}) |
|
} |
The current interface only allows Incrementing and Decrementing, but does not allow getting the current reference-count. As a result, we need to do implement confusing code to get the current count;
We should look at a cleaner way to get this information without having to increment/decrement. Perhaps we don't need the actual count (only if in use?), but we can discuss that when working on this.
moby/daemon/graphdriver/counter.go
Lines 10 to 37 in 54fcd40