Bump v1.2.2

pull/6/head
Fenny 2020-01-31 15:19:57 -05:00
parent c195450790
commit 29091a54b4
9 changed files with 61 additions and 77 deletions

View File

@ -13,7 +13,7 @@ import (
)
const (
Version = "1.2.1"
Version = "1.2.2"
// https://play.golang.org/p/r6GNeV1gbH
banner = "" +
" \x1b[1;32m _____ _ _\n" +
@ -38,6 +38,7 @@ type Fiber struct {
Engine *engine
// https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
Prefork bool
child bool
// Stores all routes
routes []*route
}
@ -72,6 +73,7 @@ func New() *Fiber {
Server: "",
Banner: true,
Prefork: *prefork,
child: *child,
Engine: &engine{
Concurrency: 256 * 1024,
DisableKeepAlive: false,

View File

@ -827,7 +827,7 @@ Sends the HTTP response.
The Send parameters can be of any type
```go
// Function signature
c.Send(bodies ...interface{})
c.Send(body ...interface{})
// Example
app.Get("/", func(c *fiber.Ctx) {

View File

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

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=

102
listen.go
View File

@ -17,6 +17,7 @@ import (
"strings"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
)
// Listen : https://gofiber.github.io/fiber/#/application?id=listen
@ -24,19 +25,16 @@ func (r *Fiber) Listen(address interface{}, tls ...string) {
host := ""
switch val := address.(type) {
case int:
// 8080 => ":8080"
host = ":" + strconv.Itoa(val)
host = ":" + strconv.Itoa(val) // 8080 => ":8080"
case string:
// Address needs to contain a semicolon
if !strings.Contains(val, ":") {
// "8080" => ":8080"
val = ":" + val
val = ":" + val // "8080" => ":8080"
}
host = val
default:
panic("Host must be an INT port or STRING address")
}
// Copy settings to fasthttp server
// Create fasthttp server
server := &fasthttp.Server{
Handler: r.handler,
Name: r.Server,
@ -60,83 +58,69 @@ func (r *Fiber) Listen(address interface{}, tls ...string) {
NoDefaultContentType: r.Engine.NoDefaultContentType,
KeepHijackedConns: r.Engine.KeepHijackedConns,
}
// Print banner if enabled, ignore if child proccess
if r.Banner && !*child {
if r.Prefork {
fmt.Printf(banner, Version, "-prefork", "Express on steriods", host)
} else {
fmt.Printf(banner, Version, "", "Express on steriods", host)
}
}
// Create listener
var listener net.Listener
var err error
// If prefork enabled & enough cores
// Prefork enabled
if r.Prefork && runtime.NumCPU() > 1 {
listener, err = r.reuseport(host)
if err != nil {
panic(err)
if r.Banner && !r.child {
cores := fmt.Sprintf("%s\x1b[1;30m %v cores", host, runtime.NumCPU())
fmt.Printf(banner, Version, " prefork", "Express on steriods", cores)
}
} else {
listener, err = net.Listen("tcp4", host)
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(runtime.NumCPU())
r.prefork(server, host, tls...)
}
// Check ssl files are provided
// Prefork disabled
if r.Banner {
fmt.Printf(banner, Version, "", "Express on steriods", host)
}
ln, err := net.Listen("tcp4", host)
if err != nil {
panic(err)
}
// enable TLS/HTTPS
if len(tls) > 1 {
if err := server.ServeTLS(listener, tls[0], tls[1]); err != nil {
if err := server.ServeTLS(ln, tls[0], tls[1]); err != nil {
panic(err)
}
}
if err := server.Serve(listener); err != nil {
if err := server.Serve(ln); err != nil {
panic(err)
}
}
// TODO: enable ipv6 support ~ tcp4 > tcp = tcp4+tcp6
// https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
func (r *Fiber) reuseport(host string) (net.Listener, error) {
var listener net.Listener
if !*child {
addr, err := net.ResolveTCPAddr("tcp4", host)
if err != nil {
return nil, err
}
tcplistener, err := net.ListenTCP("tcp4", addr)
if err != nil {
return nil, err
}
file, err := tcplistener.File()
if err != nil {
return nil, err
}
func (r *Fiber) prefork(server *fasthttp.Server, host string, tls ...string) {
// Master proc
if !r.child {
// Create babies
childs := make([]*exec.Cmd, runtime.NumCPU())
for i := range childs {
childs[i] = exec.Command(os.Args[0], append(os.Args[1:], "-child")...)
childs[i] = exec.Command(os.Args[0], "-prefork", "-child")
childs[i].Stdout = os.Stdout
childs[i].Stderr = os.Stderr
childs[i].ExtraFiles = []*os.File{file}
if err := childs[i].Start(); err != nil {
return nil, err
panic(err)
}
}
for _, child := range childs {
if err := child.Wait(); err != nil {
panic(err)
}
}
os.Exit(0)
panic("Problem with calling os.Exit(0)")
} else {
// fmt.Printf(" \x1b[1;30mChild \x1b[1;32m#%v\x1b[1;30m reuseport\x1b[1;32m%s\x1b[0000m\n", os.Getpid(), host)
var err error
listener, err = net.FileListener(os.NewFile(3, ""))
if err != nil {
return nil, err
}
runtime.GOMAXPROCS(1)
}
return listener, nil
// Child proc
runtime.GOMAXPROCS(1)
ln, err := reuseport.Listen("tcp4", host)
if err != nil {
panic(err)
}
// enable TLS/HTTPS
if len(tls) > 1 {
if err := server.ServeTLS(ln, tls[0], tls[1]); err != nil {
panic(err)
}
}
if err := server.Serve(ln); err != nil {
panic(err)
}
}

View File

@ -152,10 +152,6 @@ func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
}
// Body : https://gofiber.github.io/fiber/#/context?id=body
// curl -X POST \
// http://localhost:8080 \
// -H 'Content-Type: application/x-www-form-urlencoded' \
// -d john=doe
func (ctx *Ctx) Body(args ...interface{}) string {
if len(args) == 0 {
return getString(ctx.Fasthttp.Request.Body())

View File

@ -218,15 +218,16 @@ func (ctx *Ctx) Render() {
// Send : https://gofiber.github.io/fiber/#/context?id=send
func (ctx *Ctx) Send(args ...interface{}) {
for i := range args {
switch body := args[i].(type) {
case string:
ctx.Fasthttp.Response.SetBodyString(body)
case []byte:
ctx.Fasthttp.Response.SetBodyString(getString(body))
default:
ctx.Fasthttp.Response.SetBodyString(fmt.Sprintf("%v", body))
}
if len(args) == 0 {
return
}
switch body := args[0].(type) {
case string:
ctx.Fasthttp.Response.SetBodyString(body)
case []byte:
ctx.Fasthttp.Response.SetBodyString(getString(body))
default:
ctx.Fasthttp.Response.SetBodyString(fmt.Sprintf("%v", body))
}
}

View File

@ -7,6 +7,7 @@
package fiber
// common content types
const (
contentTypeJson = "application/json"
contentTypeJs = "application/javascript"

View File

@ -71,8 +71,7 @@ func getType(ext string) (mime string) {
return mime
}
func getStatus(status int) (msg string) {
msg = statusMessages[status]
return msg
return statusMessages[status]
}
func getString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))