Skip to content

Latest commit

 

History

History
138 lines (84 loc) · 3.3 KB

File metadata and controls

138 lines (84 loc) · 3.3 KB

Multithreaded, libevent-based socket server.

Using nice libevent

Libevent is a nice library for handling and dispatching events, as well as doing nonblocking I/O.

This is fine, except that it is basically single-threaded.

Do not under-utilize your computing resource

If you have multiple CPUs or a CPU with hyperthreading,

you're really under-utilizing the CPU resources available to your server application.

Because your event pump is running in a single thread and therefore can only use one CPU core at a time.

Event Queueing

The solution is to create one libevent event queues (AKA event_base) per active connection,

each with its own event pump thread.

This project does exactly that,

giving you everything you need to write high-performance, multi-threaded, libevent-based socket servers.

There are mentionings of running libevent in a multithreaded implementation,

however it is very difficult (if not impossible) to find working implementations.

This project is a working implementation of a multi-threaded, libevent-based socket server.

Build

Build output server.o is in build directory.

You can choose each build method.

Build with bazel

Bazel look up src/BUILD file.

foo@bar:~/multithread-libevent-echo-server$ bazel build //src:server
INFO: Analyzed target //src:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //src:server up-to-date:
  bazel-bin/src/server
INFO: Elapsed time: 0.121s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

Then, copy executable file.

foo@bar:~/multithread-libevent-echo-server$ cp bazel-bin/src/server build/server.o

Build with Make

foo@bar:~/multithread-libevent-echo-server$ make

Using native CC

Go to src directory.

foo@bar:~/multithread-libevent-echo-server$ cd src 

GCC

foo@bar:~/multithread-libevent-echo-server$ gcc -o ../build/server.o src/server.c src/workqueue.c -levent -lpthread

clang

foo@bar:~/multithread-libevent-echo-server$ clang-12 -o ../build/server.o src/server.c src/workqueue.c -levent -lpthread

Test Echo

Run Server

Server running in port defined in server.c [SERVER_PORT].

Host server

foo@bar:~/multithread-libevent-echo-server/build$./server.o
Server running in [SERVER_PORT]

Run on Docker

Build Docker image.

foo@bar:~/multithread-libevent-echo-server/$ podman build -t multithread-event-server .

Run container.

foo@bar:~/multithread-libevent-echo-server/$ podman run -p 8080:8080 --name multithread-event-server multithread-event-server:latest
Server running in [SERVER_PORT]

Run on host client

The server itself simply echoes whatever you send to it.

Start it up, then telnet

foo@bar:~/telnet localhost 8080

License

Copyright (c) 2012 Ronald Bennett Cemer

This software is licensed under the BSD license.

See the accompanying LICENSE.txt for details.

References

Echoserver link

Cliserver link

Bazel Build System with C