当前位置:网站首页>ETCD数据库源码分析——集群间网络层客户端peerRt
ETCD数据库源码分析——集群间网络层客户端peerRt
2022-07-05 13:36:00 【肥叔菌】
peerRt(RoundTripper类型)主要负责实现etcdserver的网络请求等功能,用于向集群其他节点发送version或lease服务请求。我们知道其是在ETCD数据库源码分析——etcdserver bootstrap初始化中的第二步进行初始化的。如何使用它呢?
如server/etcdserver/cluster_util.go的promoteMemberHTTP所示,使用peerRt创建http客户端,然后构造请求Url和指定请求方法为POST,创建http Request结构体,调用http客户端的do函数向服务端进行请求,并获取其响应。
func promoteMemberHTTP(ctx context.Context, url string, id uint64, peerRt http.RoundTripper) ([]*membership.Member, error) {
cc := &http.Client{
Transport: peerRt} // 使用peerRt创建http客户端
// TODO: refactor member http handler code cannot import etcdhttp, so manually construct url
requestUrl := url + "/members/promote/" + fmt.Sprintf("%d", id)
req, err := http.NewRequest("POST", requestUrl, nil)
if err != nil {
return nil, err }
req = req.WithContext(ctx)
resp, err := cc.Do(req)
if err != nil {
return nil, err }
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err }
// 处理请求超时
if resp.StatusCode == http.StatusRequestTimeout {
return nil, errors.ErrTimeout }
if resp.StatusCode == http.StatusPreconditionFailed {
// both ErrMemberNotLearner and ErrLearnerNotReady have same http status code
if strings.Contains(string(b), errors.ErrLearnerNotReady.Error()) {
return nil, errors.ErrLearnerNotReady }
if strings.Contains(string(b), membership.ErrMemberNotLearner.Error()) {
return nil, membership.ErrMemberNotLearner }
return nil, fmt.Errorf("member promote: unknown error(%s)", string(b))
}
if resp.StatusCode == http.StatusNotFound {
return nil, membership.ErrIDNotFound }
// 其他类型的错误
if resp.StatusCode != http.StatusOK {
// all other types of errors
return nil, fmt.Errorf("member promote: unknown error(%s)", string(b))
}
var membs []*membership.Member
if err := json.Unmarshal(b, &membs); err != nil {
return nil, err } // 反序列化请求响应内容
return membs, nil
}
如下server/etcdserver/adapters.go中的GetMembersVersions也是使用peerRt获取MembersVersions。
func (s *serverVersionAdapter) GetMembersVersions() map[string]*version.Versions {
return getMembersVersions(s.lg, s.cluster, s.MemberId(), s.peerRt, s.Cfg.ReqTimeout())
}
边栏推荐
- SAE international strategic investment geometry partner
- js判断数组中是否存在某个元素(四种方法)
- Mmseg - Mutli view time series data inspection and visualization
- Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
- 【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
- Personal component - message prompt
- C# 对象存储
- Huawei push service content, read notes
- [深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
- asp.net 读取txt文件
猜你喜欢
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
内网穿透工具 netapp
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
真正的缓存之王,Google Guava 只是弟弟
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
这18个网站能让你的页面背景炫酷起来
What are the private addresses
Intranet penetration tool NetApp
TortoiseSVN使用情形、安装与使用
随机推荐
山东大学暑期实训一20220620
先写API文档还是先写代码?
asp.net 读取txt文件
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
Godson 2nd generation burn PMON and reload system
从外卖点单浅谈伪需求
"Baidu Cup" CTF competition in September, web:sql
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
[daily question] 1200 Minimum absolute difference
js判断数组中是否存在某个元素(四种方法)
Difference between avc1 and H264
法国学者:最优传输理论下对抗攻击可解释性探讨
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Summary and arrangement of JPA specifications
不知道这4种缓存模式,敢说懂缓存吗?
mysql获得时间
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]