forked from telepresenceio/telepresence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.go
58 lines (50 loc) · 1.96 KB
/
sockets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package client
import (
"context"
"fmt"
"net"
"time"
"google.golang.org/grpc"
)
// DialSocket dials the given socket and returns the resulting connection
func DialSocket(ctx context.Context, socketName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return dialSocket(ctx, socketName, opts...)
}
// ListenSocket returns a listener for the given socket and returns the resulting connection
func ListenSocket(ctx context.Context, processName, socketName string) (net.Listener, error) {
return listenSocket(ctx, processName, socketName)
}
// RemoveSocket removes any representation of the socket from the filesystem.
func RemoveSocket(listener net.Listener) error {
return removeSocket(listener)
}
// SocketExists returns true if a socket is found with the given name
func SocketExists(name string) (bool, error) {
return socketExists(name)
}
// WaitUntilSocketVanishes waits until the socket at the given path is removed
// and returns when that happens. The wait will be max ttw (time to wait) long.
// An error is returned if that time is exceeded before the socket is removed.
func WaitUntilSocketVanishes(name, path string, ttw time.Duration) error {
giveUp := time.Now().Add(ttw)
for giveUp.After(time.Now()) {
if exists, err := SocketExists(path); err != nil || !exists {
return err
}
time.Sleep(250 * time.Millisecond)
}
return fmt.Errorf("timeout while waiting for %s to exit", name)
}
// WaitUntilSocketAppears waits until the socket at the given path comes into
// existence and returns when that happens. The wait will be max ttw (time to wait) long.
// An error is returned if that time is exceeded before the socket is removed.
func WaitUntilSocketAppears(name, path string, ttw time.Duration) error {
giveUp := time.Now().Add(ttw)
for giveUp.After(time.Now()) {
if exists, err := SocketExists(path); err != nil || exists {
return err
}
time.Sleep(250 * time.Millisecond)
}
return fmt.Errorf("timeout while waiting for %s to start", name)
}