mirror of https://github.com/gofiber/fiber.git
Add memory info
parent
badb45779f
commit
ec51458940
73
app.go
73
app.go
|
@ -23,7 +23,7 @@ import (
|
|||
"time"
|
||||
|
||||
utils "github.com/gofiber/utils"
|
||||
colorable "github.com/segrey/go-colorable"
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
fasthttp "github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
|
@ -427,7 +427,7 @@ func (app *App) Serve(ln net.Listener, tlsconfig ...*tls.Config) error {
|
|||
}
|
||||
// Print startup message
|
||||
if !app.Settings.DisableStartupMessage {
|
||||
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0)
|
||||
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0, "")
|
||||
}
|
||||
return app.server.Serve(ln)
|
||||
}
|
||||
|
@ -464,7 +464,7 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
|
|||
}
|
||||
// Print startup message
|
||||
if !app.Settings.DisableStartupMessage {
|
||||
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0)
|
||||
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0, "")
|
||||
}
|
||||
// Start listening
|
||||
return app.server.Serve(ln)
|
||||
|
@ -611,7 +611,7 @@ func (app *App) init() *App {
|
|||
const (
|
||||
cBlack = "\u001b[90m"
|
||||
// cRed = "\u001b[91m"
|
||||
cGreen = "\u001b[92m"
|
||||
// cGreen = "\u001b[92m"
|
||||
// cYellow = "\u001b[93m"
|
||||
// cBlue = "\u001b[94m"
|
||||
// cMagenta = "\u001b[95m"
|
||||
|
@ -620,31 +620,58 @@ const (
|
|||
cReset = "\u001b[0m"
|
||||
)
|
||||
|
||||
func (app *App) startupMessage(addr string, tls bool, pids ...[]string) {
|
||||
childs := ""
|
||||
if len(pids) > 0 {
|
||||
childs = "," + strings.Join(pids[0], ",")
|
||||
type colors struct {
|
||||
base string
|
||||
}
|
||||
|
||||
func (c *colors) Cyan(v interface{}) string {
|
||||
return fmt.Sprintf("%s%v%s", cCyan, v, c.base)
|
||||
}
|
||||
|
||||
func (app *App) startupMessage(addr string, tls bool, pids string) {
|
||||
// ignore child processes
|
||||
if utils.GetArgument(flagChild) {
|
||||
return
|
||||
}
|
||||
logo := ` %s_______ __
|
||||
%s____%s / ____(_) /_ ___ _____ %s
|
||||
%s_____%s / /_ / / __ \/ _ \/ ___/ %s
|
||||
%s__%s / __/ / / /_/ / __/ / %s
|
||||
/_/ /_/_.___/\___/_/%s %s`
|
||||
host := strings.Split(addr, ":")[0]
|
||||
port := strings.Split(addr, ":")[1]
|
||||
//
|
||||
var logo string
|
||||
logo += `%s _______ __ %s` + "\n"
|
||||
logo += `%s ____%s / ____(_) /_ ___ _____ %s` + "\n"
|
||||
logo += `%s_____%s / /_ / / __ \/ _ \/ ___/ %s` + "\n"
|
||||
logo += `%s __%s / __/ / / /_/ / __/ / %s` + "\n"
|
||||
logo += `%s /_/ /_/_.___/\___/_/%s %s` + "\n"
|
||||
|
||||
// statup details
|
||||
var (
|
||||
host = strings.Split(addr, ":")[0]
|
||||
port = strings.Split(addr, ":")[1]
|
||||
tlsStr = "FALSE"
|
||||
routesLen = len(app.Routes())
|
||||
osName = utils.ToUpper(runtime.GOOS)
|
||||
memTotal = utils.ByteSize(utils.MemoryTotal())
|
||||
cpuCores = runtime.NumCPU()
|
||||
ppid = os.Getppid()
|
||||
)
|
||||
if host == "" {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
if tls {
|
||||
tlsStr = "TRUE"
|
||||
}
|
||||
// tabwriter makes sure the spacing are consistant across different values
|
||||
// colorable handles the escape sequence for stdout using ascii color codes
|
||||
out := tabwriter.NewWriter(colorable.NewColorableStdout(), 0, 8, 4, ' ', 0)
|
||||
if !utils.GetArgument(flagChild) {
|
||||
fmt.Fprintf(out, logo, cBlack,
|
||||
cCyan, cBlack, fmt.Sprintf(" HOST: %s%s%s \tPORT: %s%v%s", cCyan, host, cBlack, cCyan, port, cBlack),
|
||||
cCyan, cBlack, fmt.Sprintf(" ROUTES: %s%v%s \tTLS: %s%v%s", cCyan, len(app.Routes()), cBlack, cCyan, tls, cBlack),
|
||||
cCyan, cBlack, fmt.Sprintf(" PREFORK: %s%v%s \tPPID: %s%v%s%s", cCyan, app.Settings.Prefork, cBlack, cCyan, os.Getppid(), cBlack, childs),
|
||||
fmt.Sprintf("%s%s%s", cCyan, Version, cBlack),
|
||||
fmt.Sprintf(" OS: %s%v%s \tARCH: %s%v%s\n", cCyan, runtime.GOOS, cBlack, cCyan, runtime.GOARCH, cReset))
|
||||
out := tabwriter.NewWriter(colorable.NewColorableStdout(), 0, 0, 2, ' ', 0)
|
||||
// simple Sprintf function that defaults back to black
|
||||
cyan := func(v interface{}) string {
|
||||
return fmt.Sprintf("%s%v%s", cCyan, v, cBlack)
|
||||
}
|
||||
// Build startup banner
|
||||
fmt.Fprintf(out, logo, cBlack, cBlack,
|
||||
cCyan, cBlack, fmt.Sprintf(" HOST %s\tOS %s", cyan(host), cyan(osName)),
|
||||
cCyan, cBlack, fmt.Sprintf(" PORT %s\tCORES %s", cyan(port), cyan(cpuCores)),
|
||||
cCyan, cBlack, fmt.Sprintf(" TLS %s\tMEM %s", cyan(tlsStr), cyan(memTotal)),
|
||||
cBlack, cyan(Version), fmt.Sprintf(" ROUTES %s\t\t\t PPID %s%s%s\n", cyan(routesLen), cyan(ppid), pids, cReset),
|
||||
)
|
||||
// Write to io.write
|
||||
_ = out.Flush()
|
||||
}
|
||||
|
|
4
go.mod
4
go.mod
|
@ -3,9 +3,9 @@ module github.com/gofiber/fiber
|
|||
go 1.11
|
||||
|
||||
require (
|
||||
github.com/gofiber/utils v0.0.6
|
||||
github.com/gofiber/utils v0.0.8
|
||||
github.com/gorilla/schema v1.1.0
|
||||
github.com/segrey/go-colorable v0.1.8
|
||||
github.com/mattn/go-colorable v0.1.7
|
||||
github.com/valyala/bytebufferpool v1.0.0
|
||||
github.com/valyala/fasthttp v1.14.0
|
||||
)
|
||||
|
|
8
go.sum
8
go.sum
|
@ -1,15 +1,15 @@
|
|||
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
||||
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/gofiber/utils v0.0.6 h1:lWuJfXQ06aFPVA2/y7lUw9ahS4TuXhbwXnxKl1D/fuY=
|
||||
github.com/gofiber/utils v0.0.6/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||
github.com/gofiber/utils v0.0.8 h1:k6OSI31Gg06eT6jLVVZMzdMEK460Lh1JwMNkIA+YKVc=
|
||||
github.com/gofiber/utils v0.0.8/go.mod h1:0dwJg4h6ME5RdxgukBF46XCYUBLI6nX5PvD6P4DDFBU=
|
||||
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
|
||||
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
|
||||
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
|
||||
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
|
||||
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/segrey/go-colorable v0.1.8 h1:UVuWIiT7W9xV25H69N1a0znYOZK0xVRgeoG1DrIJFNk=
|
||||
github.com/segrey/go-colorable v0.1.8/go.mod h1:9Gc/K8hJlvkFa0LKZlvsMh2I0nmgzunt/qoMtbsRi3M=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
|
||||
|
|
14
prefork.go
14
prefork.go
|
@ -9,6 +9,8 @@ import (
|
|||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
utils "github.com/gofiber/utils"
|
||||
reuseport "github.com/valyala/fasthttp/reuseport"
|
||||
|
@ -36,7 +38,10 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
|
|||
var ln net.Listener
|
||||
// get an SO_REUSEPORT listener or SO_REUSEADDR for windows
|
||||
if ln, err = reuseport.Listen("tcp4", addr); err != nil {
|
||||
return err
|
||||
if !app.Settings.DisableStartupMessage {
|
||||
time.Sleep(100 * time.Millisecond) // avoid colliding with startup message
|
||||
}
|
||||
return fmt.Errorf("prefork %v", err)
|
||||
}
|
||||
// wrap a tls config around the listener if provided
|
||||
if len(tlsconfig) > 0 {
|
||||
|
@ -55,7 +60,6 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
|
|||
var max = runtime.GOMAXPROCS(0)
|
||||
var childs = make(map[int]*exec.Cmd)
|
||||
var channel = make(chan child, max)
|
||||
//var stdout = tabwriter.NewWriter(colorable.NewColorableStdout(), 0, 8, 0, ' ', 0)
|
||||
|
||||
// kill child procs when master exits
|
||||
defer func() {
|
||||
|
@ -63,6 +67,7 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
|
|||
_ = proc.Process.Kill()
|
||||
}
|
||||
}()
|
||||
// collect child pids
|
||||
pids := []string{}
|
||||
// launch child procs
|
||||
for i := 0; i < max; i++ {
|
||||
|
@ -76,9 +81,6 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
|
|||
// store child process
|
||||
childs[cmd.Process.Pid] = cmd
|
||||
pids = append(pids, strconv.Itoa(cmd.Process.Pid))
|
||||
// notify stdout
|
||||
// fmt.Fprintf(stdout, "%sChild PID: %s#%v%s\n", cBlack, cGreen, cmd.Process.Pid, cReset)
|
||||
// _ = stdout.Flush()
|
||||
// notify master if child crashes
|
||||
go func() {
|
||||
channel <- child{cmd.Process.Pid, cmd.Wait()}
|
||||
|
@ -87,7 +89,7 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
|
|||
|
||||
// Print startup message
|
||||
if !app.Settings.DisableStartupMessage {
|
||||
app.startupMessage(addr, len(tlsconfig) > 0, pids)
|
||||
app.startupMessage(addr, len(tlsconfig) > 0, ","+strings.Join(pids, ","))
|
||||
}
|
||||
|
||||
// return error if child crashes
|
||||
|
|
Loading…
Reference in New Issue