Skip to content

Commit

Permalink
Add memory info
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny committed Jun 27, 2020
1 parent badb457 commit ec51458
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
77 changes: 52 additions & 25 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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], ",")
}
logo := ` %s_______ __
%s____%s / ____(_) /_ ___ _____ %s
%s_____%s / /_ / / __ \/ _ \/ ___/ %s
%s__%s / __/ / / /_/ / __/ / %s
/_/ /_/_.___/\___/_/%s %s`
host := strings.Split(addr, ":")[0]
port := strings.Split(addr, ":")[1]
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
}
//
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 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
14 changes: 8 additions & 6 deletions prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os/exec"
"runtime"
"strconv"
"strings"
"time"

utils "github.com/gofiber/utils"
reuseport "github.com/valyala/fasthttp/reuseport"
Expand Down Expand Up @@ -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 {
Expand All @@ -55,14 +60,14 @@ 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() {
for _, proc := range childs {
_ = proc.Process.Kill()
}
}()
// collect child pids
pids := []string{}
// launch child procs
for i := 0; i < max; i++ {
Expand All @@ -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()}
Expand All @@ -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
Expand Down

0 comments on commit ec51458

Please sign in to comment.