netpoll
A high-performance non-blocking I/O networking framework focusing on RPC scenarios.
Top Related Projects
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
Fast event-loop networking for Go
Quick Overview
CloudWeGo's netpoll is a high-performance, scalable, and easy-to-use network I/O library for Go. It provides a simple and efficient API for building network applications, with a focus on performance and scalability.
Pros
- High Performance: netpoll is designed to be highly efficient, with low overhead and support for large numbers of concurrent connections.
- Scalability: The library can handle a large number of concurrent connections without sacrificing performance.
- Ease of Use: netpoll offers a simple and intuitive API, making it easy to integrate into existing Go projects.
- Cross-Platform: The library is cross-platform, supporting Windows, macOS, and Linux.
Cons
- Limited Documentation: The project's documentation could be more comprehensive, making it harder for new users to get started.
- Lack of Community: Compared to some other Go networking libraries, netpoll has a smaller community and fewer third-party integrations.
- Specialized Use Case: netpoll is primarily designed for building high-performance network applications, which may not be relevant for all Go projects.
- Potential Learning Curve: Developers who are new to event-driven programming or low-level network programming may need to invest more time to fully understand and utilize netpoll.
Code Examples
Here are a few examples of how to use the netpoll library:
- Creating a simple TCP server:
package main
import (
"fmt"
"log"
"github.com/cloudwego/netpoll"
)
func main() {
server, err := netpoll.NewServer(func(conn netpoll.Connection) {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
log.Printf("read error: %v", err)
return
}
fmt.Printf("received: %s\n", buf[:n])
_, err = conn.Write([]byte("Hello, client!"))
if err != nil {
log.Printf("write error: %v", err)
}
})
if err != nil {
log.Fatalf("failed to create server: %v", err)
}
defer server.Shutdown()
if err := server.Serve(":8080"); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
- Creating a simple HTTP server:
package main
import (
"fmt"
"log"
"net/http"
"github.com/cloudwego/netpoll"
)
func main() {
server, err := netpoll.NewServer(func(conn netpoll.Connection) {
req, err := http.ReadRequest(netpoll.NewReader(conn))
if err != nil {
log.Printf("read request error: %v", err)
return
}
fmt.Fprintf(netpoll.NewWriter(conn), "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello, world!")
})
if err != nil {
log.Fatalf("failed to create server: %v", err)
}
defer server.Shutdown()
if err := server.Serve(":8080"); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
- Creating a simple UDP server:
package main
import (
"fmt"
"log"
"github.com/cloudwego/netpoll"
)
func main() {
server, err := netpoll.NewUDPServer(func(conn netpoll.UDPConnection) {
buf := make([]byte, 1024)
n, addr, err := conn.ReadFrom(buf)
if err != nil {
log.Printf("read error: %v", err)
return
}
fmt.Printf("received from %s: %s\n", addr, buf[:n])
Competitor Comparisons
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
Pros of gnet
- Higher performance in some benchmarks, especially for high-concurrency scenarios
- More flexible event-driven model with customizable event handlers
- Built-in load balancing and graceful shutdown features
Cons of gnet
- Less actively maintained compared to netpoll (fewer recent updates)
- More complex API, which may have a steeper learning curve
- Limited documentation and examples compared to netpoll
Code Comparison
gnet
type Server struct {
*gnet.EventServer
}
func (s *Server) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// Handle incoming data
return frame, gnet.None
}
netpoll
func onRequest(ctx context.Context, connection netpoll.Connection) error {
reader := connection.Reader()
writer := connection.Writer()
// Handle incoming data
return nil
}
Both libraries provide efficient network I/O operations, but gnet offers a more event-driven approach with customizable handlers, while netpoll focuses on simplicity and ease of use. gnet may have an edge in high-concurrency scenarios, but netpoll is more actively maintained and has better documentation. The choice between the two depends on specific project requirements and developer preferences.
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Pros of fasthttp
- Higher performance and lower memory usage for HTTP servers and clients
- Optimized for high-load scenarios with minimal memory allocations
- Extensive feature set including support for HTTP/1.1 and WebSockets
Cons of fasthttp
- Less compatibility with the standard net/http package
- Steeper learning curve due to its unique API design
- May require more manual memory management in some cases
Code Comparison
fasthttp
func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!")
}
fasthttp.ListenAndServe(":8080", requestHandler)
netpoll
func main() {
eventLoop, _ := netpoll.NewEventLoop(handler)
eventLoop.Serve(":8080")
}
Key Differences
- netpoll focuses on providing a high-performance network I/O framework, while fasthttp is specifically designed for HTTP
- netpoll offers more flexibility for various network protocols, whereas fasthttp is optimized for HTTP
- fasthttp provides a complete HTTP implementation, while netpoll requires additional layers for HTTP handling
Both libraries aim to improve performance over standard Go networking, but they approach the problem from different angles. fasthttp is more specialized for HTTP, while netpoll offers a broader network programming foundation.
Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
Pros of nbio
- Simpler API and easier to use for basic networking tasks
- Better support for WebSocket connections out of the box
- More flexible configuration options for fine-tuning performance
Cons of nbio
- Less optimized for high-performance scenarios compared to netpoll
- Fewer advanced features for complex networking requirements
- Smaller community and ecosystem around the project
Code Comparison
nbio:
engine := nbio.NewEngine(nbio.Config{
Network: "tcp",
Addr: ":8080",
})
engine.OnData(func(c *nbio.Conn, data []byte) {
c.Write(data)
})
netpoll:
eventLoop, _ := netpoll.NewEventLoop(
netpoll.WithOnPrepare(onPrepare),
netpoll.WithOnConnect(onConnect),
netpoll.WithOnRequest(onRequest),
)
eventLoop.Serve(listener)
Both nbio and netpoll are Go libraries for building high-performance network applications. nbio offers a simpler API and better WebSocket support, making it easier to use for basic networking tasks. However, netpoll is more optimized for high-performance scenarios and provides more advanced features for complex networking requirements. The code comparison shows that nbio has a more straightforward setup process, while netpoll offers more granular control over event handling.
Fast event-loop networking for Go
Pros of evio
- Simpler API and easier to use for basic networking tasks
- Supports both TCP and UDP protocols out of the box
- Includes built-in load balancing capabilities
Cons of evio
- Less optimized for high-performance scenarios compared to netpoll
- Limited customization options for advanced use cases
- Lacks some of the more advanced features found in netpoll
Code Comparison
evio example:
func main() {
var events evio.Events
events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
out = in
return
}
if err := evio.Serve(events, "tcp://localhost:5000"); err != nil {
panic(err)
}
}
netpoll example:
func main() {
eventLoop, _ := netpoll.NewEventLoop(
func(ctx context.Context, connection netpoll.Connection) error {
_, err := connection.Write([]byte("Hello, World!"))
return err
},
)
_ = eventLoop.Serve("tcp://localhost:8888")
}
Both libraries provide event-driven networking capabilities, but netpoll offers more advanced features and optimizations for high-performance scenarios. evio has a simpler API and is easier to use for basic tasks, while netpoll provides more customization options and is better suited for complex, high-throughput applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
CloudWeGo-Netpoll
Introduction
Netpoll is a high-performance non-blocking I/O networking framework, which focused on RPC scenarios, developed by ByteDance.
RPC is usually heavy on processing logic and therefore cannot handle I/O serially. But Go's standard library net is designed for blocking I/O APIs, so that the RPC framework can only follow the One Conn One Goroutine design. It will waste a lot of cost for context switching, due to a large number of goroutines under high concurrency. Besides, net.Conn has no API to check Alive, so it is difficult to make an efficient connection pool for RPC framework, because there may be a large number of failed connections in the pool.
On the other hand, the open source community currently lacks Go network libraries that focus on RPC scenarios. Similar repositories such as: evio, gnet, etc., are all focus on scenarios like Redis, HAProxy.
But now, Netpoll was born and solved the above problems. It draws inspiration from the design of evio and netty, has excellent Performance, and is more suitable for microservice architecture. Also Netpoll provides a number of Features, and it is recommended to replace net in some RPC scenarios.
We developed the RPC framework Kitex and HTTP framework Hertz based on Netpoll, both with industry-leading performance.
Examples show how to build RPC client and server using Netpoll.
For more information, please refer to Document.
Features
-
Already
- LinkBuffer provides nocopy API for streaming reading and writing
- gopool provides high-performance goroutine pool
- mcache provides efficient memory reuse
IsActive
supports checking whether the connection is aliveDialer
supports building clientsEventLoop
supports building a server- TCP, Unix Domain Socket
- Linux, macOS (operating system)
-
Future
- io_uring
- Shared Memory IPC
- TLS
- UDP
-
Unsupported
- Windows (operating system)
Performance
Benchmark should meet the requirements of industrial use. In the RPC scenario, concurrency and timeout are necessary support items.
We provide the netpoll-benchmark project to track and compare the performance of Netpoll and other frameworks under different conditions for reference.
More benchmarks reference kitex-benchmark and hertz-benchmark.
Reference
Top Related Projects
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
Fast event-loop networking for Go
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot