Skip to content

Use Random port for listener in collector ext #1751

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

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions collector/internal/telemetryapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,32 @@ package telemetryapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os"
"syscall"
"time"

"github.com/golang-collections/go-datastructures/queue"
"go.uber.org/zap"
)

const defaultListenerPort = "53612"
const initialQueueSize = 5
const (
initialQueueSize = 5
maxRetries = 5
// Define ephemeral port range (typical range is 49152-65535)
minPort = 49152
maxPort = 65535
)

// getRandomPort returns a random port number within the ephemeral range
func getRandomPort() string {
return fmt.Sprintf("%d", rand.Intn(maxPort-minPort)+minPort)
}

// Listener is used to listen to the Telemetry API
type Listener struct {
Expand All @@ -46,26 +60,48 @@ func NewListener(logger *zap.Logger) *Listener {
}
}

func listenOnAddress() string {
func (s *Listener) tryBindPort() (net.Listener, string, error) {
for i := 0; i < maxRetries; i++ {
port := getRandomPort()
address := listenOnAddress(port)

l, err := net.Listen("tcp", address)
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
s.logger.Debug("Port in use, trying another",
zap.String("address", address))
continue
}
return nil, "", err
}
return l, address, nil
}

return nil, "", fmt.Errorf("failed to find available port after %d attempts", maxRetries)
}

func listenOnAddress(port string) string {
envAwsLocal, ok := os.LookupEnv("AWS_SAM_LOCAL")
var addr string
if ok && envAwsLocal == "true" {
addr = ":" + defaultListenerPort
addr = ":" + port
} else {
addr = "sandbox.localdomain:" + defaultListenerPort
addr = "sandbox.localdomain:" + port
}

return addr
}

// Start the server in a goroutine where the log events will be sent
func (s *Listener) Start() (string, error) {
address := listenOnAddress()
listener, address, err := s.tryBindPort()
if err != nil {
return "", fmt.Errorf("failed to find available port: %w", err)
}
s.logger.Info("Listening for requests", zap.String("address", address))
s.httpServer = &http.Server{Addr: address}
http.HandleFunc("/", s.httpHandler)
go func() {
err := s.httpServer.ListenAndServe()
err := s.httpServer.Serve(listener)
if err != http.ErrServerClosed {
s.logger.Error("Unexpected stop on HTTP Server", zap.Error(err))
s.Shutdown()
Expand Down
Loading