一、Transport结构体
type Transport struct {
Logger *zap.Logger
DialTimeout time.Duration // maximum duration before timing out dial of the request,dial的超时时间
// DialRetryFrequency defines the frequency of streamReader dial retrial attempts;
// a distinct rate limiter is created per every peer (default value: 10 events/sec)
DialRetryFrequency rate.Limit //定义streamReader dial 重试尝试的频率;每一个peer都有不同频率的限制
TLSInfo transport.TLSInfo // TLS information used when creating connection tls信息,创建连接时使用
ID types.ID // local member ID 当前节点自己的ID
URLs types.URLs // local peer URLs 当前节点与集群中其他节点交互时使用的URL地址
ClusterID types.ID // raft cluster ID for request validation 当前节点所在的集群的ID
/*
Raft是一个接口,其实现的底层封装了前面介绍的etcd-raft模块,当rafthttp.Transport收到消息之后,会将其交给Raft实例进行交互
*/
Raft Raft // raft state machine, to which the Transport forwards received messages and reports status
Snapshotter *snap.Snapshotter //负责管理快照文件
ServerStats *stats.ServerStats // used to record general transportation statistics 用于统计一般的transportation统计
// used to record transportation statistics with followers when
// performing as leader in raft protocol
LeaderStats *stats.LeaderStats //raft协议中的leader节点统计followers节点的 transportation 状态
// ErrorC is used to report detected critical errors, e.g.,
// the member has been permanently removed from the cluster
// When an error is received from ErrorC, user should stop raft state
// machine and thus stop the Transport.
ErrorC chan error
streamRt http.RoundTripper // roundTripper used by streams Stream消息通道中使用http.RoundTripper实例,HTTP长连接
pipelineRt http.RoundTripper // roundTripper used by pipelines Pipeline消息通道中使用的http.RoundTripper实例,传输完成后会立即关闭连接,传输数据量较大、发送频率较低的消息,如MsgSnap消息
mu sync.RWMutex // protect the remote and peer map
//remote中只封装了pipeline实例,remote主要负责发送快照数据,帮助新加入的节点快速追上其他节点的数据
remotes map[types.ID]*remote // remotes map that helps newly joined member to catch up
/*
Peer接口是当前节点对集群中其他节点的抽象表示。对于当前节点来说,集群中其他节点在本地都会有一个Peer实例与之对应,
peers字段维护了节点ID到对应Peer实例之间的映射关系
*/
peers map[types.ID]Peer // peers map
//用于探测Pipeline消息通道是否可用。
prober probing.Prober
}
Transport实现了Transporter接口。它提供了向对等端发送raft消息的功能,并且接受其他节点的消息。
ID记录当前节点的ID
ClusterID记录当前节点的集群ID
Raft接口,用于和raft模块进行交互,etcd-raft前面已经介绍。
streamRt和pipelineRt 底层都是http.RoundTripper的默认实现http.Transport,完成HTTP事务。
remote remote中只封装了pipeline实例,remote主要负责发送快照数据,帮助新加入的节点快速追上其他节点的数据
peers 记录当前节点和集群中其他节点的对应关系(网络层),Peer接口是当前节点对集群中其他节点的抽象表示。对于当前节点来说ÿ