Skip to content

Commit

Permalink
Add option to exit on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
jsafrane committed Feb 14, 2019
1 parent bd468a0 commit 4690c17
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"strings"
"time"
Expand All @@ -35,6 +36,8 @@ const (
connectionLoggingInterval = 10 * time.Second
)

const terminationLogPath = "/dev/termination-log"

// Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to UNIX domain socket
// file or have format '<protocol>://', following gRPC name resolution mechanism at
// https://github.com/grpc/grpc/blob/master/doc/naming.md.
Expand Down Expand Up @@ -63,14 +66,27 @@ type Option func(o *options)

// OnConnectionLoss registers a callback that will be invoked when the
// connection got lost. If that callback returns true, the connection
// is restablished. Otherwise the connection is left as it is and
// is reestablished. Otherwise the connection is left as it is and
// all future gRPC calls using it will fail with status.Unavailable.
func OnConnectionLoss(reconnect func() bool) Option {
return func(o *options) {
o.reconnect = reconnect
}
}

// ExitOnConnectionLoss returns callback for OnConnectionLoss() that writes
// an error to /dev/termination-log and exits.
func ExitOnConnectionLoss() func() bool {
return func() bool {
terminationMsg := "Lost connection to CSI driver, exiting"
if err := ioutil.WriteFile(terminationLogPath, []byte(terminationMsg), 0644); err != nil {
klog.Errorf("%s: %s", terminationLogPath, err)
}
klog.Fatalf(terminationMsg)
return false
}
}

type options struct {
reconnect func() bool
}
Expand Down

0 comments on commit 4690c17

Please sign in to comment.