Skip to content

Commit

Permalink
remove unnecessary listener abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Nov 14, 2024
1 parent 863b1dd commit 080f4d3
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 68 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/subcommands/flare/command_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewSystemProbeTestServer(handler http.Handler) (*httptest.Server, error) {
return nil, err
}

server.Listener = conn.GetListener()
server.Listener = conn
return server, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/system-probe/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func StartServer(cfg *sysconfigtypes.Config, telemetry telemetry.Component, wmet
}

go func() {
err = http.Serve(conn.GetListener(), mux)
err = http.Serve(conn, mux)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("error creating HTTP server: %s", err)
}
Expand Down
11 changes: 0 additions & 11 deletions cmd/system-probe/api/server/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,3 @@

// Package server contains system-probe API server functionality
package server

import "net"

// Listener is a wrapper over some net.Listener
type Listener interface {
// GetListener returns the underlying net.Listener
GetListener() net.Listener

// Stop and clean up resources for the underlying connection
Stop()
}
4 changes: 3 additions & 1 deletion cmd/system-probe/api/server/listener_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

package server

import "net"

// NewListener is not supported
func NewListener(_ string) (Listener, error) {
func NewListener(_ string) (net.Listener, error) {
return nil, ErrNotImplemented
}
30 changes: 2 additions & 28 deletions cmd/system-probe/api/server/listener_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// UDSListener (Unix Domain Socket Listener)
type UDSListener struct {
conn net.Listener
socketPath string
}

// NewListener creates a Unix Domain Socket Listener
func NewListener(socketAddr string) (Listener, error) {
func NewListener(socketAddr string) (net.Listener, error) {
if len(socketAddr) == 0 {
return nil, errors.New("uds: empty socket path provided")
}
Expand Down Expand Up @@ -65,26 +59,6 @@ func NewListener(socketAddr string) (Listener, error) {
return nil, err
}

listener := &UDSListener{
conn: conn,
socketPath: socketAddr,
}

log.Debugf("uds: %s successfully initialized", conn.Addr())
return listener, nil
}

// GetListener will return the underlying Conn's net.Listener
func (l *UDSListener) GetListener() net.Listener {
return l.conn
}

// Stop closes the UDSListener connection and stops listening
func (l *UDSListener) Stop() {
_ = l.conn.Close()

// Socket cleanup on exit - above conn.Close() should remove it, but just in case.
if err := os.Remove(l.socketPath); err != nil {
log.Debugf("uds: error removing socket file: %s", err)
}
return conn, nil
}
4 changes: 2 additions & 2 deletions cmd/system-probe/api/server/listener_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func testSocketExistsNewUDSListener(t *testing.T, socketPath string) {
l, err := NewListener(socketPath)
require.NoError(t, err)

l.Stop()
l.Close()
}

func testSocketExistsAsRegularFileNewUDSListener(t *testing.T, socketPath string) {
Expand All @@ -45,7 +45,7 @@ func testSocketExistsAsRegularFileNewUDSListener(t *testing.T, socketPath string
func testWorkingNewUDSListener(t *testing.T, socketPath string) {
s, err := NewListener(socketPath)
require.NoError(t, err)
defer s.Stop()
defer s.Close()

assert.NoError(t, err)
assert.NotNil(t, s)
Expand Down
20 changes: 2 additions & 18 deletions cmd/system-probe/api/server/listener_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ const (
namedPipeSecurityDescriptor = "D:PAI(A;;FA;;;WD)"
)

// WindowsPipeListener for communicating with Probe
type WindowsPipeListener struct {
conn net.Listener
pipePath string
}

// systemProbePipSecurityDescriptor has the effective DACL for the system probe named pipe.
var systemProbePipSecurityDescriptor = namedPipeSecurityDescriptor

Expand All @@ -50,21 +44,11 @@ func newPipeListener(namedPipeName string) (net.Listener, error) {
}

// NewListener sets up a named pipe listener for the system probe service.
func NewListener(namedPipeName string) (Listener, error) {
func NewListener(namedPipeName string) (net.Listener, error) {
namedPipe, err := newPipeListener(namedPipeName)
if err != nil {
return nil, fmt.Errorf("error named pipe %s : %s", namedPipeName, err)
}

return &WindowsPipeListener{namedPipe, namedPipeName}, nil
}

// GetListener will return underlying Listener's conn
func (wp *WindowsPipeListener) GetListener() net.Listener {
return wp.conn
}

// Stop closes the WindowsPipeListener connection and stops listening
func (wp *WindowsPipeListener) Stop() {
_ = wp.conn.Close()
return namedPipe, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestWinCrashReporting(t *testing.T) {

listener, err := server.NewListener(systemProbeTestPipeName)
require.NoError(t, err)
defer listener.Stop()
defer listener.Close()

mux := http.NewServeMux()
server := http.Server{
Expand All @@ -76,7 +76,7 @@ func TestWinCrashReporting(t *testing.T) {
}))
mux.Handle("/debug/stats", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
}))
go server.Serve(listener.GetListener())
go server.Serve(listener)

t.Run("test that no crash detected properly reports", func(t *testing.T) {
testSetup(t)
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestCrashReportingStates(t *testing.T) {

listener, err := server.NewListener(systemProbeTestPipeName)
require.NoError(t, err)
defer listener.Stop()
defer listener.Close()

mux := http.NewServeMux()
server := http.Server{
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestCrashReportingStates(t *testing.T) {
}))
mux.Handle("/debug/stats", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
}))
go server.Serve(listener.GetListener())
go server.Serve(listener)

t.Run("test reporting a crash with a busy intermediate state", func(t *testing.T) {
testSetup(t)
Expand Down
4 changes: 2 additions & 2 deletions pkg/languagedetection/detector_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ func startTestUnixServer(t *testing.T, handler http.Handler) string {
socketPath := path.Join(t.TempDir(), "test.sock")
listener, err := server.NewListener(socketPath)
require.NoError(t, err)
t.Cleanup(listener.Stop)
t.Cleanup(func() { _ = listener.Close() })

srv := httptest.NewUnstartedServer(handler)
srv.Listener = listener.GetListener()
srv.Listener = listener
srv.Start()
t.Cleanup(srv.Close)

Expand Down

0 comments on commit 080f4d3

Please sign in to comment.