void conn_free(struct connection *conn) { if (conn == NULL) return; if (conn->fd != -1) { close(conn->fd); conn->fd = -1; } conn->registered = false; if (conn->ev != NULL) { conn->ev->info = NULL; conn_free(conn->ev); conn_recycle(conn->ctx, conn->ev); conn->ev = NULL; } if (conn->info == NULL) return; struct conn_info *info = conn->info; info->status = DISCONNECTED; reader_free(&info->reader); reader_init(&info->reader); EMPTY_CMD_QUEUE(&info->cmd_queue, cmd_next); EMPTY_CMD_QUEUE(&info->ready_queue, ready_next); EMPTY_CMD_QUEUE(&info->waiting_queue, waiting_next); }
static struct connection *conn_create_server(struct context *ctx, struct address *addr, char *key) { int fd = conn_create_fd(); if (fd == -1) { LOG(ERROR, "conn_create_server: fail to create fd"); return NULL; } struct connection *server = server_create(ctx, fd); struct conn_info *info = server->info; memcpy(&info->addr, addr, sizeof(info->addr)); if (conn_connect(server) == CORVUS_ERR) { LOG(ERROR, "conn_create_server: fail to connect %s:%d", info->addr.ip, info->addr.port); conn_free(server); conn_buf_free(server); conn_recycle(ctx, server); return NULL; } strncpy(info->dsn, key, DSN_LEN); dict_set(&ctx->server_table, info->dsn, (void*)server); TAILQ_INSERT_TAIL(&ctx->servers, server, next); return server; }
static struct connection *conn_create_server(struct context *ctx, struct address *addr, char *key, bool readonly) { int fd = conn_create_fd(); if (fd == -1) { LOG(ERROR, "conn_create_server: fail to create fd"); return NULL; } struct connection *server = server_create(ctx, fd); struct conn_info *info = server->info; memcpy(&info->addr, addr, sizeof(info->addr)); extern const size_t CMD_NUM; info->slow_cmd_counts = cv_calloc(CMD_NUM, sizeof(uint32_t)); if (conn_connect(server) == CORVUS_ERR) { LOG(ERROR, "conn_create_server: fail to connect %s:%d", info->addr.ip, info->addr.port); conn_free(server); conn_buf_free(server); conn_recycle(ctx, server); return NULL; } if (readonly) { server->info->readonly = true; } strncpy(info->dsn, key, ADDRESS_LEN); dict_set(&ctx->server_table, info->dsn, (void*)server); TAILQ_INSERT_TAIL(&ctx->servers, server, next); return server; }