reuseport

package module
v0.0.0-...-6918993 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2016 License: MIT Imports: 7 Imported by: 7 modules, 6 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.

README

GO_REUSEPORT

Go Report Card Build Status codecov GoDoc

GO_REUSEPORT is a little expirement to create a net.Listener that supports SO_REUSEPORT socket option.

For now, Darwin and Linux (from 3.9) systems are supported. I'll be pleased if you'll test other systems and tell me the results. documentation on godoc.org.

Example

package main

import (
  "fmt"
  "html"
  "net/http"
  "os"
  "runtime"
  "github.com/funny/reuseport"
)

func main() {
  runtime.GOMAXPROCS(runtime.NumCPU())

  listener, err := reuseport.NewReusablePortListener("tcp4", "localhost:8881")
  if err != nil {
    panic(err)
  }
  defer listener.Close()

  server := &http.Server{}
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Println(os.Getgid())
    fmt.Fprintf(w, "Hello, %q\n", html.EscapeString(r.URL.Path))
  })

  panic(server.Serve(listener))
}

Now you can run several instances of this tiny server without Address already in use errors.

Thanks

Inspired by Artur Siekielski post about SO_REUSEPORT.

Documentation

Overview

Package reuseport provides a function that returns a net.Listener powered by a net.FileListener with a SO_REUSEPORT option set to the socket.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReusablePortListener

func NewReusablePortListener(proto, addr string) (l net.Listener, err error)

NewReusablePortListener returns net.FileListener that created from a file discriptor for a socket with SO_REUSEPORT option.

Types

This section is empty.

Jump to

Keyboard shortcuts

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