forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
113 lines (91 loc) · 2.49 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Example provided with help from Fatih Arslan and Gabriel Aszalos.
// Package pool manages a user defined set of resources.
package pool
import (
"errors"
"io"
"log"
"sync"
)
// Pool manages a set of resources that can be shared safely by
// multiple goroutines. The resource being managed must implement
// the io.Closer interface.
type Pool struct {
mu sync.Mutex
resources chan io.Closer
factory func() (io.Closer, error)
closed bool
}
// ErrPoolClosed is returned when an Acquire returns on a
// closed pool.
var ErrPoolClosed = errors.New("Pool has been closed")
// New creates a pool that manages resources. A pool requires a
// function that can allocate a new resource and the size of
// the pool.
func New(size uint, f func() (io.Closer, error)) (*Pool, error) {
if size == 0 {
return nil, errors.New("Size value too small")
}
return &Pool{
factory: f,
resources: make(chan io.Closer, size),
}, nil
}
// Acquire retrieves a resource from the pool.
func (p *Pool) Acquire() (io.Closer, error) {
select {
// Check for a free resource.
case r, ok := <-p.resources:
log.Println("Acquire:", "Shared Resource")
if !ok {
return nil, ErrPoolClosed
}
return r, nil
// Provide a new resource since there are none available.
default:
log.Println("Acquire:", "New Resource")
return p.factory()
}
}
// Release places a new resource onto the pool.
func (p *Pool) Release(r io.Closer) {
// Secure this operation with the Close operation.
p.mu.Lock()
defer p.mu.Unlock()
// If the pool is closed, discard the resource.
if p.closed {
r.Close()
return
}
select {
// Attempt to place the new resource on the queue.
case p.resources <- r:
log.Println("Release:", "In Queue")
// If the queue is already at cap we close the resource.
default:
log.Println("Release:", "Closing")
r.Close()
}
}
// Close will shutdown the pool and close all existing resources.
func (p *Pool) Close() error {
// Secure this operation with the Release operation.
p.mu.Lock()
defer p.mu.Unlock()
// If the pool is already close, don't do anything.
if p.closed {
return ErrPoolClosed
}
// Set the pool as closed.
p.closed = true
// Close the channel before we drain the channel of its
// resources. If we don't do this, we will have a deadlock.
close(p.resources)
// Close the resources
for r := range p.resources {
r.Close()
}
return nil
}