github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/net/rpc/debug.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rpc 6 7 /* 8 Some HTML presented at http://machine:port/debug/rpc 9 Lists services, their methods, and some statistics, still rudimentary. 10 */ 11 12 import ( 13 "fmt" 14 "html/template" 15 "net/http" 16 "sort" 17 ) 18 19 const debugText = `<html> 20 <body> 21 <title>Services</title> 22 {{range .}} 23 <hr> 24 Service {{.Name}} 25 <hr> 26 <table> 27 <th align=center>Method</th><th align=center>Calls</th> 28 {{range .Method}} 29 <tr> 30 <td align=left font=fixed>{{.Name}}({{.Type.ArgType}}, {{.Type.ReplyType}}) error</td> 31 <td align=center>{{.Type.NumCalls}}</td> 32 </tr> 33 {{end}} 34 </table> 35 {{end}} 36 </body> 37 </html>` 38 39 var debug = template.Must(template.New("RPC debug").Parse(debugText)) 40 41 // If set, print log statements for internal and I/O errors. 42 var debugLog = false 43 44 type debugMethod struct { 45 Type *methodType 46 Name string 47 } 48 49 type methodArray []debugMethod 50 51 type debugService struct { 52 Service *service 53 Name string 54 Method methodArray 55 } 56 57 type serviceArray []debugService 58 59 func (s serviceArray) Len() int { return len(s) } 60 func (s serviceArray) Less(i, j int) bool { return s[i].Name < s[j].Name } 61 func (s serviceArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 62 63 func (m methodArray) Len() int { return len(m) } 64 func (m methodArray) Less(i, j int) bool { return m[i].Name < m[j].Name } 65 func (m methodArray) Swap(i, j int) { m[i], m[j] = m[j], m[i] } 66 67 type debugHTTP struct { 68 *Server 69 } 70 71 // Runs at /debug/rpc 72 func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) { 73 // Build a sorted version of the data. 74 var services = make(serviceArray, len(server.serviceMap)) 75 i := 0 76 server.mu.Lock() 77 for sname, service := range server.serviceMap { 78 services[i] = debugService{service, sname, make(methodArray, len(service.method))} 79 j := 0 80 for mname, method := range service.method { 81 services[i].Method[j] = debugMethod{method, mname} 82 j++ 83 } 84 sort.Sort(services[i].Method) 85 i++ 86 } 87 server.mu.Unlock() 88 sort.Sort(services) 89 err := debug.Execute(w, services) 90 if err != nil { 91 fmt.Fprintln(w, "rpc: error executing template:", err.Error()) 92 } 93 }