-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor code and add some documentation
- Loading branch information
Showing
8 changed files
with
224 additions
and
93 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,92 @@ | ||
package helpers | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
// Buffer is an internal buffer wrapper to ensure that Write/Read | ||
// operations are thread-safe and cannot be run at the same time. | ||
// It also starts a small goroutine that notifies about buffer | ||
// size changes through channel. See Updated method for more information. | ||
type Buffer struct { | ||
*bytes.Buffer | ||
|
||
size int | ||
lastSize int | ||
updateChan chan struct{} | ||
mu sync.Mutex | ||
} | ||
|
||
// Write implements io.Writer interface. | ||
// It ensures thread-safe execution. | ||
func (b *Buffer) Write(p []byte) (n int, err error) { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
n, err = b.Buffer.Write(p) | ||
b.size = b.Buffer.Len() | ||
return | ||
} | ||
|
||
// Next overrides bytes.Buffer method. | ||
// It ensures thread-safe execution. | ||
func (b *Buffer) Next(n int) []byte { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.Buffer.Next(n) | ||
} | ||
|
||
// ReadBytes overrides bytes.Buffer method. | ||
// It ensures thread-safe execution. | ||
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.Buffer.ReadBytes(delim) | ||
} | ||
|
||
// String overrides bytes.Buffer method. | ||
// It ensures thread-safe execution. | ||
func (b *Buffer) String() string { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.Buffer.String() | ||
} | ||
|
||
// Len overrides bytes.Buffer method. | ||
// It ensures thread-safe execution. | ||
func (b *Buffer) Len() int { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.Buffer.Len() | ||
} | ||
|
||
// Updated returns a channel that receives signal | ||
// every time buffer size gets updated. | ||
func (b *Buffer) Updated() chan struct{} { | ||
return b.updateChan | ||
} | ||
|
||
func (b *Buffer) startSizeWatcher() { | ||
go wait.Until(func() { | ||
if b.size != b.lastSize { | ||
b.updateChan <- struct{}{} | ||
b.lastSize = b.size | ||
} | ||
}, 100*time.Millisecond, wait.NeverStop) | ||
} | ||
|
||
func (b *Buffer) init() *Buffer { | ||
b.startSizeWatcher() | ||
|
||
return b | ||
} | ||
|
||
func NewBuffer() *Buffer { | ||
return (&Buffer{ | ||
Buffer: bytes.NewBuffer([]byte{}), | ||
updateChan: make(chan struct{}), | ||
}).init() | ||
} |
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
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
Oops, something went wrong.