-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up idle streams in UDP syslog target
The UDP transport in the syslog target creates a goroutine for each remote address to handle multiple packets coming from the same address. However, these goroutines were not cleaned up. This commit adds an idle timeout for these goroutines waiting for packets to be processed in order to prevent goroutine leaking. Signed-off-by: Christian Haudum <[email protected]>
- Loading branch information
Showing
2 changed files
with
125 additions
and
8 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,33 @@ | ||
package syslog | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIdleTimeoutConnPipe(t *testing.T) { | ||
addrs, _ := net.InterfaceAddrs() | ||
timeout := 100 * time.Millisecond | ||
|
||
p := NewIdleTimeoutConnPipe(addrs[0], timeout) | ||
// upon creation, the idle timeout is set | ||
require.False(t, p.IsIdle(time.Now())) | ||
time.Sleep(50 * time.Millisecond) | ||
require.False(t, p.IsIdle(time.Now())) | ||
|
||
// When reading or writing, the deadline is extended | ||
go func() { | ||
buf := make([]byte, 0, 1024) | ||
_, err := p.Read(buf) | ||
require.NoError(t, err) | ||
}() | ||
_, err := p.Write([]byte{104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100}) | ||
require.NoError(t, err) | ||
time.Sleep(50 * time.Millisecond) | ||
require.False(t, p.IsIdle(time.Now())) | ||
time.Sleep(80 * time.Millisecond) | ||
require.True(t, p.IsIdle(time.Now())) | ||
} |