Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Add grpc middleware for connection retry #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
name = "gopkg.in/yaml.v2"
version = "v2.1.1"

[[constraint]]
name = "github.com/grpc-ecosystem/go-grpc-middleware"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true
2 changes: 1 addition & 1 deletion cmd/csi-sanity/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_NAME := csi-sanity
VER :=$(shell git describe)
VER :=$(shell git describe --tags --dirty)
Copy link
Contributor Author

@darkowlzz darkowlzz Aug 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to some reason, just describe was returning v0.2.0 tag for me, even after pull all the latest changes. After setting --tags, it returned v0.3.0. And --dirty is nice to have :)
Hope it's fine to have these.

RELEASEVER := $(shell git describe --abbrev=0)
BRANCH := $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
SHA := $(shell git rev-parse --short HEAD)
Expand Down
16 changes: 14 additions & 2 deletions utils/grpcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (
"net/url"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/retry"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)

const maxretries int = 3

// Connect address by grpc
func Connect(address string) (*grpc.ClientConn, error) {
dialOptions := []grpc.DialOption{
Expand All @@ -41,16 +44,25 @@ func Connect(address string) (*grpc.ClientConn, error) {
}))
}

// Add retry support.
dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor()))

conn, err := grpc.Dial(address, dialOptions...)
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we really need to wait for a minute. But maybe 10 seconds would be too short. Would need help here.

defer cancel()

// Retry a few times before returning connection error.
retries := 0
for {
if !conn.WaitForStateChange(ctx, conn.GetState()) {
return conn, fmt.Errorf("Connection timed out")
retries++
if retries >= maxretries {
return conn, fmt.Errorf("Connection timed out")
}
}
if conn.GetState() == connectivity.Ready {
return conn, nil
Expand Down
201 changes: 201 additions & 0 deletions vendor/github.com/grpc-ecosystem/go-grpc-middleware/LICENSE

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

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

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

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

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

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

Loading