Giter VIP home page Giter VIP logo

paranlee / multithread-libevent-echo-server Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 4.0 38 KB

Multithreaded, libevent-based socket server.

Home Page: https://roncemer.com/software-development/multi-threaded-libevent-server-example/

License: BSD 3-Clause "New" or "Revised" License

C 78.17% Makefile 1.01% Dockerfile 5.23% Starlark 15.59%
c-server libevent event-driven mutlithreading realtime-messaging

multithread-libevent-echo-server's Introduction

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

multithread-libevent-echo-server's People

Contributors

paranlee avatar roncemer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

multithread-libevent-echo-server's Issues

Handling of queued clients and timeout

Hi,

First thanks so much for sharing your code. There really are very few usable examples of multithreaded libevent that can be used as a tutorial.

I see something that I perceive as an issue. Correct me if I'm wrong.

I start a server with 2 threads on a server with 2 CPU.
I connect 3 independent telnet clients a,b,c close in time to each other to create congestion.

client a connects and echos immediately. There are messages from thread [10] on the server
client b connects and echos immediately. There are messages from thread [14] on the server
client c connects immediately (process is listening) but does not echo (event is not triggering).
I'd expect this as there are #define CONNECTION_BACKLOG 8 on the TCP socket.

I now leave client b to time out after SOCKET_READ_TIMEOUT_SECONDS 100.
Client a continues working normally. Client c is never serviced.

Now what I think is abnormal. Client b times out expires. Client c times out simultaneously and never gets serviced.

Is this expected behaviour, a bug, or a limitation in libevent?

Shouldn't the code be using something like evconnlistener_new_bind rather than listening and binding independently of libevent?

I can debug further on request if you're still maintaining this code.

best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.