Skip to content

Commit 4452e88

Browse files
soulbirddoujiang24doujiang24jizhuozhizhaoshirui
authored
feat: sync upstream (#5)
Co-authored-by: doujiang <[email protected]> Co-authored-by: doujiang24 <[email protected]> Co-authored-by: jizhuozhi <[email protected]> Co-authored-by: zhaoshirui <[email protected]>
1 parent b0c829a commit 4452e88

File tree

7 files changed

+441
-22
lines changed

7 files changed

+441
-22
lines changed

README.markdown

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Synopsis
5050
init_by_lua_block {
5151
local resty_chash = require "resty.chash"
5252
local resty_roundrobin = require "resty.roundrobin"
53+
local resty_swrr = require "resty.swrr"
5354

5455
local server_list = {
5556
["127.0.0.1:1985"] = 2,
@@ -76,6 +77,9 @@ Synopsis
7677

7778
local rr_up = resty_roundrobin:new(server_list)
7879
package.loaded.my_rr_up = rr_up
80+
81+
local swrr_up = resty_swrr:new(server_list)
82+
package.loaded.my_swrr_up = swrr_up
7983
}
8084

8185
upstream backend_chash {
@@ -108,6 +112,20 @@ Synopsis
108112
}
109113
}
110114

115+
upstream backend_swrr {
116+
server 0.0.0.1;
117+
balancer_by_lua_block {
118+
local b = require "ngx.balancer"
119+
120+
local swrr_up = package.loaded.my_swrr_up
121+
122+
-- Note that SWRR picks the first server randomly
123+
local server = swrr_up:find()
124+
125+
assert(b.set_current_peer(server))
126+
}
127+
}
128+
111129
server {
112130
location /chash {
113131
proxy_pass http://backend_chash;
@@ -116,6 +134,10 @@ Synopsis
116134
location /roundrobin {
117135
proxy_pass http://backend_rr;
118136
}
137+
138+
location /swrr {
139+
proxy_pass http://backend_swrr;
140+
}
119141
}
120142
```
121143

@@ -124,7 +146,7 @@ Synopsis
124146
Methods
125147
=======
126148

127-
Both `resty.chash` and `resty.roundrobin` have the same apis.
149+
Both `resty.chash`, `resty.roundrobin` and `resty.swrr` have the same apis.
128150

129151
[Back to TOC](#table-of-contents)
130152

chash.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define u_char unsigned char
1111
#endif
1212

13+
#define CHASH_OK 0
14+
#define CHASH_ERR -1
1315

1416
#define crc32_final(crc) \
1517
crc ^= 0xffffffff
@@ -144,7 +146,7 @@ chash_point_init(chash_point_t *arr, uint32_t base_hash, uint32_t start,
144146
}
145147

146148

147-
void
149+
int
148150
chash_point_sort(chash_point_t arr[], uint32_t n)
149151
{
150152
chash_point_t *points;
@@ -163,6 +165,9 @@ chash_point_sort(chash_point_t arr[], uint32_t n)
163165
step = pow(2, 32) / m;
164166

165167
points = (chash_point_t *) calloc(m, sizeof(chash_point_t));
168+
if (points == NULL) {
169+
return CHASH_ERR;
170+
}
166171

167172
for (i = 0; i < n; i++) {
168173
node = &arr[i];
@@ -246,10 +251,12 @@ chash_point_sort(chash_point_t arr[], uint32_t n)
246251
}
247252

248253
free(points);
254+
255+
return CHASH_OK;
249256
}
250257

251258

252-
void
259+
int
253260
chash_point_add(chash_point_t *old_points, uint32_t old_length,
254261
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id,
255262
chash_point_t *new_points)
@@ -258,9 +265,16 @@ chash_point_add(chash_point_t *old_points, uint32_t old_length,
258265
chash_point_t *tmp_points;
259266

260267
tmp_points = (chash_point_t *) calloc(num, sizeof(chash_point_t));
268+
if (tmp_points == NULL) {
269+
return CHASH_ERR;
270+
}
261271

262272
chash_point_init_crc(tmp_points, 0, base_hash, from, num, id);
263-
chash_point_sort(tmp_points, num);
273+
274+
if (chash_point_sort(tmp_points, num) != CHASH_OK) {
275+
free(tmp_points);
276+
return CHASH_ERR;
277+
}
264278

265279
j = num - 1;
266280
k = old_length + num - 1;
@@ -283,10 +297,12 @@ chash_point_add(chash_point_t *old_points, uint32_t old_length,
283297
}
284298

285299
free(tmp_points);
300+
301+
return CHASH_OK;
286302
}
287303

288304

289-
void
305+
int
290306
chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
291307
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id)
292308
{
@@ -296,7 +312,11 @@ chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
296312
tmp_points = (chash_point_t *) calloc(num, sizeof(chash_point_t));
297313

298314
chash_point_init_crc(tmp_points, 0, base_hash, from, num, id);
299-
chash_point_sort(tmp_points, num);
315+
316+
if (chash_point_sort(tmp_points, num) != CHASH_OK) {
317+
free(tmp_points);
318+
return CHASH_ERR;
319+
}
300320

301321
for (i = 0, j = 0, k = 0; i < old_length; i++) {
302322
if (j < num
@@ -315,6 +335,8 @@ chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
315335
}
316336

317337
free(tmp_points);
338+
339+
return CHASH_OK;
318340
}
319341

320342

chash.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ typedef struct {
3939
*/
4040
void chash_point_init(chash_point_t *points, uint32_t base_hash,
4141
uint32_t start, uint32_t num, uint32_t id) LCH_EXPORT;
42-
void chash_point_sort(chash_point_t *points, uint32_t npoints) LCH_EXPORT;
42+
int chash_point_sort(chash_point_t *points, uint32_t npoints) LCH_EXPORT;
4343

44-
void chash_point_add(chash_point_t *old_points, uint32_t old_length,
44+
int chash_point_add(chash_point_t *old_points, uint32_t old_length,
4545
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id,
4646
chash_point_t *new_points) LCH_EXPORT;
47-
void chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
47+
int chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
4848
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id) LCH_EXPORT;
4949
void chash_point_delete(chash_point_t *old_points, uint32_t old_length,
5050
uint32_t id) LCH_EXPORT;

lib/resty/chash.lua

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ local pairs = pairs
1919
local tostring = tostring
2020
local tonumber = tonumber
2121
local bxor = bit.bxor
22+
local error = error
23+
24+
25+
local CHASH_OK = 0
2226

2327

2428
ffi.cdef[[
@@ -31,12 +35,12 @@ typedef struct {
3135

3236
void chash_point_init(chash_point_t *points, uint32_t base_hash, uint32_t start,
3337
uint32_t num, uint32_t id);
34-
void chash_point_sort(chash_point_t *points, uint32_t size);
38+
int chash_point_sort(chash_point_t *points, uint32_t size);
3539

36-
void chash_point_add(chash_point_t *old_points, uint32_t old_length,
40+
int chash_point_add(chash_point_t *old_points, uint32_t old_length,
3741
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id,
3842
chash_point_t *new_points);
39-
void chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
43+
int chash_point_reduce(chash_point_t *old_points, uint32_t old_length,
4044
uint32_t base_hash, uint32_t from, uint32_t num, uint32_t id);
4145
void chash_point_delete(chash_point_t *old_points, uint32_t old_length,
4246
uint32_t id);
@@ -117,7 +121,9 @@ local function _precompute(nodes)
117121
start = start + num
118122
end
119123

120-
clib.chash_point_sort(points, npoints)
124+
if clib.chash_point_sort(points, npoints) ~= CHASH_OK then
125+
error("no memory")
126+
end
121127

122128
return ids, points, npoints, newnodes
123129
end
@@ -194,14 +200,21 @@ local function _incr(self, id, weight)
194200
local new_npoints = self.npoints + weight * CONSISTENT_POINTS
195201
if self.size < new_npoints then
196202
new_points = ffi_new(chash_point_t, new_npoints)
197-
self.size = new_npoints
198203
end
199204

200205
local base_hash = bxor(crc32(tostring(id)), 0xffffffff)
201-
clib.chash_point_add(self.points, self.npoints, base_hash,
202-
old_weight * CONSISTENT_POINTS,
203-
weight * CONSISTENT_POINTS,
204-
index, new_points)
206+
local rc = clib.chash_point_add(self.points, self.npoints, base_hash,
207+
old_weight * CONSISTENT_POINTS,
208+
weight * CONSISTENT_POINTS,
209+
index, new_points)
210+
211+
if rc ~= CHASH_OK then
212+
error("no memory")
213+
end
214+
215+
if self.size < new_npoints then
216+
self.size = new_npoints
217+
end
205218

206219
self.points = new_points
207220
self.npoints = new_npoints
@@ -230,10 +243,15 @@ local function _decr(self, id, weight)
230243
end
231244

232245
local base_hash = bxor(crc32(tostring(id)), 0xffffffff)
233-
clib.chash_point_reduce(self.points, self.npoints, base_hash,
234-
(old_weight - weight) * CONSISTENT_POINTS,
235-
CONSISTENT_POINTS * weight,
236-
index)
246+
local from = (old_weight - weight) * CONSISTENT_POINTS
247+
local num = CONSISTENT_POINTS * weight
248+
249+
local rc = clib.chash_point_reduce(self.points, self.npoints, base_hash,
250+
from, num, index)
251+
252+
if rc ~= CHASH_OK then
253+
error("no memory")
254+
end
237255

238256
nodes[id] = old_weight - weight
239257
self.npoints = self.npoints - CONSISTENT_POINTS * weight

lib/resty/roundrobin.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local next = next
44
local tonumber = tonumber
55
local setmetatable = setmetatable
66
local math_random = math.random
7+
local error = error
78

89
local utils = require "resty.balancer.utils"
910

0 commit comments

Comments
 (0)