generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 67
Patch listener in collector ext to use random port #1099
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,89 @@ | ||
| diff --git a/collector/internal/telemetryapi/listener.go b/collector/internal/telemetryapi/listener.go | ||
| index 8499d4e..93ff0dc 100644 | ||
| --- a/collector/internal/telemetryapi/listener.go | ||
| +++ b/collector/internal/telemetryapi/listener.go | ||
| @@ -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 { | ||
| @@ -46,21 +60,44 @@ func NewListener(logger *zap.Logger) *Listener { | ||
| } | ||
| } | ||
|
|
||
| -func listenOnAddress() string { | ||
| +func (s *Listener) tryBindPort() (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 "", err | ||
| + } | ||
| + l.Close() | ||
| + return address, nil | ||
| + } | ||
| + | ||
| + return "", 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() | ||
| + 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) | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.