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)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,993评论 2 59
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,937评论 0 5
  • 自从有了微信后,很少在Q群里聊天,要不是有时工作需要,可能连QQ都不上了! 这段时间,新加了一个同城群,意想不到的...
    南城青年阅读 225评论 0 1