Skip to content

Commit

Permalink
update utilroutes
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed Apr 6, 2021
1 parent 4926264 commit d8975fb
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 120 deletions.
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/lovego/goa

go 1.14

require (
bou.ke/monkey v1.0.2 // indirect
github.com/fatih/color v1.10.0
github.com/julienschmidt/httprouter v1.3.0
github.com/lovego/date v0.0.0-20200911055540-3565a62cb479
github.com/lovego/errs v0.0.0-20200914124519-002655aaf593
github.com/lovego/fs v0.0.1
github.com/lovego/jsondoc v0.0.0-20200618111905-62f874c0453a
github.com/lovego/logger v0.0.0-20200804041111-ab21de5d5a74
github.com/lovego/maps v0.0.0-20190404131308-5c14106deb1f // indirect
github.com/lovego/regex_tree v0.0.0-20201024082014-ed2a66f4b888
github.com/lovego/strs v0.0.0-20200806061622-587de9a27e5c
github.com/lovego/struct_tag v0.0.0-20200821111615-1a11055a2d31
github.com/lovego/structs v0.0.0-20210207031546-7db08510750d
github.com/lovego/tracer v0.0.0-20200207073659-f9fecb73b19f
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lovego/date v0.0.0-20200911055540-3565a62cb479 h1:74DRkcosqy8d0qQdUgdXo7HlmqbCa0pV2eoCM7lQT7U=
github.com/lovego/date v0.0.0-20200911055540-3565a62cb479/go.mod h1:dHef61fZ3P8JwWJvgK600bob+q2Guy1DnexxaXSqgNo=
github.com/lovego/errs v0.0.0-20200914124519-002655aaf593 h1:r7226H6FsObjjedOSIVC8iF0ueJBObYsF8oukgAQ6m0=
github.com/lovego/errs v0.0.0-20200914124519-002655aaf593/go.mod h1:FfIOS6S5g1NZl4X4jGnY5DifiOaKOpv4N9wZXUKej8k=
github.com/lovego/fs v0.0.1 h1:YaOX/k1nE/Iqw7aUbWyvz9Zwu1xBTLCvZSjnhvt4W/8=
github.com/lovego/fs v0.0.1/go.mod h1:rn6mRqNaC3noNNdckmOH0h9uvcka9JNvk1KbgZyh47Q=
github.com/lovego/jsondoc v0.0.0-20200618111905-62f874c0453a h1:A+zOCDKe3+cFbraZChaFtxq1HHs6DA29D2FfbsbG0zY=
github.com/lovego/jsondoc v0.0.0-20200618111905-62f874c0453a/go.mod h1:mfwxhCity8tzznk1e6O8KHp5MN2eVgEIo7LFaTSNC5E=
github.com/lovego/logger v0.0.0-20200804041111-ab21de5d5a74 h1:rj+TqKUL0gV7EKww+ZN/PYYlMR62SZCX0qFrkVyWLSc=
github.com/lovego/logger v0.0.0-20200804041111-ab21de5d5a74/go.mod h1:uAHp76Nknc9tJbA2XKYQCUYqQR82udPAItcs/BtQDn4=
github.com/lovego/maps v0.0.0-20190404131308-5c14106deb1f h1:ddyIZqaD9In5hDtB5eDgUr04JjBp0cpN3XPzS22kREc=
github.com/lovego/maps v0.0.0-20190404131308-5c14106deb1f/go.mod h1:7hsF4cgdpLGo+c6/0bQAKi14FnRIqmFu2bfAHKdQuG8=
github.com/lovego/regex_tree v0.0.0-20201024082014-ed2a66f4b888 h1:K53qtMNgslDpOiVU/dM+0SgxOQhCNerHSYtiVM6xrjs=
github.com/lovego/regex_tree v0.0.0-20201024082014-ed2a66f4b888/go.mod h1:EyRae8Bzxiu365gWzB4xDtYF+72btiDP1LfXhTibiSM=
github.com/lovego/strs v0.0.0-20200806061622-587de9a27e5c h1:nNif2nzOnIQhoWRjkU3zfa/MZLBaA8mzIfIEpTkc1hI=
github.com/lovego/strs v0.0.0-20200806061622-587de9a27e5c/go.mod h1:YgXHmoSkZdN/hEzZ0LszKnvz8JHP9oSXbHvkrE/8ZiI=
github.com/lovego/struct_tag v0.0.0-20200821111615-1a11055a2d31 h1:ivs6MCONAgAxVwtFzw076W2Y71Ey3JvXoq84QlDyyDs=
github.com/lovego/struct_tag v0.0.0-20200821111615-1a11055a2d31/go.mod h1:2Y5edfo2L39OxqqJqk0w67T136Njegcfi6YeOCbEkng=
github.com/lovego/structs v0.0.0-20210207031546-7db08510750d h1:jSrxw9x99zDZ5OLp63KVqw4oFMg5+/NS9dZumaDPJF8=
github.com/lovego/structs v0.0.0-20210207031546-7db08510750d/go.mod h1:pQsJc6Te//8NDwEMvPHjB6o25Ghr8ahkxtxwHtpKkIM=
github.com/lovego/tracer v0.0.0-20200207073659-f9fecb73b19f h1:ojEisTS/YwLIB82lLXE62LqIDAreD37kFJdg9b+Olmc=
github.com/lovego/tracer v0.0.0-20200207073659-f9fecb73b19f/go.mod h1:aKGpzdC+2JKu/90fy95JH9Ndaske6mVUEpwMW6rsvMo=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/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=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7 changes: 2 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/fatih/color"
"github.com/lovego/goa/utilroutes"
)

type Server struct {
Expand Down Expand Up @@ -53,11 +54,7 @@ func (s Server) gracefulShutdown() {
}

func getListener() net.Listener {
port := os.Getenv(`GOPORT`)
if port == `` {
port = `3000`
}
addr := `:` + port
addr := utilroutes.ListenAddr()
ln, err := net.Listen(`tcp`, addr)
if err != nil {
log.Panic(err)
Expand Down
1 change: 1 addition & 0 deletions utilroutes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.html
101 changes: 101 additions & 0 deletions utilroutes/debug_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package utilroutes

import (
"bytes"
"runtime/pprof"
"sort"
"text/template"
)

func debugIndex() []byte {
profiles := pprof.Profiles()
sort.Slice(profiles, func(i, j int) bool {
var orderI, orderJ = 99, 99
for order, v := range []string{
"heap", "allocs", "goroutine", "threadcreate", "block", "mutex",
} {
switch v {
case profiles[i].Name():
orderI = order
case profiles[j].Name():
orderJ = order
}
}
return orderI < orderJ
})

var buf bytes.Buffer
if err := debugIndexTmpl.Execute(&buf, struct {
Instance string
ReqCount int
Profiles []*pprof.Profile
Descs map[string]string
}{
Instance: instanceName,
ReqCount: requests.Count(),
Profiles: profiles,
Descs: pprofDescs,
}); err != nil {
return []byte(err.Error())
}
return buf.Bytes()
}

var debugIndexTmpl = template.Must(template.New(``).Parse(`
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<base target="_blank">
<title>pprof</title>
<style>
table { border-collapse: collapse; }
th,td { padding: 5px 10px; border: 1px dashed gray; }
</style>
</head>
<body>
<h3>{{ .Instance }} profiles</h3>
<table>
<tr>
<th>Name</th>
<th>Count</th>
<th>Description</th>
</tr>
<tr>
<td><a href=/_debug/reqs>requests</a></td>
<td align=right>{{ .ReqCount }}</td>
<td>Requests in processing.</td>
</tr>
<tr>
<td><a href=/_debug/cpu>cpu</a></td>
<td></td>
<td> CPU profile. You can specify the duration in the "seconds" GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile. </td>
</tr>
{{range .Profiles}}
<tr>
<td><a href="/_debug/{{.Name}}?debug=1">{{.Name}}</a></td>
<td align=right>{{.Count}}</td>
<td>{{ index $.Descs .Name }}</td>
</tr>
{{end}}
<tr>
<td><a href=/_debug/trace>trace</a></td>
<td></td>
<td> A trace of execution of the current program. You can specify the duration in the "seconds" GET parameter. After you get the trace file, use the go tool trace command to investigate the trace. </td>
</tr>
</table>
</body>
</html>
`))

var pprofDescs = map[string]string{
"heap": "Heap memory profile. You can specify the gc GET parameter to run GC before taking the heap sample.",
"allocs": "Heap memory allocations, including all past.",
"goroutine": "Stack traces of all current goroutines.",
"threadcreate": "Stack traces that led to the creation of new OS threads.",
"block": "Stack traces that led to blocking on synchronization primitives.",
"mutex": "Stack traces of holders of contended mutexes.",
}
57 changes: 57 additions & 0 deletions utilroutes/instance_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package utilroutes

import (
"fmt"
"log"
"net"
"os"
"strings"
)

var instanceName = getInstanceName()

func getInstanceName() string {
hostname, err := os.Hostname()
if err != nil {
log.Panic(err)
}
return fmt.Sprintf(
"%s (%s) (Listen At %s)", hostname, strings.Join(ipv4Addrs(), ", "), ListenAddr(),
)
}

func ipv4Addrs() (result []string) {
ifcs, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, ifc := range ifcs {
if ifc.Flags&net.FlagLoopback == 0 {
result = append(result, ipv4AddrsOfInterface(ifc)...)
}
}
return result
}
func ipv4AddrsOfInterface(ifc net.Interface) (result []string) {
addrs, err := ifc.Addrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
if str := addr.String(); strings.IndexByte(str, '.') > 0 { // ipv4
if i := strings.IndexByte(str, '/'); i >= 0 {
str = str[:i]
}
result = append(result, str)
}
}
return result
}

func ListenAddr() string {
port := os.Getenv(`GOPORT`)
if port == `` {
port = `3000`
}
return `:` + port
}
80 changes: 0 additions & 80 deletions utilroutes/pprof.go

This file was deleted.

Loading

0 comments on commit d8975fb

Please sign in to comment.