1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18
19#ifndef BRPC_RETRY_POLICY_H
20#define BRPC_RETRY_POLICY_H
21
22#include "brpc/controller.h"
23
24
25namespace brpc {
26
27// Inherit this class to customize when the RPC should be retried.
28class RetryPolicy {
29public:
30 virtual ~RetryPolicy();
31
32 // Returns true if the RPC represented by `controller' should be retried.
33 // [Example]
34 // By default, HTTP errors are not retried, but you need to retry
35 // HTTP_STATUS_FORBIDDEN in your app. You can implement the RetryPolicy
36 // as follows:
37 //
38 // class MyRetryPolicy : public brpc::RetryPolicy {
39 // public:
40 // bool DoRetry(const brpc::Controller* cntl) const {
41 // if (cntl->ErrorCode() == 0) { // don't retry successful RPC
42 // return false;
43 // }
44 // if (cntl->ErrorCode() == brpc::EHTTP && // http errors
45 // cntl->http_response().status_code() == brpc::HTTP_STATUS_FORBIDDEN) {
46 // return true;
47 // }
48 // // Leave other cases to default.
49 // return brpc::DefaultRetryPolicy()->DoRetry(cntl);
50 // }
51 // };
52 //
53 // You can retry unqualified responses even if the RPC was successful
54 // class MyRetryPolicy : public brpc::RetryPolicy {
55 // public:
56 // bool DoRetry(const brpc::Controller* cntl) const {
57 // if (cntl->ErrorCode() == 0) { // successful RPC
58 // if (!is_qualified(cntl->response())) {
59 // cntl->response()->Clear(); // reset the response
60 // return true;
61 // }
62 // return false;
63 // }
64 // // Leave other cases to default.
65 // return brpc::DefaultRetryPolicy()->DoRetry(cntl);
66 // }
67 // };
68 virtual bool DoRetry(const Controller* controller) const = 0;
69 // ^
70 // don't forget the const modifier
71};
72
73// Get the RetryPolicy used by brpc.
74const RetryPolicy* DefaultRetryPolicy();
75
76} // namespace brpc
77
78
79#endif // BRPC_RETRY_POLICY_H
80