Skip to content

Commit 851b5d3

Browse files
committed
Add retry/backoff
1 parent 96c9cae commit 851b5d3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

main.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"strconv"
2121
"syscall"
22+
"time"
2223
)
2324

2425
func activateAttachAPI(pid int) error {
@@ -44,13 +45,26 @@ func activateAttachAPI(pid int) error {
4445
return fmt.Errorf("could not send signal 3 to activate attach API: %w", err)
4546
}
4647

47-
// TODO: Poll a few milliseconds to ensure the socket is active?
48+
// Check if the UNIX socket is active
49+
sock := socketPath(pid)
50+
for i := 1; i < 10; i++ {
51+
if _, err := os.Stat(sock); err != nil && !os.IsNotExist(err) {
52+
break
53+
}
54+
55+
// exponential backoff
56+
time.Sleep(time.Duration(1<<uint(i)) * time.Millisecond)
57+
}
4858

4959
return nil
5060
}
5161

62+
func socketPath(pid int) string {
63+
return fmt.Sprintf("/proc/%v/root/tmp/.java_pid%v", pid, pid)
64+
}
65+
5266
func connect(pid int) (*net.UnixConn, error) {
53-
sock := fmt.Sprintf("/proc/%v/root/tmp/.java_pid%v", pid, pid)
67+
sock := socketPath(pid)
5468

5569
// Check if the UNIX socket is active
5670
if _, err := os.Stat(sock); err != nil && os.IsNotExist(err) {

0 commit comments

Comments
 (0)