Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Mar 18, 2019
0 parents commit f4fb6a9
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js linguist-language=GO
*.css linguist-language=GO
*.html linguist-language=GO
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.buildpath
.hgignore.swp
.project
.orig
.swp
.idea/
.settings/
.vscode/
vender/
log/
composer.lock
gitpush.sh
pkg/
bin/
cbuild
**/.DS_Store
.vscode/
go.sum

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 [email protected] https://goframe.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# greuseport [![Go Doc](https://godoc.org/github.com/gogf/greuseport?status.svg)](https://godoc.org/github.com/gogf/greuseport) [![Documents](https://img.shields.io/badge/docs-100%25-green.svg)](https://goframe.org) [![License](https://img.shields.io/github/license/gogf/greuseport.svg?style=flat)](https://github.com/gogf/greuseport) [![Language](https://img.shields.io/badge/language-go-blue.svg)](https://github.com/gogf/greuseport) [![Release](https://img.shields.io/github/release/gogf/greuseport.svg?style=flat)](https://github.com/gogf/greuseport/releases)

Package greuseport provides Listen and Dial functions that set socket options in order to be able to reuse ports. You should only use this package if you know what SO_REUSEADDR and SO_REUSEPORT are.

# Installation
```
go get -u github.com/gogf/greuseport
```
or use `go.mod`
```
require github.com/gogf/greuseport latest
```

# Documentation

* [https://godoc.org/github.com/gogf/greuseport](https://godoc.org/github.com/gogf/greuseport)

# Quick Start
```go
package main

import (
"fmt"
"github.com/gogf/greuseport"
"net/http"
"os"
)

// We can create two processes with this code.
// Do some requests, then watch the output of the console.
func main() {
listener, err := greuseport.Listen("tcp", ":8881")
if err != nil {
panic(err)
}
defer listener.Close()

server := &http.Server{}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "gid: %d, pid: %d\n", os.Getgid(), os.Getpid())
})

panic(server.Serve(listener))
}
```


# License

`greuseport` is licensed under the [MIT License](LICENSE), 100% free and open-source, forever.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/gogf/greuseport
59 changes: 59 additions & 0 deletions greuseport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Package greuseport provides Listen and Dial functions that set socket
// options in order to be able to reuse ports. You should only use this
// package if you know what SO_REUSEADDR and SO_REUSEPORT are.
//
// For example:
//
// // listen on the same port.
// l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
// l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")
//
// // dial from the same port.
// l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
// l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
// c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")
//
// Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections,
// and doing so would clash.
package greuseport

import (
"context"
"net"
)

var (
Enabled = false
listenConfig = net.ListenConfig {
Control: Control,
}
)

// Listen listens at the given network and address. see net.Listen
// Returns a net.Listener created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func Listen(network, address string) (net.Listener, error) {
return listenConfig.Listen(context.Background(), network, address)
}

// ListenPacket listens at the given network and address. see net.ListenPacket
// Returns a net.Listener created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func ListenPacket(network, address string) (net.PacketConn, error) {
return listenConfig.ListenPacket(context.Background(), network, address)
}

// Dial dials the given network and address. see net.Dialer.Dial
// Returns a net.Conn created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func Dial(network, laddr, raddr string) (net.Conn, error) {
nla, err := ResolveAddr(network, laddr)
if err != nil {
return nil, err
}
d := net.Dialer {
Control: Control,
LocalAddr: nla,
}
return d.Dial(network, raddr)
}
20 changes: 20 additions & 0 deletions greuseport_addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package greuseport

import (
"net"
)

func ResolveAddr(network, address string) (net.Addr, error) {
switch network {
case "ip", "ip4", "ip6":
return net.ResolveIPAddr(network, address)
case "tcp", "tcp4", "tcp6":
return net.ResolveTCPAddr(network, address)
case "udp", "udp4", "udp6":
return net.ResolveUDPAddr(network, address)
case "unix", "unixgram", "unixpacket":
return net.ResolveUnixAddr(network, address)
default:
return nil, net.UnknownNetworkError(network)
}
}
12 changes: 12 additions & 0 deletions greuseport_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !windows,!linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd

package greuseport

import (
"syscall"
)

// See net.RawConn.Control
func Control(network, address string, c syscall.RawConn) (err error) {
return nil
}
28 changes: 28 additions & 0 deletions greuseport_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build linux darwin dragonfly freebsd netbsd openbsd

package greuseport

import (
"github.com/gogf/gf/third/golang.org/x/sys/unix"
"syscall"
)

func init() {
Enabled = true
}

// See net.RawConn.Control
func Control(network, address string, c syscall.RawConn) (err error) {
c.Control(func(fd uintptr) {
if err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
panic(err)
return
}
if err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
panic(err)
return
}

})
return
}
17 changes: 17 additions & 0 deletions greuseport_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build windows

package greuseport

import (
"github.com/gogf/gf/third/golang.org/x/sys/windows"
"syscall"
)

// See net.RawConn.Control
func Control(network, address string, c syscall.RawConn) (err error) {
return c.Control(func(fd uintptr) {
if err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1); err != nil {
return
}
})
}
Loading

0 comments on commit f4fb6a9

Please sign in to comment.