reuseport

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: AGPL-3.0, MIT, MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package reuseport provides TCP net.Listener with SO_REUSEPORT support.

SO_REUSEPORT allows linear scaling server performance on multi-CPU servers. See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for more details :)

The package is based on https://github.com/kavu/go_reuseport .

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(network, addr string) (net.Listener, error)

Listen returns TCP listener with SO_REUSEPORT option set.

The returned listener tries enabling the following TCP options, which usually have positive impact on performance:

  • TCP_DEFER_ACCEPT. This option expects that the server reads from accepted connections before writing to them.

- TCP_FASTOPEN. See https://lwn.net/Articles/508865/ for details.

Use https://github.com/valyala/tcplisten if you want customizing these options.

Only tcp4 and tcp6 networks are supported.

ErrNoReusePort error is returned if the system doesn't support SO_REUSEPORT.

Example
package main

import (
	"fmt"
	"log"

	"infini.sh/framework/lib/fasthttp"
	"infini.sh/framework/lib/fasthttp/reuseport"
)

func main() {
	ln, err := reuseport.Listen("tcp4", "localhost:12345")
	if err != nil {
		log.Fatalf("error in reuseport listener: %v", err)
	}

	if err = fasthttp.Serve(ln, requestHandler); err != nil {
		log.Fatalf("error in fasthttp Server: %v", err)
	}
}

func requestHandler(ctx *fasthttp.RequestCtx) {
	fmt.Fprintf(ctx, "Hello, world!")
}

Types

type ErrNoReusePort

type ErrNoReusePort struct {
	// contains filtered or unexported fields
}

ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT.

func (*ErrNoReusePort) Error

func (e *ErrNoReusePort) Error() string

Error implements error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL