v0.9.0 - Prefork (SO_REUSEPORT)

pull/6/head
Fenny 2020-01-17 21:25:11 +01:00
parent 961243b6be
commit 93e5e0e891
4 changed files with 69 additions and 13 deletions

View File

@ -19,7 +19,7 @@
<script>
window.$docsify = {
name: 'Fiber v0.8.1',
name: 'Fiber v0.9.0',
repo: 'gofiber/fiber',
loadSidebar: "sidebar.md",
homepage: 'getting_started.md',

6
go.mod
View File

@ -1,8 +1,8 @@
module github.com/gofiber/fiber
module fiber
go 1.13
require (
github.com/json-iterator/go v1.1.9
github.com/valyala/fasthttp v1.8.0
)
go 1.13

1
go.sum
View File

@ -18,6 +18,7 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.8.0 h1:actnGGBYtGQmxVaZxyZpp57Vcc2NhcO7mMN0IMwCC0w=
github.com/valyala/fasthttp v1.8.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

View File

@ -8,19 +8,28 @@
package fiber
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
// This json parsing lib is awesome
// "github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
)
// Version for debugging
const Version = "0.8.1"
const Version = "0.9.0"
var child = flag.Bool("fiber-child", false, "is child process")
// Fiber structure
type Fiber struct {
@ -30,13 +39,14 @@ type Fiber struct {
Server string
// Disable the fiber banner on launch
Banner bool
// RedirectTrailingSlash TODO*
RedirectTrailingSlash bool
// Provide certificate files to enable TLS
CertKey string
CertFile string
// Fasthttp server settings
Fasthttp *Fasthttp
// ALPHA SETTINGS, DO NOT USE!
RedirectTrailingSlash bool
Prefork bool
}
type route struct {
@ -371,18 +381,63 @@ func (r *Fiber) Listen(port int, addr ...string) {
NoDefaultContentType: r.Fasthttp.NoDefaultContentType,
KeepHijackedConns: r.Fasthttp.KeepHijackedConns,
}
if r.Banner {
flag.Parse()
if r.Banner && !*child {
// https://play.golang.org/p/r6GNeV1gbH
// http://patorjk.com/software/taag
fmt.Printf("\x1b[1;32m _____ _ _\n \x1b[1;32m| __|_| |_ ___ ___\n \x1b[1;32m| __| | . | -_| _|\n \x1b[1;32m|__| |_|___|___|_|\x1b[1;30m%s\n \x1b[1;30m%s\x1b[1;32m%v\x1b[0000m\n\n", Version, "Express on steriods:", port)
}
if r.CertKey != "" && r.CertFile != "" {
if err := server.ListenAndServeTLS(fmt.Sprintf("%s:%v", address, port), r.CertFile, r.CertKey); err != nil {
panic(err)
if r.Prefork {
ln := getListener(port, address)
if r.CertKey != "" && r.CertFile != "" {
if err := server.ServeTLS(ln, r.CertFile, r.CertKey); err != nil {
panic(err)
}
} else {
if err := server.Serve(ln); err != nil {
panic(err)
}
}
} else {
if err := server.ListenAndServe(fmt.Sprintf("%s:%v", address, port)); err != nil {
panic(err)
runtime.GOMAXPROCS(runtime.NumCPU())
if r.CertKey != "" && r.CertFile != "" {
if err := server.ListenAndServeTLS(fmt.Sprintf("%s:%v", address, port), r.CertFile, r.CertKey); err != nil {
panic(err)
}
} else {
if err := server.ListenAndServe(fmt.Sprintf("%s:%v", address, port)); err != nil {
panic(err)
}
}
}
}
func getListener(port int, address string) net.Listener {
var addr = fmt.Sprintf("%s:%v", address, port)
// ‍👩‍👧 We are a parent
if !*child {
// 👶 Make some babies
fmt.Printf(" \x1b[1;30mStarting \x1b[1;32m%v\x1b[1;30m childs on port \x1b[1;32m%v\x1b[0000m\n\n", runtime.NumCPU(), port)
children := make([]*exec.Cmd, runtime.NumCPU())
for i := range children {
children[i] = exec.Command(os.Args[0], "-fiber-child")
children[i].Stdout = os.Stdout
children[i].Stderr = os.Stderr
if err := children[i].Start(); err != nil {
panic(err)
}
}
for _, ch := range children {
if err := ch.Wait(); err != nil {
log.Print(err)
}
}
}
runtime.GOMAXPROCS(1)
ln, err := reuseport.Listen("tcp4", addr)
if err != nil {
panic(err)
}
return ln
}