~jameinel/juju-core/api-registry-tracks-type

« back to all changes in this revision

Viewing changes to utils/limiter.go

  • Committer: John Arbash Meinel
  • Date: 2014-04-04 09:43:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20140404094344-uzbqgftedzclwa2g
Respond to review feedback

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
type empty struct{}
11
11
type limiter chan empty
12
12
 
13
 
// Limiter represents a limited resource (eg a semaphore)
 
13
// Limiter represents a limited resource (eg a semaphore).
14
14
type Limiter interface {
15
15
        // Acquire another unit of the resource.
16
16
        // Acquire returns false to indicate there is no more availability,
17
 
        // until another entity calls Release
 
17
        // until another entity calls Release.
18
18
        Acquire() bool
19
19
        // AcquireWait requests a unit of resource, but blocks until one is
20
 
        // available
 
20
        // available.
21
21
        AcquireWait()
22
22
        // Release returns a unit of the resource. Calling Release when there
23
 
        // are no units Acquired is an error
 
23
        // are no units Acquired is an error.
24
24
        Release() error
25
25
}
26
26
 
31
31
// Acquire requests some resources that you can return later
32
32
// It returns 'true' if there are resources available, but false if they are
33
33
// not. Callers are responsible for calling Release if this returns true, but
34
 
// should not release if this returns false
 
34
// should not release if this returns false.
35
35
func (l limiter) Acquire() bool {
36
36
        e := empty{}
37
37
        select {
42
42
        }
43
43
}
44
44
 
 
45
// AcquireWait waits for the resource to become available before returning.
45
46
func (l limiter) AcquireWait() {
46
47
        e := empty{}
47
48
        l <- e
48
49
}
49
50
 
 
51
// Release returns the resource to the available pool.
50
52
func (l limiter) Release() error {
51
53
        select {
52
54
        case <-l: