-
Notifications
You must be signed in to change notification settings - Fork 12
/
roundrobin.h
105 lines (77 loc) · 2.47 KB
/
roundrobin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* roundrobin.h
*
* Created on: 2016年5月6日
* Author: xie
*/
#ifndef PLUGINS_EXPERIMENTAL_BALANCER_ROUNDROBIN_H_
#define PLUGINS_EXPERIMENTAL_BALANCER_ROUNDROBIN_H_
#include <stdlib.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include "balancer.h"
#define MAX_FAIL_TIME 30
#define FAIL_STATUS 500
#define OS_SINGLE 1
class RoundRobinBalancer {
public:
RoundRobinBalancer();
~RoundRobinBalancer();
void hold() {
ink_atomic_increment(&_ref_count, 1);
// TSDebug(PLUGIN_NAME,"----------hold _ref_count---------------%d",_ref_count);
}
void release() {
if (1 >= ink_atomic_decrement(&_ref_count, 1)) {
// TSDebug(PLUGIN_NAME,"----------release _ref_count---------------%d",_ref_count);
delete this;
}
}
void push_target(BalancerTarget *target);
//获取一个后端
BalancerTarget *balance(bool follow_https);
//清除peer 的fails 和 timeout_fails状态
void clean_peer_status();
//首先给down状态下的服务器一次机会 now - check >= fail_timeout * timeout_fails
//如果主还有存活的,就不用考虑down状态下冷却超时的备用,只有当主都不存活,才考虑
BalancerTarget *get_down_timeout_peer(time_t now);
//获取最优的target 此处参考nginx rr 算法
BalancerTarget *get_healthy_peer(std::vector<BalancerTarget *> &targets, time_t now);
//更改后端状态,后端返回5xx,就认为失败
TSReturnCode os_response_back_status(uint target_id, TSHttpStatus status);
BalancerTarget * MakeBalancerTarget(const char *strval);
void set_path(char *path) {
this->path = path;
}
char *get_path() const {
return this->path;
}
void set_backend_tag(bool is_need, bool is_need_health_check, bool is_follow_model) {
this->need_https_backend = is_need;
this->need_health_check = is_need_health_check;
this->follow_model = is_follow_model;
}
bool get_https_backend_tag() {
return this->need_https_backend;
}
bool get_health_check_tag() {
return this->need_health_check;
}
bool is_need_follow_model() {
return this->follow_model;
}
private:
std::vector<BalancerTarget *> targets_s; //主线路 如果是follow模式 为http源
std::vector<BalancerTarget *> targets_b; //备用线路 如果是follow模式 为https源
uint peersS_number;
uint peersB_number;
unsigned next;
char *path;
bool need_https_backend;
bool need_health_check;
bool follow_model;
volatile int _ref_count;
};
#endif /* PLUGINS_EXPERIMENTAL_BALANCER_ROUNDROBIN_H_ */