Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

cmd/trace-agent: remove exit channels in favor of http.Server and context.Context #393

Merged
merged 1 commit into from
Mar 6, 2018

Conversation

gbbr
Copy link
Contributor

@gbbr gbbr commented Mar 5, 2018

In this change:

  • Removes exit channel from StoppableListener and instead just uses (*http.Server).Shutdown from the standard library (introduced in Go 1.8)
  • Removes exit channel from the agent and instead uses the more idiomatic cancellation context.
  • Adds the Windows build to CI.

@gbbr gbbr force-pushed the gbbr/no-chan branch 2 times, most recently from 0c0754f to 5273743 Compare March 5, 2018 15:07
// StoppableListener wraps a regular TCPListener with an exit channel so we can exit cleanly from the Serve() loop of our HTTP server
type StoppableListener struct {
exit chan struct{}
// RateLimitedListener wraps a regular TCPListener with an exit channel so we can exit cleanly from the Serve() loop of our HTTP server
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove reference to the exit channel, since it is no longer true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link

@bmermet bmermet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@@ -24,14 +25,14 @@ import (
)

// handleSignal closes a channel to exit cleanly from routines
func handleSignal(exit chan struct{}) {
func handleSignal(onCancel func()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the nameonCancel, it makes me think it's triggered by a cancel event when it's the other way around. onSignal or cancelFunc would seem like more natural names to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Changed to onSignal.

@@ -128,8 +127,7 @@ func (r *HTTPReceiver) Listen(addr, logExtra string) error {
return fmt.Errorf("cannot listen on %s: %v", addr, err)
}

stoppableListener, err := NewStoppableListener(listener, r.exit,
r.conf.ConnectionLimit)
stoppableListener, err := NewRateLimitedListener(listener, r.conf.ConnectionLimit)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/stoppableListener/rateLimitedListener/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooopsie...

func (r *HTTPReceiver) Stop() error {
expiry := time.Now().Add(20 * time.Second) // give it 20 seconds
ctx, _ := context.WithDeadline(context.Background(), expiry)
return r.server.Shutdown(ctx)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a much better way to Stop existing connections than before 👍

Copy link
Contributor Author

@gbbr gbbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. PTAL.

// StoppableListener wraps a regular TCPListener with an exit channel so we can exit cleanly from the Serve() loop of our HTTP server
type StoppableListener struct {
exit chan struct{}
// RateLimitedListener wraps a regular TCPListener with an exit channel so we can exit cleanly from the Serve() loop of our HTTP server
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -24,14 +25,14 @@ import (
)

// handleSignal closes a channel to exit cleanly from routines
func handleSignal(exit chan struct{}) {
func handleSignal(onCancel func()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Changed to onSignal.

@@ -128,8 +127,7 @@ func (r *HTTPReceiver) Listen(addr, logExtra string) error {
return fmt.Errorf("cannot listen on %s: %v", addr, err)
}

stoppableListener, err := NewStoppableListener(listener, r.exit,
r.conf.ConnectionLimit)
stoppableListener, err := NewRateLimitedListener(listener, r.conf.ConnectionLimit)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooopsie...

@@ -63,7 +63,7 @@ func TestReceiverRequestBodyLength(t *testing.T) {
go receiver.Run()

defer func() {
close(receiver.exit)
receiver.Stop()
// we need to wait more than on second (time for StoppableListener.Accept
// to acknowledge the connection has been closed)
time.Sleep(2 * time.Second)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't spot that on my first read. But is this Sleep still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know to be honest. The tests pass on both master and this branch without it. Do you reckon I should remove it? We could always add it back if this test turns flaky because of it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that approach makes sense, we're always better off with less code :)

// We need to manually close the receiver as the Run() func
// should have been broken and interrupted by the watchdog panic
close(agent.Receiver.exit)
agent.Receiver.Stop()
// we need to wait more than on second (time for StoppableListener.Accept
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here.

@gbbr
Copy link
Contributor Author

gbbr commented Mar 5, 2018

Removed the time.Sleep calls. Tests passed without them before, on master too. I think it's safe to not have them now. Perhaps we can address that later if the test turns out to be flaky.

@gbbr gbbr merged commit 0638620 into master Mar 6, 2018
@gbbr gbbr deleted the gbbr/no-chan branch March 6, 2018 12:20
@palazzem palazzem added the core label Mar 19, 2018
@palazzem palazzem added this to the 6.1.0 milestone Mar 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants