-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
510 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "client_list.h" | ||
|
||
blocked_client_t *blocked_client_create(RedisModuleBlockedClient*bc, void **targ) { | ||
blocked_client_t *bct = malloc(sizeof(blocked_client_t)); | ||
bct->client = bc; | ||
bct->targ = targ; | ||
return bct; | ||
} | ||
|
||
int blocked_client_free(blocked_client_t *bct) { | ||
free(&bct->targ); | ||
free(bct); | ||
return 1; | ||
} | ||
|
||
//Blocked clients array | ||
blocked_clients_t *blocked_clients_create(const char * cmd) { | ||
blocked_clients_t* blocked = malloc(sizeof(blocked_clients_t)); | ||
blocked->clients = malloc(sizeof(RedisModuleBlockedClient*)); | ||
blocked->size = 0; | ||
blocked->cmd = strdup(cmd); | ||
return blocked; | ||
} | ||
|
||
int blocked_clients_add(blocked_clients_t *bc, RedisModuleBlockedClient *client, void **targ) { | ||
bc->clients = realloc(bc->clients, sizeof(blocked_client_t*) * (bc->size +1)); | ||
bc->clients[bc->size] = blocked_client_create(client, targ); | ||
bc->size++; | ||
return 1; | ||
} | ||
|
||
int blocked_clients_free(blocked_clients_t *bc) { | ||
for (size_t i = 0; i < bc->size; i++){ | ||
blocked_client_free(bc->clients[i]); | ||
} | ||
free(bc->clients); | ||
free(bc); | ||
return 1; | ||
} | ||
|
||
//General blocklist maps blocked clients and command | ||
blocked_list_t *blocked_list_create(const char * cmd) { | ||
blocked_list_t* bl = malloc(sizeof(blocked_list_t)); | ||
bl->list = malloc(sizeof(blocked_clients_t*)); | ||
bl->size = 0; | ||
bl->cmd = strdup(cmd); | ||
return bl; | ||
} | ||
|
||
int blocked_list_add_bc(blocked_list_t *bl, blocked_clients_t *clientList) { | ||
bl->list = realloc(bl->list, sizeof(blocked_clients_t*) * (bl->size +1)); | ||
bl->list[bl->size] = clientList; | ||
bl->size++; | ||
return 1; | ||
} | ||
|
||
int blocked_list_delete(blocked_list_t *bl, blocked_clients_t *clientList) { | ||
int pos = -1; | ||
for (size_t i = 0; i < bl->size; i++){ | ||
if (bl->list[i] == clientList) pos = i; | ||
} | ||
|
||
if (pos == -1) return 0; | ||
|
||
blocked_clients_free(clientList); | ||
|
||
for (size_t i = pos - 1; 0 < bl->size - 1; i++) | ||
bl->list[i] = bl->list[i+1]; | ||
bl->size --; | ||
bl->list = realloc(bl->list, sizeof(blocked_clients_t*)); | ||
return 1; | ||
} | ||
|
||
//Searches clients blocked by command | ||
blocked_clients_t *blocked_list_find_cmd(blocked_list_t *list, const char *cmd) { | ||
for (size_t i = 0; i < list->size; i++) { | ||
if (strcasecmp(list->list[i]->cmd, cmd)) { | ||
return list->list[i]; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
//Add blocked client into specified group | ||
int blocked_list_add_client(blocked_list_t *list, const char *cmd, RedisModuleBlockedClient *client, void **targ) { | ||
blocked_clients_t *bc; | ||
bc = blocked_list_find_cmd(list, cmd); | ||
//create new bock list if not exists | ||
if (bc == NULL) { | ||
bc = blocked_clients_create(cmd); | ||
blocked_list_add_bc(list, bc); | ||
} | ||
blocked_clients_add(bc, client, targ); | ||
return 1; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef __CLIENTS_LIST_H | ||
#define __CLIENTS_LIST_H | ||
|
||
#include "general.h" | ||
|
||
typedef struct blocked_client { | ||
RedisModuleBlockedClient *client; | ||
void ** targ; | ||
} blocked_client_t; | ||
|
||
typedef struct blocked_clients { | ||
blocked_client_t **clients; | ||
size_t size; | ||
const char * cmd; | ||
} blocked_clients_t; | ||
|
||
typedef struct blocked_list { | ||
blocked_clients_t **list; | ||
size_t size; | ||
const char * cmd; | ||
} blocked_list_t; | ||
|
||
blocked_clients_t *blocked_list_find_cmd(blocked_list_t *list, const char *cmd); | ||
int blocked_list_add_client(blocked_list_t *list, const char *cmd, RedisModuleBlockedClient *client, void **targ); | ||
blocked_list_t *blocked_list_create(const char * cmd); | ||
int blocked_client_free(blocked_client_t *bct); | ||
int blocked_clients_free(blocked_clients_t *bc); | ||
int blocked_list_delete(blocked_list_t *bl, blocked_clients_t *clientList); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.