-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* emit event for every line, buffering if necessary. * fix memory corruption (stop sharing bufer) * delivery timeout after 1ms fixes #16
- Loading branch information
Showing
5 changed files
with
160 additions
and
6 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,52 @@ | ||
package kail | ||
|
||
import "bytes" | ||
|
||
const bufferMaxRetainSize = logBufsiz | ||
|
||
type buffer interface { | ||
process([]byte) []Event | ||
} | ||
|
||
type _buffer struct { | ||
source EventSource | ||
prev *bytes.Buffer | ||
} | ||
|
||
func newBuffer(source EventSource) buffer { | ||
return &_buffer{source, new(bytes.Buffer)} | ||
} | ||
|
||
func (b *_buffer) process(log []byte) []Event { | ||
|
||
var events []Event | ||
|
||
for end := bytes.IndexRune(log, '\n'); end >= 0 && len(log) > 0; end = bytes.IndexRune(log, '\n') { | ||
var ebuf []byte | ||
|
||
if plen := b.prev.Len(); plen > 0 { | ||
ebuf = make([]byte, plen+end) | ||
copy(ebuf, b.prev.Bytes()) | ||
copy(ebuf[plen:], log[:end]) | ||
b.prev.Reset() | ||
} else { | ||
ebuf = make([]byte, end) | ||
copy(ebuf, log[:end]) | ||
} | ||
|
||
events = append(events, newEvent(b.source, ebuf)) | ||
log = log[end+1:] | ||
} | ||
|
||
if sz := len(log); sz > 0 { | ||
b.prev.Write(log) | ||
if plen := b.prev.Len(); plen >= bufferMaxRetainSize { | ||
ebuf := make([]byte, plen) | ||
copy(ebuf, b.prev.Bytes()) | ||
events = append(events, newEvent(b.source, ebuf)) | ||
b.prev.Reset() | ||
} | ||
} | ||
|
||
return events | ||
} |
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,66 @@ | ||
package kail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuffer(t *testing.T) { | ||
source := eventSource{} | ||
|
||
{ | ||
buffer := newBuffer(source) | ||
events := buffer.process([]byte("")) | ||
assert.Empty(t, events) | ||
|
||
events = buffer.process([]byte("foo\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "foo", string(events[0].Log())) | ||
} | ||
|
||
{ | ||
buffer := newBuffer(source) | ||
events := buffer.process([]byte("foo")) | ||
assert.Empty(t, events) | ||
|
||
events = buffer.process([]byte("\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "foo", string(events[0].Log())) | ||
} | ||
|
||
{ | ||
buffer := newBuffer(source) | ||
events := buffer.process([]byte("foo\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "foo", string(events[0].Log())) | ||
|
||
events = buffer.process([]byte("bar\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "bar", string(events[0].Log())) | ||
} | ||
|
||
{ | ||
buffer := newBuffer(source) | ||
events := buffer.process([]byte("foo\nbar\n")) | ||
assert.Len(t, events, 2) | ||
assert.Equal(t, "foo", string(events[0].Log())) | ||
assert.Equal(t, "bar", string(events[1].Log())) | ||
|
||
events = buffer.process([]byte("baz\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "baz", string(events[0].Log())) | ||
} | ||
|
||
{ | ||
buffer := newBuffer(source) | ||
events := buffer.process([]byte("foo\nbar")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "foo", string(events[0].Log())) | ||
|
||
events = buffer.process([]byte("baz\n")) | ||
assert.Len(t, events, 1) | ||
assert.Equal(t, "barbaz", string(events[0].Log())) | ||
} | ||
|
||
} |
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