Skip to content

Commit

Permalink
feat: add grpc connection pool dump
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh committed Jun 2, 2024
1 parent c0ac08e commit fdba214
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/remote/trans/nphttp2/conn_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"net"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -227,6 +228,32 @@ func (p *connPool) Close() error {
return nil
}

// Dump dumps the connection pool with the details of the underlying transport.
func (p *connPool) Dump() interface{} {
defer func() {
if panicErr := recover(); panicErr != nil {
klog.Errorf("KITEX: dump gRPC client connection pool panic, err=%v, stack=%s", panicErr, string(debug.Stack()))
}
}()
res := make(map[string]interface{})
p.conns.Range(func(k, v interface{}) bool {
addr := k.(string)
ts := v.(*transports)
var transportsDump = make([]interface{}, 0, len(ts.cliTransports))
for _, t := range ts.cliTransports {
if t == nil {
continue
}
if dumper, ok := t.(interface{ Dump() interface{} }); ok {
transportsDump = append(transportsDump, dumper.Dump())
}
}
res[addr] = transportsDump
return true
})
return res
}

// newTLSConn constructs a client-side TLS connection and performs handshake.
func newTLSConn(conn net.Conn, tlsCfg *tls.Config) (net.Conn, error) {
tlsConn := tls.Client(conn, tlsCfg)
Expand Down
35 changes: 35 additions & 0 deletions pkg/remote/trans/nphttp2/grpc/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,3 +1219,38 @@ func (t *http2Client) IsActive() bool {
defer t.mu.Unlock()
return t.state == reachable
}

type ClientTransportDump struct {
State transportState `json:"transport_state"`
LocalAddress string `json:"local_address"`
ActiveStreams []StreamDump `json:"active_streams"`
KeepAliveEnabled bool `json:"keepalive_enabled"`
LastRead int64 `json:"last_read"`
}

type StreamDump struct {
ID uint32 `json:"id"`
Method string `json:"method"`
State streamState `json:"stream_state"`
}

func (t *http2Client) Dump() interface{} {
t.mu.Lock()
defer t.mu.Unlock()

var activeStreams = make([]StreamDump, 0, len(t.activeStreams))
for _, v := range t.activeStreams {
activeStreams = append(activeStreams, StreamDump{
ID: v.id,
Method: v.method,
State: v.state,
})
}
return ClientTransportDump{
State: t.state,
LocalAddress: t.LocalAddr().String(),
ActiveStreams: activeStreams,
KeepAliveEnabled: t.keepaliveEnabled,
LastRead: atomic.LoadInt64(&t.lastRead),
}
}

0 comments on commit fdba214

Please sign in to comment.