-
Notifications
You must be signed in to change notification settings - Fork 182
newlog: get rid of Fatal's in vector.go #5292
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,13 @@ package main | |
| import ( | ||
| "bufio" | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file" | ||
| ) | ||
|
|
@@ -25,25 +27,44 @@ var ( | |
| candidateConfigPath = "/persist/vector/config/vector.yaml.new" | ||
| ) | ||
|
|
||
| func createVectorSockets(sockPath string, backoffTime time.Duration) *net.UnixListener { | ||
| for { | ||
| // Create unix socket | ||
| if err := os.Remove(sockPath); errors.Is(err, os.ErrNotExist) { | ||
| // Socket doesn't exist, this is expected | ||
| } else if err != nil { | ||
| log.Errorf("createIncomingSockListener: Remove socket failed: %v", err) | ||
| time.Sleep(backoffTime) // wait before retry | ||
| continue | ||
| } | ||
| unixAddr, err := net.ResolveUnixAddr("unix", sockPath) | ||
| if err != nil { | ||
| log.Errorf("createIncomingSockListener: ResolveUnixAddr failed: %v", err) | ||
| time.Sleep(backoffTime) // wait before retry | ||
| continue | ||
| } | ||
| unixListener, err := net.ListenUnix("unix", unixAddr) | ||
| if err != nil { | ||
| log.Errorf("createIncomingSockListener: ListenUnix failed: %v", err) | ||
| time.Sleep(backoffTime) // wait before retry | ||
| continue | ||
| } | ||
| // Set permissions on socket | ||
| if err := os.Chmod(sockPath, 0666); err != nil { | ||
| log.Errorf("createIncomingSockListener: chmod socket failed: %v", err) | ||
| unixListener.Close() | ||
| time.Sleep(backoffTime) // wait before retry | ||
| continue | ||
| } | ||
| return unixListener | ||
| } | ||
| } | ||
|
|
||
| // listenOnSocketAndWriteToChan - goroutine to listen on unix sockets for incoming log entries | ||
| func listenOnSocketAndWriteToChan(sockPath string, sendToChan chan<- string) { | ||
| // Create unix socket | ||
| os.Remove(sockPath) // Remove any existing socket | ||
| unixAddr, err := net.ResolveUnixAddr("unix", sockPath) | ||
| if err != nil { | ||
| log.Fatalf("createIncomingSockListener: ResolveUnixAddr failed: %v", err) | ||
| } | ||
| unixListener, err := net.ListenUnix("unix", unixAddr) | ||
| if err != nil { | ||
| log.Fatalf("createIncomingSockListener: ListenUnix failed: %v", err) | ||
| } | ||
| defer unixListener.Close() | ||
| unixListener := createVectorSockets(sockPath, 10*time.Second) | ||
| defer os.Remove(sockPath) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I've just noticed it. The order of the defers is strange. First, it will delete the socket, then it will attempt to close the connection (deferred tasks are handled in LIFO order).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, fixed |
||
|
|
||
| // Set permissions on socket | ||
| if err := os.Chmod(sockPath, 0666); err != nil { | ||
| log.Fatalf("createIncomingSockListener: chmod socket failed: %v", err) | ||
| } | ||
| defer unixListener.Close() | ||
|
|
||
| // Handle socket connections | ||
| for { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) 2025 Zededa, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestCreateVectorSockets(t *testing.T) { | ||
| t.Parallel() | ||
| g := gomega.NewWithT(t) | ||
|
|
||
| // Create a temporary directory for testing | ||
| tmpDir, err := os.MkdirTemp("", "vector_socket_test") | ||
| g.Expect(err).To(gomega.BeNil()) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create a socket path in a subdirectory that doesn't exist yet | ||
| nonExistentDir := filepath.Join(tmpDir, "nonexistent") | ||
| sockPath := filepath.Join(nonExistentDir, "test.sock") | ||
|
|
||
| unixListenerChan := make(chan *net.UnixListener, 1) | ||
| backoffPeriod := 100 * time.Millisecond | ||
|
|
||
| go func() { | ||
| unixListener := createVectorSockets(sockPath, backoffPeriod) | ||
| unixListenerChan <- unixListener | ||
| }() | ||
|
|
||
| time.Sleep(2 * backoffPeriod) // Wait to ensure retries happen | ||
|
|
||
| // Verify that the socket was not created yet | ||
| _, err = os.Stat(sockPath) | ||
| g.Expect(os.IsNotExist(err)).To(gomega.BeTrue(), "Socket file should not exist yet") | ||
|
|
||
| // Now create the directory | ||
| err = os.MkdirAll(nonExistentDir, 0755) | ||
| g.Expect(err).To(gomega.BeNil()) | ||
|
|
||
| // Wait a bit to let the function succeed | ||
| var unixListener *net.UnixListener | ||
| select { | ||
| case unixListener = <-unixListenerChan: | ||
| // Successfully created the listener | ||
| case <-time.After(10 * backoffPeriod): | ||
| t.Fatal("Timeout waiting for createVectorSockets to succeed") | ||
| } | ||
|
|
||
| // verify that the listener was created | ||
| g.Expect(unixListener).ToNot(gomega.BeNil(), "createVectorSockets should succeed after directory creation") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this in a race condition with the go func lambda above?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I'll make them communicate through a channel |
||
|
|
||
| // Verify the socket was created | ||
| info, err := os.Stat(sockPath) | ||
| g.Expect(err).To(gomega.BeNil(), "Socket file should be created after directory creation") | ||
|
|
||
| expectedMode := os.FileMode(0666) | ||
| g.Expect(info.Mode().Perm()).To(gomega.Equal(expectedMode), "Socket permissions should be correct") | ||
|
|
||
| // Verify we can connect to the socket | ||
| conn, err := net.Dial("unix", sockPath) | ||
| g.Expect(err).To(gomega.BeNil(), "Should be able to connect to socket") | ||
| conn.Close() | ||
|
|
||
| t.Log("Test passed: socket created successfully and is connectable") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I thought for a minute about this and then I thought my version is more verbose and explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, I am also fine with this if you prefer it.