/src/h2o/lib/common/balancer/roundrobin.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2017 Justin Zhu |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include "h2o/balancer.h" |
23 | | |
24 | | struct round_robin_t { |
25 | | h2o_balancer_t super; |
26 | | size_t pos; /* current position */ |
27 | | size_t consumed_weight; /* remained weight of current position */ |
28 | | pthread_mutex_t mutex; |
29 | | }; |
30 | | |
31 | | static inline void select_next(struct round_robin_t *self, h2o_socketpool_target_vector_t *targets) |
32 | 0 | { |
33 | 0 | self->pos += 1; |
34 | 0 | if (self->pos == targets->size) |
35 | 0 | self->pos = 0; |
36 | 0 | self->consumed_weight = 0; |
37 | 0 | } |
38 | | |
39 | | static size_t selector(h2o_balancer_t *balancer, h2o_socketpool_target_vector_t *targets, char *tried) |
40 | 0 | { |
41 | 0 | size_t i; |
42 | 0 | size_t result = 0; |
43 | 0 | struct round_robin_t *self = (void *)balancer; |
44 | |
|
45 | 0 | pthread_mutex_lock(&self->mutex); |
46 | |
|
47 | 0 | assert(targets->size != 0); |
48 | 0 | for (i = 0; i < targets->size; i++) { |
49 | 0 | if (!tried[self->pos]) { |
50 | | /* get the result */ |
51 | 0 | result = self->pos; |
52 | 0 | if (++self->consumed_weight > targets->entries[self->pos]->conf.weight_m1) |
53 | 0 | select_next(self, targets); |
54 | 0 | pthread_mutex_unlock(&self->mutex); |
55 | 0 | return result; |
56 | 0 | } else { |
57 | 0 | select_next(self, targets); |
58 | 0 | } |
59 | 0 | } |
60 | 0 | h2o_fatal("unreachable"); |
61 | 0 | } |
62 | | |
63 | | static void destroy(h2o_balancer_t *balancer) |
64 | 0 | { |
65 | 0 | struct round_robin_t *self = (void *)balancer; |
66 | 0 | pthread_mutex_destroy(&self->mutex); |
67 | 0 | free(self); |
68 | 0 | } |
69 | | |
70 | | h2o_balancer_t *h2o_balancer_create_rr(void) |
71 | 1 | { |
72 | 1 | static const h2o_balancer_callbacks_t rr_callbacks = {selector, destroy}; |
73 | | |
74 | 1 | struct round_robin_t *self = h2o_mem_alloc(sizeof(*self)); |
75 | 1 | pthread_mutex_init(&self->mutex, NULL); |
76 | 1 | self->super.callbacks = &rr_callbacks; |
77 | 1 | self->pos = 0; |
78 | 1 | self->consumed_weight = 0; |
79 | | |
80 | 1 | return &self->super; |
81 | 1 | } |