Skip to content

"Be smart about timeouts" libev timeout utility library.

License

Notifications You must be signed in to change notification settings

andrew-canaday/libbsat

Repository files navigation

libbsat

configure-make-check

This is a small C utility library which provides timeout management for libev-based projects.

It implements be smart about timeouts strategy #4 — which allows you to manage an arbitrary number of timeouts in O(1) time.

This makes it easy to implement things like idle disconnect and graceful shutdown timeouts, regardless of workload concurrency.

License

It's licensed under the MIT License.1

How does it work?

This library is used to manage sets of timeouts which share a common timeout delta.

There are two basic types, bsat_toq_t (a timeout queue) and bsat_timeout_t (an individual timeout item).

A server get's one timeout queue (bsat_toq_t), which has an associated timeout delta and callback function.

Individual connections have a timeout (bsat_timeout_t) which is started on connect, reset whenever there is any activity, and stopped on disconnect.

The timeout callback will be invoked for any connections which have timeouts that haven't been reset within the specified timeout period.

See Also

Homebrew

brew tap andrew-canaday/tap
brew install --build-from-source libbsat

Building

This project uses:

NOTE: the whole library is one .c file and one .h file. It should be pretty trivial to integrate it into a different build system or statically compile it into another program or library.

Configuration, build, and installation follows the classic pattern:

# NOTE: this assumes you're in the source root directory.

# Generate the configure script and ancillary build files:
./autogen.sh

# Create a build directory and cd into it:
mkdir -p ./build ; cd ./build

# configure, make, make check, and make install!
../configure --prefix=/my/installation/prefix \
    && make \
    && make check \
    && make install

Example Server

The example server is an automake "extra" target. You can build it like so:

# NOTE: assumes you are in the "build" directory above.

# Build it:
make -C ./example bsat_example

# Run it:
./example/bsat_example

1 "Wait a minute! Aren't you one of those GPL nuts?"
Yes, but this library is very small and it's just a naive implementation of the strategy documented in the link above.