src/pkg/net/rpc/debug.go - The Go Programming Language

Golang

Source file src/pkg/net/rpc/debug.go

     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		"net/http"
    15		"sort"
    16		"text/template"
    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	type debugMethod struct {
    42		Type *methodType
    43		Name string
    44	}
    45	
    46	type methodArray []debugMethod
    47	
    48	type debugService struct {
    49		Service *service
    50		Name    string
    51		Method  methodArray
    52	}
    53	
    54	type serviceArray []debugService
    55	
    56	func (s serviceArray) Len() int           { return len(s) }
    57	func (s serviceArray) Less(i, j int) bool { return s[i].Name < s[j].Name }
    58	func (s serviceArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    59	
    60	func (m methodArray) Len() int           { return len(m) }
    61	func (m methodArray) Less(i, j int) bool { return m[i].Name < m[j].Name }
    62	func (m methodArray) Swap(i, j int)      { m[i], m[j] = m[j], m[i] }
    63	
    64	type debugHTTP struct {
    65		*Server
    66	}
    67	
    68	// Runs at /debug/rpc
    69	func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    70		// Build a sorted version of the data.
    71		var services = make(serviceArray, len(server.serviceMap))
    72		i := 0
    73		server.mu.Lock()
    74		for sname, service := range server.serviceMap {
    75			services[i] = debugService{service, sname, make(methodArray, len(service.method))}
    76			j := 0
    77			for mname, method := range service.method {
    78				services[i].Method[j] = debugMethod{method, mname}
    79				j++
    80			}
    81			sort.Sort(services[i].Method)
    82			i++
    83		}
    84		server.mu.Unlock()
    85		sort.Sort(services)
    86		err := debug.Execute(w, services)
    87		if err != nil {
    88			fmt.Fprintln(w, "rpc: error executing template:", err.Error())
    89		}
    90	}