You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[`WithBlock`](https://pkg.go.dev/google.golang.org/grpc#WithBlock) in an instance where WithBlock(true) is used, `Connect` and `WaitForStateChange` is invoked until either the context created via `context.WithTimeout` expires or the `ClientConn` is ready.
42
60
43
61
### Using `FailOnNonTempDialError`, `WithBlock`, and `WithReturnConnectionError`
44
62
45
63
The gRPC API provides several options that can be used to configure the behavior
46
-
of dialing and connecting to a gRPC server. Some of these options, such as
64
+
of dialing and connecting to a gRPC server. Some of these options, such as
47
65
`FailOnNonTempDialError`, `WithBlock`, and `WithReturnConnectionError`, rely on
48
-
failures at dial time. However, we strongly discourage developers from using
66
+
failures at dial time. However, we strongly discourage developers from using
49
67
these options, as they can introduce race conditions and result in unreliable
50
68
and difficult-to-debug code.
51
69
52
70
One of the most important reasons for avoiding these options, which is often
53
-
overlooked, is that connections can fail at any point in time. This means that
71
+
overlooked, is that connections can fail at any point in time. This means that
54
72
you need to handle RPC failures caused by connection issues, regardless of
55
73
whether a connection was never established in the first place, or if it was
56
74
created and then immediately lost. Implementing proper error handling for RPCs
57
75
is crucial for maintaining the reliability and stability of your gRPC
58
76
communication.
59
77
60
-
### Difference between Dial and NewClient
61
-
[`grpc.NewClient`](https://pkg.go.dev/google.golang.org/grpc#NewClient) is a function in the grpc libaray that creates a new gRPC `channel` for the target URI that is passed in as an argument, together with a list of `DialOption`, and returns [`ClientConn`](https://pkg.go.dev/google.golang.org/grpc#ClientConn) an object representing a server connection.
62
-
63
-
Unlike `grpc.NewClient`, whereby using the ClientConn for RPCs will automatically cause it to connect or `Connect` may be used to manually create a connection, by default `Dial` does not always establish a connection to servers. Connection behavior is determined by the load balancing policy used.
64
-
65
-
`grpc.NewClient` automatically ignores `DialOptions` returned by `WithBlock`, `WithTimeout`, `WithReturnConnectionError`, and `FailOnNonTempDialError.
66
-
67
-
`grpc.NewClient` uses passthrough as the default name resolver for backward compatibility while `Dial` uses dns as its default name resolver. This subtle diffrence is crucial in legacy systems that specify a custom dialer and expect it to receive the target string directly.
68
-
69
-
Timeouts are not supported by `grpc.NewClient`.
70
-
71
78
### Why we discourage using `FailOnNonTempDialError`, `WithBlock`, and `WithReturnConnectionError`
72
79
73
80
When a client attempts to connect to a gRPC server, it can encounter a variety
74
81
of errors, including network connectivity issues, server-side errors, and
75
-
incorrect usage of the gRPC API. The options `FailOnNonTempDialError`,
82
+
incorrect usage of the gRPC API. The options `FailOnNonTempDialError`,
76
83
`WithBlock`, and `WithReturnConnectionError` are designed to handle some of
77
-
these errors, but they do so by relying on failures at dial time. This means
84
+
these errors, but they do so by relying on failures at dial time. This means
78
85
that they may not provide reliable or accurate information about the status of
79
86
the connection.
80
87
@@ -87,26 +94,26 @@ network issues that are resolved shortly after the initial dial attempt.
87
94
## Best practices for error handling in gRPC
88
95
89
96
Instead of relying on failures at dial time, we strongly encourage developers to
90
-
rely on errors from RPCs. When a client makes an RPC, it can receive an error
91
-
response from the server. These errors can provide valuable information about
97
+
rely on errors from RPCs. When a client makes an RPC, it can receive an error
98
+
response from the server. These errors can provide valuable information about
92
99
what went wrong, including information about network issues, server-side errors,
93
100
and incorrect usage of the gRPC API.
94
101
95
102
By handling errors from RPCs correctly, developers can write more reliable and
96
-
robust gRPC applications. Here are some best practices for error handling in
103
+
robust gRPC applications. Here are some best practices for error handling in
97
104
gRPC:
98
105
99
-
- Always check for error responses from RPCs and handle them appropriately.
100
-
- Use the `status` field of the error response to determine the type of error that
101
-
occurred.
106
+
- Always check for error responses from RPCs and handle them appropriately.
107
+
- Use the `status` field of the error response to determine the type of error
108
+
that occurred.
102
109
- When retrying failed RPCs, consider using the built-in retry mechanism
103
110
provided by gRPC-Go, if available, instead of manually implementing retries.
0 commit comments