All Projects → hslam → netpoll

hslam / netpoll

Licence: MIT license
Package netpoll implements a network poller based on epoll/kqueue.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to netpoll

Gnet
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go./ gnet 是一个高性能、轻量级、非阻塞的事件驱动 Go 网络框架。
Stars: ✭ 5,736 (+14994.74%)
Mutual labels:  event-driven, kqueue, epoll, non-blocking
ev
Lightweight event-loop library based on multiplexing IO
Stars: ✭ 15 (-60.53%)
Mutual labels:  event-driven, kqueue, epoll
libapenetwork
Fast cross-platform async network library
Stars: ✭ 17 (-55.26%)
Mutual labels:  event-driven, kqueue, epoll
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (-7.89%)
Mutual labels:  kqueue, epoll, non-blocking
epump
ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。
Stars: ✭ 26 (-31.58%)
Mutual labels:  event-driven, epoll
conn
netpoll事件驱动,goroutine协程池化,降低无效协程的资源占用,适用于高连接数(对于低频数据传输的场景,可以大幅降低协程数,提升资源利用率)
Stars: ✭ 28 (-26.32%)
Mutual labels:  event-driven, netpoll
sol
Lightweight MQTT broker, written from scratch. IO is handled by a super simple event loop based upon the most common IO multiplexing implementations.
Stars: ✭ 72 (+89.47%)
Mutual labels:  event-driven, epoll
Vino
Vino is a lightweight and efficient web server.
Stars: ✭ 181 (+376.32%)
Mutual labels:  epoll, non-blocking
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+744.74%)
Mutual labels:  event-driven, non-blocking
Swift Nio
Event-driven network application framework for high performance protocol servers & clients, non-blocking.
Stars: ✭ 6,777 (+17734.21%)
Mutual labels:  event-driven, non-blocking
Libuev
Lightweight event loop library for Linux epoll() family APIs
Stars: ✭ 170 (+347.37%)
Mutual labels:  event-driven, epoll
llb
Dead simple event-driven load-balancer
Stars: ✭ 27 (-28.95%)
Mutual labels:  event-driven, epoll
xm2cloud term
powerful webssh that developed with django, channels, xterm,ioloop
Stars: ✭ 17 (-55.26%)
Mutual labels:  kqueue, epoll
hev-task-system
A simple, lightweight multi-task system (coroutines) for Unix (Linux/BSD/macOS)
Stars: ✭ 41 (+7.89%)
Mutual labels:  kqueue, epoll
Gev
🚀Gev is a lightweight, fast non-blocking TCP network library based on Reactor mode. Support custom protocols to quickly and easily build high-performance servers.
Stars: ✭ 1,082 (+2747.37%)
Mutual labels:  event-driven, epoll
Socket
Non-blocking socket and TLS functionality for PHP based on Amp.
Stars: ✭ 122 (+221.05%)
Mutual labels:  unix, non-blocking
netman
高性能的TCP网络框架、支持TLS、可配置的路由、websocket、基于事件循环(epoll),百万连接(C1000K)
Stars: ✭ 96 (+152.63%)
Mutual labels:  epoll, non-blocking
fastsocket
⚠️⚠️⚠️ DEPRECATED
Stars: ✭ 14 (-63.16%)
Mutual labels:  kqueue, epoll
Zeus
A high performance, cross-platform Internet Communication Engine. Developed with native socket API. Aim at handling millions of concurrent connections.
Stars: ✭ 30 (-21.05%)
Mutual labels:  event-driven, epoll
jobflow
runs stuff in parallel (like GNU parallel, but much faster and memory-efficient)
Stars: ✭ 67 (+76.32%)
Mutual labels:  fast, unix

netpoll

PkgGoDev Build Status codecov Go Report Card LICENSE

Package netpoll implements a network poller based on epoll/kqueue.

Features

  • Epoll/kqueue
  • TCP/UNIX
  • Compatible with the net.Conn interface.
  • Upgrade connection
  • Non-blocking I/O
  • Splice/sendfile
  • Rescheduling workers

Comparison to other packages.

Package net netpoll gnet evio
Low memory usage No Yes Yes Yes
Non-blocking I/O No Yes Yes Yes
Splice/sendfile Yes Yes No No
Rescheduling Yes Yes No No
Compatible with the net.Conn interface Yes Yes No No

Benchmark

mock 0msmock 1ms

Get started

Install

go get github.com/hslam/netpoll

Import

import "github.com/hslam/netpoll"

Usage

Simple Example

package main

import "github.com/hslam/netpoll"

func main() {
	var handler = &netpoll.DataHandler{
		NoShared:   true,
		NoCopy:     true,
		BufferSize: 1024,
		HandlerFunc: func(req []byte) (res []byte) {
			res = req
			return
		},
	}
	if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
		panic(err)
	}
}

TLS Example

package main

import (
	"crypto/tls"
	"github.com/hslam/netpoll"
	"github.com/hslam/socket"
	"net"
)

func main() {
	var handler = &netpoll.DataHandler{
		NoShared:   true,
		NoCopy:     true,
		BufferSize: 1024,
		HandlerFunc: func(req []byte) (res []byte) {
			res = req
			return
		},
	}
	handler.SetUpgrade(func(conn net.Conn) (net.Conn, error) {
		tlsConn := tls.Server(conn, socket.DefalutTLSConfig())
		if err := tlsConn.Handshake(); err != nil {
			return nil, err
		}
		return tlsConn, nil
	})
	if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
		panic(err)
	}
}

Websocket Example

package main

import (
	"github.com/hslam/netpoll"
	"github.com/hslam/websocket"
	"net"
)

func main() {
	var handler = &netpoll.ConnHandler{}
	handler.SetUpgrade(func(conn net.Conn) (netpoll.Context, error) {
		return websocket.Upgrade(conn, nil)
	})
	handler.SetServe(func(context netpoll.Context) error {
		ws := context.(*websocket.Conn)
		msg, err := ws.ReadMessage()
		if err != nil {
			return err
		}
		return ws.WriteMessage(msg)
	})
	if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
		panic(err)
	}
}

HTTP Example

package main

import (
	"bufio"
	"github.com/hslam/mux"
	"github.com/hslam/netpoll"
	"github.com/hslam/response"
	"net"
	"net/http"
	"sync"
)

func main() {
	m := mux.New()
	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	ListenAndServe(":8080", m)
}

func ListenAndServe(addr string, handler http.Handler) error {
	var h = &netpoll.ConnHandler{}
	type Context struct {
		reader  *bufio.Reader
		rw      *bufio.ReadWriter
		conn    net.Conn
		serving sync.Mutex
	}
	h.SetUpgrade(func(conn net.Conn) (netpoll.Context, error) {
		reader := bufio.NewReader(conn)
		rw := bufio.NewReadWriter(reader, bufio.NewWriter(conn))
		return &Context{reader: reader, conn: conn, rw: rw}, nil
	})
	h.SetServe(func(context netpoll.Context) error {
		ctx := context.(*Context)
		ctx.serving.Lock()
		req, err := http.ReadRequest(ctx.reader)
		if err != nil {
			ctx.serving.Unlock()
			return err
		}
		res := response.NewResponse(req, ctx.conn, ctx.rw)
		handler.ServeHTTP(res, req)
		res.FinishRequest()
		ctx.serving.Unlock()
		response.FreeResponse(res)
		return nil
	})
	return netpoll.ListenAndServe("tcp", addr, h)
}

License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

netpoll was written by Meng Huang.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].