-
Notifications
You must be signed in to change notification settings - Fork 151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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.
There was a problem hiding this comment.
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.