safesocket: add ConnectContext
This adds a variant for Connect that takes in a context.Context which allows passing through cancellation etc by the caller. Updates tailscale/corp#18266 Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
package safesocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"runtime"
|
||||
@@ -52,11 +53,14 @@ func tailscaledStillStarting() bool {
|
||||
return tailscaledProcExists()
|
||||
}
|
||||
|
||||
// Connect connects to tailscaled using a unix socket or named pipe.
|
||||
func Connect(path string) (net.Conn, error) {
|
||||
// ConnectContext connects to tailscaled using a unix socket or named pipe.
|
||||
func ConnectContext(ctx context.Context, path string) (net.Conn, error) {
|
||||
for {
|
||||
c, err := connect(path)
|
||||
c, err := connect(ctx, path)
|
||||
if err != nil && tailscaledStillStarting() {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
@@ -64,6 +68,12 @@ func Connect(path string) (net.Conn, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Connect connects to tailscaled using a unix socket or named pipe.
|
||||
// Deprecated: use ConnectContext instead.
|
||||
func Connect(path string) (net.Conn, error) {
|
||||
return ConnectContext(context.Background(), path)
|
||||
}
|
||||
|
||||
// Listen returns a listener either on Unix socket path (on Unix), or
|
||||
// the NamedPipe path (on Windows).
|
||||
func Listen(path string) (net.Listener, error) {
|
||||
|
||||
Reference in New Issue
Block a user