retry.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. )
  20. type rpcFunc func(ctx context.Context) error
  21. type retryRpcFunc func(context.Context, rpcFunc)
  22. func (c *Client) newRetryWrapper() retryRpcFunc {
  23. return func(rpcCtx context.Context, f rpcFunc) {
  24. for {
  25. err := f(rpcCtx)
  26. // ignore grpc conn closing on fail-fast calls; they are transient errors
  27. if err == nil || !isConnClosing(err) {
  28. return
  29. }
  30. select {
  31. case <-c.balancer.ConnectNotify():
  32. case <-rpcCtx.Done():
  33. case <-c.ctx.Done():
  34. return
  35. }
  36. }
  37. }
  38. }
  39. type retryKVClient struct {
  40. pb.KVClient
  41. retryf retryRpcFunc
  42. }
  43. // RetryKVClient implements a KVClient that uses the client's FailFast retry policy.
  44. func RetryKVClient(c *Client) pb.KVClient {
  45. return &retryKVClient{pb.NewKVClient(c.conn), c.retryWrapper}
  46. }
  47. func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
  48. rkv.retryf(ctx, func(rctx context.Context) error {
  49. resp, err = rkv.KVClient.Put(rctx, in, opts...)
  50. return err
  51. })
  52. return resp, err
  53. }
  54. func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
  55. rkv.retryf(ctx, func(rctx context.Context) error {
  56. resp, err = rkv.KVClient.DeleteRange(rctx, in, opts...)
  57. return err
  58. })
  59. return resp, err
  60. }
  61. func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
  62. rkv.retryf(ctx, func(rctx context.Context) error {
  63. resp, err = rkv.KVClient.Txn(rctx, in, opts...)
  64. return err
  65. })
  66. return resp, err
  67. }
  68. func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
  69. rkv.retryf(ctx, func(rctx context.Context) error {
  70. resp, err = rkv.KVClient.Compact(rctx, in, opts...)
  71. return err
  72. })
  73. return resp, err
  74. }
  75. type retryLeaseClient struct {
  76. pb.LeaseClient
  77. retryf retryRpcFunc
  78. }
  79. // RetryLeaseClient implements a LeaseClient that uses the client's FailFast retry policy.
  80. func RetryLeaseClient(c *Client) pb.LeaseClient {
  81. return &retryLeaseClient{pb.NewLeaseClient(c.conn), c.retryWrapper}
  82. }
  83. func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
  84. rlc.retryf(ctx, func(rctx context.Context) error {
  85. resp, err = rlc.LeaseClient.LeaseGrant(rctx, in, opts...)
  86. return err
  87. })
  88. return resp, err
  89. }
  90. func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
  91. rlc.retryf(ctx, func(rctx context.Context) error {
  92. resp, err = rlc.LeaseClient.LeaseRevoke(rctx, in, opts...)
  93. return err
  94. })
  95. return resp, err
  96. }
  97. type retryClusterClient struct {
  98. pb.ClusterClient
  99. retryf retryRpcFunc
  100. }
  101. // RetryClusterClient implements a ClusterClient that uses the client's FailFast retry policy.
  102. func RetryClusterClient(c *Client) pb.ClusterClient {
  103. return &retryClusterClient{pb.NewClusterClient(c.conn), c.retryWrapper}
  104. }
  105. func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
  106. rcc.retryf(ctx, func(rctx context.Context) error {
  107. resp, err = rcc.ClusterClient.MemberAdd(rctx, in, opts...)
  108. return err
  109. })
  110. return resp, err
  111. }
  112. func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
  113. rcc.retryf(ctx, func(rctx context.Context) error {
  114. resp, err = rcc.ClusterClient.MemberRemove(rctx, in, opts...)
  115. return err
  116. })
  117. return resp, err
  118. }
  119. func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
  120. rcc.retryf(ctx, func(rctx context.Context) error {
  121. resp, err = rcc.ClusterClient.MemberUpdate(rctx, in, opts...)
  122. return err
  123. })
  124. return resp, err
  125. }
  126. type retryAuthClient struct {
  127. pb.AuthClient
  128. retryf retryRpcFunc
  129. }
  130. // RetryAuthClient implements a AuthClient that uses the client's FailFast retry policy.
  131. func RetryAuthClient(c *Client) pb.AuthClient {
  132. return &retryAuthClient{pb.NewAuthClient(c.conn), c.retryWrapper}
  133. }
  134. func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
  135. rac.retryf(ctx, func(rctx context.Context) error {
  136. resp, err = rac.AuthClient.AuthEnable(rctx, in, opts...)
  137. return err
  138. })
  139. return resp, err
  140. }
  141. func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
  142. rac.retryf(ctx, func(rctx context.Context) error {
  143. resp, err = rac.AuthClient.AuthDisable(rctx, in, opts...)
  144. return err
  145. })
  146. return resp, err
  147. }
  148. func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
  149. rac.retryf(ctx, func(rctx context.Context) error {
  150. resp, err = rac.AuthClient.UserAdd(rctx, in, opts...)
  151. return err
  152. })
  153. return resp, err
  154. }
  155. func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
  156. rac.retryf(ctx, func(rctx context.Context) error {
  157. resp, err = rac.AuthClient.UserDelete(rctx, in, opts...)
  158. return err
  159. })
  160. return resp, err
  161. }
  162. func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
  163. rac.retryf(ctx, func(rctx context.Context) error {
  164. resp, err = rac.AuthClient.UserChangePassword(rctx, in, opts...)
  165. return err
  166. })
  167. return resp, err
  168. }
  169. func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
  170. rac.retryf(ctx, func(rctx context.Context) error {
  171. resp, err = rac.AuthClient.UserGrantRole(rctx, in, opts...)
  172. return err
  173. })
  174. return resp, err
  175. }
  176. func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
  177. rac.retryf(ctx, func(rctx context.Context) error {
  178. resp, err = rac.AuthClient.UserRevokeRole(rctx, in, opts...)
  179. return err
  180. })
  181. return resp, err
  182. }
  183. func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
  184. rac.retryf(ctx, func(rctx context.Context) error {
  185. resp, err = rac.AuthClient.RoleAdd(rctx, in, opts...)
  186. return err
  187. })
  188. return resp, err
  189. }
  190. func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
  191. rac.retryf(ctx, func(rctx context.Context) error {
  192. resp, err = rac.AuthClient.RoleDelete(rctx, in, opts...)
  193. return err
  194. })
  195. return resp, err
  196. }
  197. func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
  198. rac.retryf(ctx, func(rctx context.Context) error {
  199. resp, err = rac.AuthClient.RoleGrantPermission(rctx, in, opts...)
  200. return err
  201. })
  202. return resp, err
  203. }
  204. func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
  205. rac.retryf(ctx, func(rctx context.Context) error {
  206. resp, err = rac.AuthClient.RoleRevokePermission(rctx, in, opts...)
  207. return err
  208. })
  209. return resp, err
  210. }