Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC: add connection infos and uptime to getInfo #2095

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
37 changes: 26 additions & 11 deletions app/appmessage/rpc_get_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ func NewGetInfoRequestMessage() *GetInfoRequestMessage {
// its respective RPC message
type GetInfoResponseMessage struct {
baseMessage
P2PID string
MempoolSize uint64
ServerVersion string
IsUtxoIndexed bool
IsSynced bool
P2PID string
MempoolSize uint64
ServerVersion string
IsUtxoIndexed bool
IsSynced bool
MaxRPCClients int64
NumberOfRPCConnections int64
MaxP2PClients int64
NumberOfP2PConnections int64
BanDurationInMilliseconds int64
UptimeInMilliseconds int64

Error *RPCError
}
Expand All @@ -35,12 +41,21 @@ func (msg *GetInfoResponseMessage) Command() MessageCommand {
}

// NewGetInfoResponseMessage returns a instance of the message
func NewGetInfoResponseMessage(p2pID string, mempoolSize uint64, serverVersion string, isUtxoIndexed bool, isSynced bool) *GetInfoResponseMessage {
func NewGetInfoResponseMessage(p2pID string, mempoolSize uint64, serverVersion string,
isUtxoIndexed bool, isSynced bool, maxRPCClients int64, numberOfRPCConnections int64,
maxP2PClients int64, numberOfP2PConnections int64, banDurationInMilliseconds int64,
uptimeInMilliseconds int64) *GetInfoResponseMessage {
return &GetInfoResponseMessage{
P2PID: p2pID,
MempoolSize: mempoolSize,
ServerVersion: serverVersion,
IsUtxoIndexed: isUtxoIndexed,
IsSynced: isSynced,
P2PID: p2pID,
MempoolSize: mempoolSize,
ServerVersion: serverVersion,
IsUtxoIndexed: isUtxoIndexed,
IsSynced: isSynced,
MaxRPCClients: maxRPCClients,
NumberOfRPCConnections: numberOfRPCConnections,
MaxP2PClients: maxP2PClients,
NumberOfP2PConnections: numberOfP2PConnections,
BanDurationInMilliseconds: banDurationInMilliseconds,
UptimeInMilliseconds: uptimeInMilliseconds,
}
}
6 changes: 6 additions & 0 deletions app/rpc/rpchandlers/get_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func HandleGetInfo(context *rpccontext.Context, _ *router.Router, _ appmessage.M
version.Version(),
context.Config.UTXOIndex,
context.ProtocolManager.Context().HasPeers() && isNearlySynced,
int64(context.Config.RPCMaxClients),
int64(context.NetAdapter.RPCConnectionCount()),
int64(context.Config.MaxInboundPeers),
int64(context.NetAdapter.P2PConnectionCount()),
int64(context.Config.BanDuration.Milliseconds()),
int64(context.NetAdapter.UptimeInMilliseconds()),
)

return response, nil
Expand Down
39 changes: 37 additions & 2 deletions infrastructure/network/netadapter/netadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package netadapter
import (
"sync"
"sync/atomic"
"time"

"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/infrastructure/config"
Expand Down Expand Up @@ -31,8 +32,13 @@ type NetAdapter struct {
rpcRouterInitializer RouterInitializer
stop uint32

p2pConnections map[*NetConnection]struct{}
startTime time.Time

p2pConnections map[*NetConnection]struct{}
rpcConnections map[*NetConnection]struct{}

p2pConnectionsLock sync.RWMutex
rpcConnectionsLock sync.RWMutex
}

// NewNetAdapter creates and starts a new NetAdapter on the
Expand All @@ -57,6 +63,7 @@ func NewNetAdapter(cfg *config.Config) (*NetAdapter, error) {
rpcServer: rpcServer,

p2pConnections: make(map[*NetConnection]struct{}),
rpcConnections: make(map[*NetConnection]struct{}),
}

adapter.p2pServer.SetOnConnectedHandler(adapter.onP2PConnectedHandler)
Expand All @@ -78,11 +85,14 @@ func (na *NetAdapter) Start() error {
if err != nil {
return err
}

err = na.rpcServer.Start()
if err != nil {
return err
}

na.startTime = time.Now()

return nil
}

Expand Down Expand Up @@ -147,9 +157,29 @@ func (na *NetAdapter) onP2PConnectedHandler(connection server.Connection) error
return nil
}

// RPCConnectionCount returns the count of the connected rpc connections
func (na *NetAdapter) RPCConnectionCount() int {
na.rpcConnectionsLock.RLock()
defer na.rpcConnectionsLock.RUnlock()

return len(na.rpcConnections)
}

func (na *NetAdapter) onRPCConnectedHandler(connection server.Connection) error {

na.rpcConnectionsLock.Lock()
defer na.rpcConnectionsLock.Unlock()

netConnection := newNetConnection(connection, na.rpcRouterInitializer, "on RPC connected")
netConnection.setOnDisconnectedHandler(func() {})
netConnection.setOnDisconnectedHandler(func() {

na.rpcConnectionsLock.Lock()
defer na.rpcConnectionsLock.Unlock()

delete(na.rpcConnections, netConnection)
})
na.rpcConnections[netConnection] = struct{}{}

netConnection.start()

return nil
Expand Down Expand Up @@ -190,3 +220,8 @@ func (na *NetAdapter) P2PBroadcast(netConnections []*NetConnection, message appm
}
return nil
}

// UptimeInMilliseconds returns this netAdapter's uptime in milliseconds
func (na *NetAdapter) UptimeInMilliseconds() int64 {
return time.Since(na.startTime).Milliseconds()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,12 @@ GetInfoRequestMessage returns info about the node.
| serverVersion | [string](#string) | | |
| isUtxoIndexed | [bool](#bool) | | |
| isSynced | [bool](#bool) | | |
| maxRPCClients | [int64](#int64) | | |
| numberOfRPCConnections | [int64](#int64) | | |
| maxP2PClients | [int64](#int64) | | |
| numberOfP2PConnections | [int64](#int64) | | |
| banDurationInMilliseconds | [int64](#int64) | | |
| uptimeInMilliseconds | [int64](#int64) | | |
| error | [RPCError](#protowire.RPCError) | | |


Expand Down
Loading