replace zxq.co/ripple/hanayo

This commit is contained in:
Alicia
2019-02-23 13:29:15 +00:00
commit c3d206c173
5871 changed files with 1353715 additions and 0 deletions

21
vendor/github.com/valyala/fasthttp/reuseport/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Max Riveiro
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.

View File

@@ -0,0 +1,106 @@
// +build linux darwin dragonfly freebsd netbsd openbsd rumprun
// 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 .
package reuseport
import (
"errors"
"fmt"
"net"
"os"
"syscall"
)
func getSockaddr(network, addr string) (sa syscall.Sockaddr, soType int, err error) {
// TODO: add support for tcp and tcp6 networks.
if network != "tcp4" {
return nil, -1, errors.New("only tcp4 network is supported")
}
tcpAddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, -1, err
}
var sa4 syscall.SockaddrInet4
sa4.Port = tcpAddr.Port
copy(sa4.Addr[:], tcpAddr.IP.To4())
return &sa4, syscall.AF_INET, nil
}
// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT.
type ErrNoReusePort struct {
err error
}
// Error implements error interface.
func (e *ErrNoReusePort) Error() string {
return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err)
}
// Listen returns TCP listener with SO_REUSEPORT option set.
//
// Only tcp4 network is supported.
//
// ErrNoReusePort error is returned if the system doesn't support SO_REUSEPORT.
func Listen(network, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(network, addr); err != nil {
return nil, err
}
syscall.ForkLock.RLock()
fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err == nil {
syscall.CloseOnExec(fd)
}
syscall.ForkLock.RUnlock()
if err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, soReusePort, 1); err != nil {
syscall.Close(fd)
return nil, &ErrNoReusePort{err}
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
name := fmt.Sprintf("reuseport.%d.%s.%s", os.Getpid(), network, addr)
file = os.NewFile(uintptr(fd), name)
if l, err = net.FileListener(file); err != nil {
file.Close()
return nil, err
}
if err = file.Close(); err != nil {
l.Close()
return nil, err
}
return l, err
}

View File

@@ -0,0 +1,9 @@
// +build darwin dragonfly freebsd netbsd openbsd rumprun
package reuseport
import (
"syscall"
)
const soReusePort = syscall.SO_REUSEPORT

View File

@@ -0,0 +1,24 @@
package reuseport_test
import (
"fmt"
"log"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
)
func ExampleListen() {
ln, err := reuseport.Listen("tcp4", "localhost:12345")
if err != nil {
log.Fatalf("error in reuseport listener: %s", err)
}
if err = fasthttp.Serve(ln, requestHandler); err != nil {
log.Fatalf("error in fasthttp Server: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!")
}

View File

@@ -0,0 +1,5 @@
// +build linux
package reuseport
const soReusePort = 0x0F

View File

@@ -0,0 +1,98 @@
package reuseport
import (
"fmt"
"io/ioutil"
"net"
"testing"
"time"
)
func TestNewListener(t *testing.T) {
addr := "localhost:10081"
serversCount := 20
requestsCount := 1000
var lns []net.Listener
doneCh := make(chan struct{}, serversCount)
for i := 0; i < serversCount; i++ {
ln, err := Listen("tcp4", addr)
if err != nil {
t.Fatalf("cannot create listener %d: %s", i, err)
}
go func() {
serveEcho(t, ln)
doneCh <- struct{}{}
}()
lns = append(lns, ln)
}
for i := 0; i < requestsCount; i++ {
c, err := net.Dial("tcp4", addr)
if err != nil {
t.Fatalf("%d. unexpected error when dialing: %s", i, err)
}
req := fmt.Sprintf("request number %d", i)
if _, err = c.Write([]byte(req)); err != nil {
t.Fatalf("%d. unexpected error when writing request: %s", i, err)
}
if err = c.(*net.TCPConn).CloseWrite(); err != nil {
t.Fatalf("%d. unexpected error when closing write end of the connection: %s", i, err)
}
var resp []byte
ch := make(chan struct{})
go func() {
if resp, err = ioutil.ReadAll(c); err != nil {
t.Fatalf("%d. unexpected error when reading response: %s", i, err)
}
close(ch)
}()
select {
case <-ch:
case <-time.After(200 * time.Millisecond):
t.Fatalf("%d. timeout when waiting for response: %s", i, err)
}
if string(resp) != req {
t.Fatalf("%d. unexpected response %q. Expecting %q", i, resp, req)
}
if err = c.Close(); err != nil {
t.Fatalf("%d. unexpected error when closing connection: %s", i, err)
}
}
for _, ln := range lns {
if err := ln.Close(); err != nil {
t.Fatalf("unexpected error when closing listener: %s", err)
}
}
for i := 0; i < serversCount; i++ {
select {
case <-doneCh:
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout when waiting for servers to be closed")
}
}
}
func serveEcho(t *testing.T, ln net.Listener) {
for {
c, err := ln.Accept()
if err != nil {
break
}
req, err := ioutil.ReadAll(c)
if err != nil {
t.Fatalf("unepxected error when reading request: %s", err)
}
if _, err = c.Write(req); err != nil {
t.Fatalf("unexpected error when writing response: %s", err)
}
if err = c.Close(); err != nil {
t.Fatalf("unexpected error when closing connection: %s", err)
}
}
}