-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
package connection | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
// Default timeout when connecting to CSI driver. I.e. if used in a CSI sidecar container, corresponding CSI driver | ||
// must be up and running within this time. | ||
DefaultDriverConnectionTimeout = time.Minute | ||
) | ||
|
||
// Connect opens insecure gRPC connection to a CSI driver. Address must have either '<protocol>://' prefix, or be | ||
// a path to a socket file. The function tries to connect every second until timeout expires. | ||
func Connect(address string, timeout time.Duration, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) { | ||
dialOptions = append(dialOptions, | ||
grpc.WithInsecure(), // Don't use TLS, it's usually local Unix domain socket in a container. | ||
grpc.WithBlock(), // Block until it succeeds (or times out). | ||
grpc.WithBackoffMaxDelay(time.Second), // Retry every second after failure. | ||
) | ||
if strings.HasPrefix(address, "/") { | ||
// It looks like filesystem path. | ||
dialOptions = append(dialOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { | ||
return net.DialTimeout("unix", addr, timeout) | ||
})) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
return grpc.DialContext(ctx, address, dialOptions...) | ||
} |