Tendermint源码阅读(二)

关注点:RPC相关配置

一、RPC的所有配置项

源码文件:
tendermint/config/config.go

//-----------------------------------------------------------------------------
// RPCConfig

// RPCConfig defines the configuration options for the Tendermint RPC server
type RPCConfig struct {
    RootDir string `mapstructure:"home"`

    // TCP or UNIX socket address for the RPC server to listen on
    ListenAddress string `mapstructure:"laddr"`

    // TCP or UNIX socket address for the gRPC server to listen on
    // NOTE: This server only supports /broadcast_tx_commit
    GRPCListenAddress string `mapstructure:"grpc_laddr"`

    // Maximum number of simultaneous connections.
    // Does not include RPC (HTTP&WebSocket) connections. See max_open_connections
    // If you want to accept more significant number than the default, make sure
    // you increase your OS limits.
    // 0 - unlimited.
    GRPCMaxOpenConnections int `mapstructure:"grpc_max_open_connections"`

    // Activate unsafe RPC commands like /dial_persistent_peers and /unsafe_flush_mempool
    Unsafe bool `mapstructure:"unsafe"`

    // Maximum number of simultaneous connections (including WebSocket).
    // Does not include gRPC connections. See grpc_max_open_connections
    // If you want to accept more significant number than the default, make sure
    // you increase your OS limits.
    // 0 - unlimited.
    // Should be < {ulimit -Sn} - {MaxNumInboundPeers} - {MaxNumOutboundPeers} - {N of wal, db and other open files}
    // 1024 - 40 - 10 - 50 = 924 = ~900
    MaxOpenConnections int `mapstructure:"max_open_connections"`
}

第一次阅读这段配置代码,非常迷惑。慢慢看代码,基本上已经弄明白。究其原因,我认为源码中的配置项的排列顺序有一点问题,我们来试着优化一下,注释就不再拷贝了。

type RPCConfig struct {
    RootDir string
    Unsafe bool 

    ListenAddress string 
    GRPCListenAddress string 

    MaxOpenConnections int 
    GRPCMaxOpenConnections int 
}

这样排列的话,就很容易理解了。Unsafe是针对ListenAddress和GRPCListenAddress都起作用的配置项。ListenAddress和GRPCListenAddress的关系一看就是完全对等的。既然这两种连接是对等的,那么MaxOpenConnections和GRPCMaxOpenConnections也应该是独立计数的。

从源码看出,ListenAddress在社区中称为RPC,支持两种协议,Http和Websocket,这两种协议可以共用一个端口,源于websockt与 HTTP 协议有着良好的兼容性,tendermint中使用一个特殊的路径来做区分,下面是部分源码

mux := http.NewServeMux()
wm := rpcserver.NewWebsocketManager(rpccore.Routes, coreCodec, rpcserver.EventSubscriber(n.eventBus))
        wm.SetLogger(rpcLogger.With("protocol", "websocket"))
        mux.HandleFunc("/websocket", wm.WebsocketHandler)

你可能感兴趣的:(Tendermint源码阅读(二))