From 7cd83e15139447156ca915ce2d9d19295c146d56 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 15 May 2023 15:31:33 +0200 Subject: rework server-worker communication OK, this is a major rework. * tcp_server: connection threads are not detached anymore, the caller has to clean them up. This was done so that the server can clean up the threads cleanly. * run_queue: simple refactoring, run_queue_entry is called just run now. * server: worker threads are now killed when a run is assigned to a worker. * worker: the connection to server is no longer persistent. A worker sends "new-worker", waits for a task, closes the connection, and when it's done, sends the "complete" message and waits for a new task. This is supposed to improve resilience, since the worker-server connections don't have to be maintained while the worker is doing a CI run. --- src/worker_queue.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/worker_queue.c (limited to 'src/worker_queue.c') diff --git a/src/worker_queue.c b/src/worker_queue.c new file mode 100644 index 0000000..3e207e3 --- /dev/null +++ b/src/worker_queue.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Egor Tensin + * This file is part of the "cimple" project. + * For details, see https://github.com/egor-tensin/cimple. + * Distributed under the MIT License. + */ + +#include "worker_queue.h" +#include "log.h" + +#include +#include +#include + +struct worker { + pthread_t thread; + int fd; + STAILQ_ENTRY(worker) entries; +}; + +int worker_create(struct worker **_entry, int fd) +{ + struct worker *entry = malloc(sizeof(struct worker)); + if (!entry) { + log_errno("malloc"); + return -1; + } + + entry->thread = pthread_self(); + entry->fd = fd; + + *_entry = entry; + return 0; +} + +void worker_destroy(struct worker *entry) +{ + log("Waiting for worker %d thread to exit\n", entry->fd); + pthread_errno_if(pthread_join(entry->thread, NULL), "pthread_join"); + free(entry); +} + +int worker_get_fd(const struct worker *entry) +{ + return entry->fd; +} + +void worker_queue_create(struct worker_queue *queue) +{ + STAILQ_INIT(queue); +} + +void worker_queue_destroy(struct worker_queue *queue) +{ + struct worker *entry1 = STAILQ_FIRST(queue); + while (entry1) { + struct worker *entry2 = STAILQ_NEXT(entry1, entries); + worker_destroy(entry1); + entry1 = entry2; + } + STAILQ_INIT(queue); +} + +int worker_queue_is_empty(const struct worker_queue *queue) +{ + return STAILQ_EMPTY(queue); +} + +void worker_queue_add_first(struct worker_queue *queue, struct worker *entry) +{ + STAILQ_INSERT_HEAD(queue, entry, entries); +} + +void worker_queue_add_last(struct worker_queue *queue, struct worker *entry) +{ + STAILQ_INSERT_HEAD(queue, entry, entries); +} + +struct worker *worker_queue_remove_first(struct worker_queue *queue) +{ + struct worker *entry = STAILQ_FIRST(queue); + STAILQ_REMOVE_HEAD(queue, entries); + return entry; +} -- cgit v1.2.3