Scripts2026年5月26日·1 分钟阅读

libevent — Event-Driven Network Programming Library

A portable C library for scalable asynchronous I/O, providing event notification across epoll, kqueue, IOCP, and more, used by Memcached, Tor, and Chromium.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
libevent Overview
直接安装命令
npx -y tokrepo@latest install cdf0e4d1-5898-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

libevent provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout. It is one of the oldest and most battle-tested event notification libraries in the C ecosystem, powering critical infrastructure like Memcached, Tor, and parts of Chromium.

What libevent Does

  • Abstracts OS-specific I/O multiplexing (epoll, kqueue, IOCP, /dev/poll, select) behind one API
  • Provides buffered event I/O with automatic read/write watermarks (bufferevents)
  • Offers HTTP server and client implementations for embedding lightweight web services
  • Handles DNS resolution asynchronously without blocking the event loop
  • Supports OpenSSL-based encrypted connections integrated with the bufferevent layer

Architecture Overview

libevent centers on an event_base that manages a priority queue of pending events. When you add an event (I/O readiness, signal, or timer), the base registers it with the OS backend. The dispatch loop blocks until events fire, then invokes callbacks in priority order. Bufferevents layer read/write buffering on top of raw events, handling partial reads and flow control automatically.

Self-Hosting & Configuration

  • Build with CMake or autoconf: mkdir build && cd build && cmake .. && make
  • Link against libevent_core (minimal), libevent_extra (HTTP, DNS), or libevent_openssl
  • Configure the backend explicitly or let libevent auto-select the fastest available
  • Set EVENT_BASE_FLAG_NOLOCK for single-threaded applications to avoid locking overhead
  • Works on Linux, macOS, Windows, FreeBSD, Solaris, and most POSIX systems

Key Features

  • Proven at scale: Memcached serves millions of requests/sec using libevent
  • Bufferevent abstraction handles partial I/O, TLS handshakes, and rate limiting
  • Built-in HTTP 1.1 server suitable for embedding admin interfaces or health checks
  • Thread-safe with explicit locking or lock-free single-threaded mode
  • BSD licensed with over 20 years of production use

Comparison with Similar Tools

  • libuv — newer, better Windows support, powers Node.js; libevent has richer built-in protocols
  • libev — faster on Unix but no Windows IOCP support and fewer built-in features
  • Boost.Asio — C++ only with heavy template usage; libevent is plain C
  • io_uring (liburing) — Linux-only, lower-level; libevent is cross-platform
  • epoll/kqueue directly — maximum control but platform-specific and more code

FAQ

Q: Should I use libevent or libuv for a new project? A: libuv has a more modern API and better Windows integration. libevent is better if you need the built-in HTTP server, DNS resolver, or OpenSSL bufferevent.

Q: Is libevent thread-safe? A: Yes, when compiled with threading support. Each thread should have its own event_base, or you can use event_base_loopbreak() / event_active() to communicate across threads.

Q: Can I handle thousands of concurrent connections? A: Yes. libevent with epoll or kqueue scales to hundreds of thousands of connections. Memcached regularly handles 100K+ concurrent connections per instance.

Q: Does libevent support HTTP/2? A: The built-in HTTP module supports HTTP/1.1 only. For HTTP/2, pair libevent with nghttp2 or use a different HTTP stack.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产