Skip to content

Commit

Permalink
Add Connect function
Browse files Browse the repository at this point in the history
  • Loading branch information
jsafrane committed Jan 18, 2019
1 parent 6bb407d commit 6ee8a5f
Show file tree
Hide file tree
Showing 5 changed files with 1,573 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions connection/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package connection

import (
"context"
"net"
"strings"
"time"

"github.com/golang/glog"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"google.golang.org/grpc"
)

const (
// Interval of logging connection errors
connectionLoggingInterval = 10 * time.Second
)

// Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to a socket file
// or have format '<protocol>://', following gRPC name resolution mechanism at https://github.com/grpc/grpc/blob/master/doc/naming.md.
// The function tries to connect indefinitely every second until it connects. The function automatically adds
// interceptor for gRPC message logging.
func Connect(address string, 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.WithBackoffMaxDelay(time.Second), // Retry every second after failure.
grpc.WithBlock(), // Block until connection succeeds.
grpc.WithUnaryInterceptor(LogGRPC), // Log all messages.
)
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)
}))
}
glog.Infof("Connecting to %s", address)

// Connect in background.
var conn *grpc.ClientConn
var err error
ready := make(chan bool)
go func() {
conn, err = grpc.Dial(address, dialOptions...)
close(ready)
}()

// Log error every connectionLoggingInterval
ticker := time.NewTicker(connectionLoggingInterval)
defer ticker.Stop()

// Wait until Dial() succeeds.
for {
select {
case <-ticker.C:
glog.Warningf("Still connecting to %s", address)

case <-ready:
return conn, err
}
}
}

// LogGRPC is gPRC unary interceptor for logging of CSI messages at level 5. It removes any secrets from the message.
func LogGRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
glog.V(5).Infof("GRPC call: %s", method)
glog.V(5).Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
err := invoker(ctx, method, req, reply, cc, opts...)
glog.V(5).Infof("GRPC response: %s", protosanitizer.StripSecrets(reply))
glog.V(5).Infof("GRPC error: %v", err)
return err
}
191 changes: 191 additions & 0 deletions vendor/github.com/golang/glog/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ee8a5f

Please sign in to comment.