-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xfer: move Buffer to own file; update comment
overlay: mutex for Weave status
- Loading branch information
1 parent
a8c163b
commit ff3aae2
Showing
4 changed files
with
62 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package xfer | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// A Buffer is a reference counted bytes.Buffer, which belongs | ||
// to a sync.Pool | ||
type Buffer struct { | ||
bytes.Buffer | ||
pool *sync.Pool | ||
refs int32 | ||
} | ||
|
||
// NewBuffer creates a new buffer | ||
func NewBuffer(pool *sync.Pool) *Buffer { | ||
return &Buffer{ | ||
pool: pool, | ||
refs: 0, | ||
} | ||
} | ||
|
||
// Get increases the reference count. It is safe for concurrent calls. | ||
func (b *Buffer) Get() { | ||
atomic.AddInt32(&b.refs, 1) | ||
} | ||
|
||
// Put decreases the reference count, and when it hits zero, puts the | ||
// buffer back in the pool. | ||
func (b *Buffer) Put() { | ||
if atomic.AddInt32(&b.refs, -1) == 0 { | ||
b.Reset() | ||
b.pool.Put(b) | ||
} | ||
} | ||
|
||
// NewBufferPool creates a new buffer pool. | ||
func NewBufferPool() *sync.Pool { | ||
result := &sync.Pool{} | ||
result.New = func() interface{} { | ||
return NewBuffer(result) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters