forked from oklog/oklog
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forward: fix conflicts and accommodate ringbuffer changes from oklog#50
- Loading branch information
Showing
7 changed files
with
67 additions
and
315 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
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 |
---|---|---|
@@ -1,93 +1,37 @@ | ||
package forward | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// RingBuffer is a fixed-length ring buffer. It can be used by a forwarder, to 'drop' messages instead of applying backpressure. See Issue #15 | ||
type RingBuffer struct { | ||
type ringBuffer struct { | ||
maxSize int | ||
|
||
buf []string | ||
first int // the index of the first item in the buffer | ||
len int // the current length of the buffer | ||
ch chan struct{} // the channel is used to block on the Get method whenever data is unavailable | ||
mutex sync.RWMutex // synchronizes changes to buf, first, len | ||
ch chan string // a buffered channel is used to buffer records | ||
} | ||
|
||
// Put() processes the record without blocking. | ||
// It's behaviour varies depending on the state of the buffer and any blocking Get() invocations | ||
// It either sends the record over the channel, adds it to the buffer, or drops the record if the buffer is full. | ||
func (b *RingBuffer) Put(record string) { | ||
b.mutex.Lock() | ||
b.buf[b.last()] = record | ||
if b.len >= b.maxSize { | ||
b.inc() | ||
} else { | ||
b.len++ | ||
} | ||
b.mutex.Unlock() | ||
//notify Get method that data is available, if necessary. | ||
select { | ||
case b.ch <- struct{}{}: | ||
default: | ||
} | ||
|
||
} | ||
|
||
func (b *RingBuffer) inc() { | ||
if b.first >= b.maxSize-1 { | ||
b.first = 0 | ||
} else { | ||
b.first++ | ||
func (b *ringBuffer) Put(record string) { | ||
for { | ||
select { | ||
case b.ch <- record: | ||
return | ||
default: | ||
// when buffer full, drop oldest record | ||
<-b.ch | ||
} | ||
} | ||
} | ||
|
||
func (b *RingBuffer) last() int { | ||
r := b.len + b.first | ||
if r >= b.maxSize { | ||
r -= b.maxSize | ||
} | ||
return r | ||
} | ||
|
||
// Get() blocks until data is available | ||
func (b *RingBuffer) Get() string { | ||
var record string | ||
b.mutex.RLock() | ||
if b.len < 1 { | ||
b.mutex.RUnlock() | ||
//just block until available | ||
<-b.ch | ||
} else { | ||
b.mutex.RUnlock() | ||
} | ||
b.mutex.Lock() | ||
defer b.mutex.Unlock() | ||
record = b.buf[b.first] | ||
b.inc() | ||
b.len-- | ||
return record | ||
} | ||
|
||
// Len() is just a synchronized version of len() | ||
func (b *RingBuffer) Len() int { | ||
b.mutex.RLock() | ||
defer b.mutex.RUnlock() | ||
return b.len | ||
func (b *ringBuffer) Get() string { | ||
return <-b.ch | ||
} | ||
|
||
func NewRingBuffer(bufSize int) *RingBuffer { | ||
if bufSize < 1 { | ||
panic("buffer size should be greater than zero") | ||
func NewRingBuffer(bufSize int) *ringBuffer { | ||
if bufSize < 0 { | ||
panic("buffer size should not be less than zero") | ||
} | ||
b := &RingBuffer{ | ||
b := &ringBuffer{ | ||
maxSize: bufSize, | ||
buf: make([]string, bufSize), | ||
mutex: sync.RWMutex{}, | ||
ch: make(chan struct{}), | ||
first: 0, | ||
len: 0, | ||
ch: make(chan string, bufSize), | ||
} | ||
return b | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.