refactor: use vec for dynamically-size router

master
Sean McBride 3 years ago
parent a529db3430
commit e20de0d18a

@ -7,58 +7,53 @@
#include "module.h" #include "module.h"
#include "route.h" #include "route.h"
#include "route_config.h" #include "route_config.h"
#include "vec.h"
#define HTTP_ROUTER_ROUTES_CAPACITY 32 typedef struct route route_t;
VEC(route_t)
typedef struct vec_route_t http_router_t;
struct http_router {
struct route routes[HTTP_ROUTER_ROUTES_CAPACITY];
size_t routes_length;
};
static inline void static inline void
http_router_init(struct http_router *router) http_router_init(http_router_t *router, size_t capacity)
{ {
router->routes_length = 0; vec_route_t_init(router, capacity);
} }
static inline int static inline int
http_router_add_route(struct http_router *router, struct route_config *config, struct module *module) http_router_add_route(http_router_t *router, struct route_config *config, struct module *module)
{ {
assert(router != NULL); assert(router != NULL);
assert(config != NULL); assert(config != NULL);
assert(module != NULL); assert(module != NULL);
assert(config->route != NULL); assert(config->route != NULL);
assert(config->http_resp_content_type != NULL); assert(config->http_resp_content_type != NULL);
assert(router->routes_length < HTTP_ROUTER_ROUTES_CAPACITY);
if (unlikely(router->routes_length == HTTP_ROUTER_ROUTES_CAPACITY)) { return -1; }
router->routes[router->routes_length] = (struct route){ struct route route = { .route = config->route,
.route = config->route, .module = module,
.module = module, .relative_deadline_us = config->relative_deadline_us,
.relative_deadline_us = config->relative_deadline_us, .relative_deadline = (uint64_t)config->relative_deadline_us
.relative_deadline = (uint64_t)config->relative_deadline_us * runtime_processor_speed_MHz, * runtime_processor_speed_MHz,
.response_size = config->http_resp_size, .response_size = config->http_resp_size,
.response_content_type = config->http_resp_content_type .response_content_type = config->http_resp_content_type };
};
/* Admissions Control */ /* Admissions Control */
uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz; uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz;
admissions_info_initialize(&router->routes[router->routes_length].admissions_info, admissions_info_initialize(&route.admissions_info, config->admissions_percentile, expected_execution,
config->admissions_percentile, expected_execution, route.relative_deadline);
router->routes[router->routes_length].relative_deadline);
int rc = vec_route_t_push(router, route);
if (unlikely(rc == -1)) { return -1; }
router->routes_length++;
return 0; return 0;
} }
static inline struct route * static inline struct route *
http_router_match_route(struct http_router *router, char *route) http_router_match_route(http_router_t *router, char *route)
{ {
for (int i = 0; i < router->routes_length; i++) { for (int i = 0; i < router->length; i++) {
if (strncmp(route, router->routes[i].route, strlen(router->routes[i].route)) == 0) { if (strncmp(route, router->buffer[i].route, strlen(router->buffer[i].route)) == 0) {
return &router->routes[i]; return &router->buffer[i];
} }
} }

@ -7,6 +7,6 @@
struct tenant { struct tenant {
char *name; char *name;
struct tcp_server tcp_server; struct tcp_server tcp_server;
struct http_router router; http_router_t router;
struct module_database module_db; struct module_database module_db;
}; };

@ -35,7 +35,7 @@ tenant_alloc(struct tenant_config *config)
config->name = NULL; config->name = NULL;
tcp_server_init(&tenant->tcp_server, config->port); tcp_server_init(&tenant->tcp_server, config->port);
http_router_init(&tenant->router); http_router_init(&tenant->router, config->routes_len);
module_database_init(&tenant->module_db); module_database_init(&tenant->module_db);
for (int i = 0; i < config->routes_len; i++) { for (int i = 0; i < config->routes_len; i++) {
@ -48,8 +48,8 @@ tenant_alloc(struct tenant_config *config)
/* Ownership of config's route and http_resp_content_type move here */ /* Ownership of config's route and http_resp_content_type move here */
int rc = http_router_add_route(&tenant->router, &config->routes[i], module); int rc = http_router_add_route(&tenant->router, &config->routes[i], module);
if (unlikely(rc != 0)) { if (unlikely(rc != 0)) {
panic("Tenant %s defined %lu routes, but router could only hold %d\n", tenant->name, panic("Tenant %s defined %lu routes, but router failed to grow beyond %lu\n", tenant->name,
config->routes_len, HTTP_ROUTER_ROUTES_CAPACITY); config->routes_len, tenant->router.capacity);
} }
config->routes[i].route = NULL; config->routes[i].route = NULL;

@ -105,7 +105,10 @@
\ \
static inline int vec_##TYPE##_insert(struct vec_##TYPE *vec, uint32_t idx, TYPE value) \ static inline int vec_##TYPE##_insert(struct vec_##TYPE *vec, uint32_t idx, TYPE value) \
{ \ { \
if (idx >= vec->capacity) vec_##TYPE##_grow(vec); \ if (vec->length == vec->capacity) { \
int rc = vec_##TYPE##_grow(vec); \
if (rc != 0) return -1; \
} \
\ \
vec->buffer[idx] = value; \ vec->buffer[idx] = value; \
return 0; \ return 0; \
@ -116,4 +119,17 @@
if (idx >= vec->capacity) return NULL; \ if (idx >= vec->capacity) return NULL; \
\ \
return &vec->buffer[idx]; \ return &vec->buffer[idx]; \
} \
\
static inline int vec_##TYPE##_push(struct vec_##TYPE *vec, TYPE value) \
{ \
if (vec->length == vec->capacity) { \
int rc = vec_##TYPE##_grow(vec); \
if (rc != 0) return -1; \
} \
\
vec->buffer[vec->length] = value; \
vec->length++; \
\
return 0; \
} }

Loading…
Cancel
Save