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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <uwsgi.h>
#include "cr.h"
extern struct uwsgi_server uwsgi;
int uwsgi_cr_map_use_void(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
return 0;
}
int uwsgi_cr_map_use_cache(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
uint64_t hits = 0;
uwsgi_rlock(ucr->cache->lock);
char *value = uwsgi_cache_get4(ucr->cache, peer->key, peer->key_len, &peer->instance_address_len, &hits);
if (!value)
goto end;
peer->tmp_socket_name = uwsgi_concat2n(value, peer->instance_address_len, "", 0);
size_t nodes = uwsgi_str_occurence(peer->tmp_socket_name, peer->instance_address_len, '|');
if (nodes > 0) {
size_t choosen_node = hits % (nodes + 1);
size_t choosen_node_len = 0;
peer->instance_address = uwsgi_str_split_nget(peer->tmp_socket_name, peer->instance_address_len, '|', choosen_node, &choosen_node_len);
if (!peer->instance_address)
goto end;
peer->instance_address_len = choosen_node_len;
}
else {
peer->instance_address = peer->tmp_socket_name;
}
char *cs_mod = uwsgi_str_contains(peer->instance_address, peer->instance_address_len, ',');
if (cs_mod) {
peer->modifier1 = uwsgi_str_num(cs_mod + 1, (peer->instance_address_len - (cs_mod - peer->instance_address)) - 1);
peer->instance_address_len = (cs_mod - peer->instance_address);
}
end:
uwsgi_rwunlock(ucr->cache->lock);
return 0;
}
int uwsgi_cr_map_use_pattern(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
size_t tmp_socket_name_len = 0;
ucr->magic_table['s'] = uwsgi_concat2n(peer->key, peer->key_len, "", 0);
peer->tmp_socket_name = magic_sub(ucr->pattern, ucr->pattern_len, &tmp_socket_name_len, ucr->magic_table);
free(ucr->magic_table['s']);
peer->instance_address_len = tmp_socket_name_len;
peer->instance_address = peer->tmp_socket_name;
return 0;
}
int uwsgi_cr_map_use_subscription(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
peer->un = uwsgi_get_subscribe_node(ucr->subscriptions, peer->key, peer->key_len);
if((peer->un == NULL) && (ucr->fallback_key != NULL)) {
peer->un = uwsgi_get_subscribe_node(ucr->subscriptions, ucr->fallback_key, ucr->fallback_key_len);
}
if (peer->un && peer->un->len) {
peer->instance_address = peer->un->name;
peer->instance_address_len = peer->un->len;
peer->modifier1 = peer->un->modifier1;
peer->modifier2 = peer->un->modifier2;
}
else if (ucr->cheap && !ucr->i_am_cheap && uwsgi_no_subscriptions(ucr->subscriptions)) {
uwsgi_gateway_go_cheap(ucr->name, ucr->queue, &ucr->i_am_cheap);
}
return 0;
}
int uwsgi_cr_map_use_subscription_dotsplit(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
char *name = peer->key;
uint16_t name_len = peer->key_len;
// max 5 split, reduce DOS attempts
int count = 5;
split:
if (!count)
return 0;
#ifdef UWSGI_DEBUG
uwsgi_log("trying with %.*s\n", name_len, name);
#endif
peer->un = uwsgi_get_subscribe_node(ucr->subscriptions, name, name_len);
if (!peer->un) {
char *next = memchr(name + 1, '.', name_len - 1);
if (next) {
name_len -= next - name;
name = next;
count--;
goto split;
}
}
if (peer->un && peer->un->len) {
peer->instance_address = peer->un->name;
peer->instance_address_len = peer->un->len;
peer->modifier1 = peer->un->modifier1;
peer->modifier2 = peer->un->modifier2;
}
else if (ucr->cheap && !ucr->i_am_cheap && uwsgi_no_subscriptions(ucr->subscriptions)) {
uwsgi_gateway_go_cheap(ucr->name, ucr->queue, &ucr->i_am_cheap);
}
return 0;
}
int uwsgi_cr_map_use_base(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
int tmp_socket_name_len = 0;
peer->tmp_socket_name = uwsgi_concat2nn(ucr->base, ucr->base_len, peer->key, peer->key_len, &tmp_socket_name_len);
peer->instance_address_len = tmp_socket_name_len;
peer->instance_address = peer->tmp_socket_name;
return 0;
}
int uwsgi_cr_map_use_cs(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
if (uwsgi.p[ucr->code_string_modifier1]->code_string) {
char *name = uwsgi_concat2("uwsgi_", ucr->short_name);
peer->instance_address = uwsgi.p[ucr->code_string_modifier1]->code_string(name, ucr->code_string_code, ucr->code_string_function, peer->key, peer->key_len);
free(name);
if (peer->instance_address) {
peer->instance_address_len = strlen(peer->instance_address);
char *cs_mod = uwsgi_str_contains(peer->instance_address, peer->instance_address_len, ',');
if (cs_mod) {
peer->modifier1 = uwsgi_str_num(cs_mod + 1, (peer->instance_address_len - (cs_mod - peer->instance_address)) - 1);
peer->instance_address_len = (cs_mod - peer->instance_address);
}
}
}
return 0;
}
int uwsgi_cr_map_use_to(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
peer->instance_address = ucr->to_socket->name;
peer->instance_address_len = ucr->to_socket->name_len;
return 0;
}
int uwsgi_cr_map_use_static_nodes(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
if (!ucr->current_static_node) {
ucr->current_static_node = ucr->static_nodes;
}
peer->static_node = ucr->current_static_node;
// is it a dead node ?
if (peer->static_node->custom > 0) {
// gracetime passed ?
if (peer->static_node->custom + ucr->static_node_gracetime <= (uint64_t) uwsgi_now()) {
peer->static_node->custom = 0;
}
else {
struct uwsgi_string_list *tmp_node = peer->static_node;
struct uwsgi_string_list *next_node = peer->static_node->next;
peer->static_node = NULL;
// needed for 1-node only setups
if (!next_node)
next_node = ucr->static_nodes;
while (tmp_node != next_node) {
if (!next_node) {
next_node = ucr->static_nodes;
}
if (tmp_node == next_node)
break;
if (next_node->custom == 0) {
peer->static_node = next_node;
break;
}
next_node = next_node->next;
}
}
}
if (peer->static_node) {
peer->instance_address = peer->static_node->value;
peer->instance_address_len = peer->static_node->len;
// set the next one
ucr->current_static_node = peer->static_node->next;
}
else {
// set the next one
ucr->current_static_node = ucr->current_static_node->next;
}
return 0;
}
|