-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ListenerSink to allow users to watch events passing through sinks #25
Open
halcyonCorsair
wants to merge
1
commit into
docker:main
Choose a base branch
from
halcyonCorsair:halcyonCorsair/add-listener-sink-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package events | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// ListenFunc is a type for listener functions must implement | ||
type ListenFunc func(event Event) | ||
|
||
// ListenerSink passes events through ListenFunc before draining through the | ||
// next sink below | ||
type ListenerSink struct { | ||
dst Sink | ||
mu sync.Mutex | ||
closed bool | ||
ListenFunc | ||
} | ||
|
||
// NewListenerSink returns a sink that will pass events to a listener | ||
func NewListenerSink(dst Sink, fn ListenFunc) *ListenerSink { | ||
ls := &ListenerSink{ | ||
dst: dst, | ||
ListenFunc: fn, | ||
closed: false, | ||
} | ||
return ls | ||
} | ||
|
||
// Write passes the event to a listener and then forwards to the next sink | ||
func (ls *ListenerSink) Write(event Event) error { | ||
ls.mu.Lock() | ||
defer ls.mu.Unlock() | ||
|
||
if ls.closed { | ||
return ErrSinkClosed | ||
} | ||
|
||
ls.ListenFunc(event) | ||
|
||
return ls.dst.Write(event) | ||
} | ||
|
||
// Close calls Close() on the sink below | ||
func (ls *ListenerSink) Close() error { | ||
if ls.closed { | ||
return nil | ||
} | ||
|
||
// set closed flag | ||
ls.closed = true | ||
return ls.dst.Close() | ||
} |
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,67 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestListener(t *testing.T) { | ||
const nevents = 100 | ||
tm := &testMetrics{0, 0, 0} | ||
|
||
ts := newTestSink(t, nevents) | ||
es := NewListenerSink(ts, tm.egress) | ||
eq := NewQueue(es) | ||
is := NewListenerSink(eq, tm.ingress) | ||
|
||
var wg sync.WaitGroup | ||
for i := 1; i <= nevents; i++ { | ||
wg.Add(1) | ||
go func(event Event) { | ||
if err := is.Write(event); err != nil { | ||
t.Fatalf("error writing event: %v", err) | ||
} | ||
wg.Done() | ||
}("event-" + fmt.Sprint(i)) | ||
} | ||
wg.Wait() | ||
checkClose(t, is) | ||
|
||
ts.mu.Lock() | ||
defer ts.mu.Unlock() | ||
|
||
t.Logf("%#v", tm) | ||
|
||
if len(ts.events) != nevents { | ||
t.Fatalf("events did not make it to the sink: %d != %d", len(ts.events), 1000) | ||
} | ||
|
||
if tm.events != nevents && tm.incoming != nevents && tm.outgoing != nevents { | ||
t.Fatalf("events, incoming, outgoing should all == %d, %#v", nevents, tm) | ||
} | ||
|
||
if !ts.closed { | ||
t.Fatalf("sink should have been closed") | ||
} | ||
// t.Fatalf("%#v", tm) | ||
} | ||
|
||
type endSink struct { | ||
Sink | ||
} | ||
|
||
type testMetrics struct { | ||
events int | ||
incoming int | ||
outgoing int | ||
} | ||
|
||
func (m *testMetrics) ingress(event Event) { | ||
m.events++ | ||
m.incoming++ | ||
} | ||
|
||
func (m *testMetrics) egress(event Event) { | ||
m.outgoing++ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to remove this line?