RpcController作用浅析
RpcController作用浅析
前面提到了RpcConsumer的实现思路,但是并没说明RpcController有什么作用,不妨看看google::protobuf::RpcController:
class PROTOBUF_EXPORT RpcController {public:inline RpcController() {}virtual ~RpcController();// Client-side methods ---------------------------------------------// These calls may be made from the client side only. Their results// are undefined on the server side (may crash).// Resets the RpcController to its initial state so that it may be reused in// a new call. Must not be called while an RPC is in progress.virtual void Reset() = 0;// After a call has finished, returns true if the call failed. The possible// reasons for failure depend on the RPC implementation. Failed() must not// be called before a call has finished. If Failed() returns true, the// contents of the response message are undefined.virtual bool Failed() const = 0;// If Failed() is true, returns a human-readable description of the error.virtual std::string ErrorText() const = 0;// Advises the RPC system that the caller desires that the RPC call be// canceled. The RPC system may cancel it immediately, may wait awhile and// then cancel it, or may not even cancel the call at all. If the call is// canceled, the "done" callback will still be called and the RpcController// will indicate that the call failed at that time.virtual void StartCancel() = 0;// Server-side methods ---------------------------------------------// These calls may be made from the server side only. Their results// are undefined on the client side (may crash).// Causes Failed() to return true on the client side. "reason" will be// incorporated into the message returned by ErrorText(). If you find// you need to return machine-readable information about failures, you// should incorporate it into your response protocol buffer and should// NOT call SetFailed().virtual void SetFailed(const std::string& reason) = 0;// If true, indicates that the client canceled the RPC, so the server may// as well give up on replying to it. The server should still call the// final "done" callback.virtual bool IsCanceled() const = 0;// Asks that the given callback be called when the RPC is canceled. The// callback will always be called exactly once. If the RPC completes without// being canceled, the callback will be called after completion. If the RPC// has already been canceled when NotifyOnCancel() is called, the callback// will be called immediately.//// NotifyOnCancel() must be called no more than once per request.virtual void NotifyOnCancel(Closure* callback) = 0;private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
};
可以看到RpcController是一个抽象类,里面有一些纯虚函数,
Reset()表示rpc状态、
Failed()表示rpc请求过程中是否发生了错误,
ErrorText()表示如果rpc请求出错了,那么错误的原因是什么
SetFailed()表示rpc请求出错了,需要设置错误的原因
StartCancel()、IsCanceled()、NotifyOnCancel()表示是否取消rpc以及相应的回调。
显然这个类的作用可以很好的帮助rpcClient判断rpc请求过程中是否出错、错误原因、或是什么原因rpcServer取消了。
否则没有Rpccontroller的帮助,RpcClient调用rpc请求之后等待rpcServer的响应,然后反序列化响应。但如果中间错误了,那么RpcClient本地序列化response响应肯定会不成功(没必要序列化响应), 也不知道具体原因是什么,这就很不友好了。那么有了RpcController可以很轻松的解决这种问题。
老样子,实现RpcController很简单,写一个派生类继承自google::protobuf::RpcController,并重写需要提供给用户的方法。
这里提供一个简单版本MyRpcController类:
#pragma once
#include <google/protobuf/service.h>
#include <string>class MyRpcController : public google::protobuf::RpcController
{
public:MyRpcController ();void Reset();bool Failed() const;std::string ErrorText() const;void SetFailed(const std::string& reason);//目前未实现具体的功能void StartCancel();bool IsCanceled() const;void NotifyOnCancel(google::protobuf::Closure* callback);private:bool m_failed; //RPC方法执行过程中的状态std::string m_errText; //Rpc方法执行过程中的错误信息
};//.cc
#include "myrpccontroller.h"MyRpcController ::MyRpcController ()
{m_failed = false;m_errText = "";
}void MyRpcController ::Reset()
{m_failed = false;m_errText = "";
}bool MyRpcController ::Failed() const
{return m_failed;
}std::string MyRpcController ::ErrorText() const
{return m_errText;
}void MyRpcController ::SetFailed(const std::string& reason)
{m_failed = true;m_errText = reason;
}//目前未实现具体的功能
void MyRpcController ::StartCancel(){}bool MyRpcController ::IsCanceled() const{return false;}void MyRpcController ::NotifyOnCancel(google::protobuf::Closure* callback){}
使用MyRpcController
fixbug::UserServiceRpc_Stub stub(new MyRpcChannel());MyRpcController controllerLogin; //rpcController//rpc方法的请求参数fixbug::LoginRequest request;request.set_name("zhang san");request.set_pwd("123");//rpc方法的响应,同步的rpc调用过程fixbug::LoginResponse response;stub.Login(&controllerLogin, &request, &response, nullptr);//一次rpc调用完成,读调用的结果if(!controllerLogin.Failed() && 0 == response.result().errcode()){std::cout << "rpc login response success: " << response.success() << std::endl;}else{if(controllerLogin.Failed())std::cout<<controllerLogin.ErrorText()<<std::endl;else std::cout << "rpc login error msg : " << response.result().errmsg() << std::endl;}
至此,简单的RpcController实现。
相关文章:
RpcController作用浅析
RpcController作用浅析 前面提到了RpcConsumer的实现思路,但是并没说明RpcController有什么作用,不妨看看google::protobuf::RpcController: class PROTOBUF_EXPORT RpcController {public:inline RpcController() {}virtual ~RpcControlle…...
Linux(三):Linux服务器下日常实操命令 (常年更新)
基础命令 cd命令:切换目录 cd :切换当前目录百至其它目录,比如进入/etc目录,则执行 cd /etccd / :在Linux 系统中斜杠“/”表示的是根目录。cd / ,即进入根目录.cd ~:进入用户在该系统的home目录&#…...
强大的截图软件--Snipaste
这里写目录标题 前言Snipaste贴图并置顶标注功能 下载 前言 在工作中,我们经常需要保存当前屏幕的图片,虽然系统总是会自带一些截图工具,但似乎用起来总是不那个顺手,例如我们需要对图片进行一些标注,或者将图片贴在屏…...
LeetCode·每日一题·722. 删除注释·模拟
题目 示例 思路 题意 -> 给定一段代码,将代码中的注释删除并返回。 由于注释只有两种类型: 字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。字符串/* 表示一个块注释,它表示直到下一个(非重叠&#x…...
npm更新和管理已发布的包
目录 1、更改包的可见性 1.1 将公共包设为私有 编辑 使用网站 使用命令行 1.2 将私有包公开 使用网站 使用命令行 2、将协作者添加到用户帐户拥有的私有包 2.1 授予对Web上私有用户包的访问权限 2.2 从命令行界面授予私有包访问权限 2.3 授予对私有组织包的访问权限…...
如何高效使用Gherkin
背景 时间回到2022年,我参与了一个使用了Flutter技术构建的Web前端项目。在这个项目上,我们小组的目标是实施Flutter前端自动化测试。 彼时,Flutter 2.x刚在Web端发力不久,Flutter Web上的应用和生态才刚刚开始,而在…...
[CKA]考试之调度 pod 到指定节点
由于最新的CKA考试改版,不允许存储书签,本博客致力怎么一步步从官网把答案找到,如何修改把题做对,下面开始我们的 CKA之旅 题目为: Task 创建一个Pod,名字为nginx-kusc00401,镜像地址是nginx…...
git 常用命令有哪些
Git 是我们开发工作中使用频率极高的工具,下面总结下他的基本指令有哪些,顺便温习一下。 前言 一般项目中长存2个分支: 主分支(master) 和开发分支(develop) 项目存在三种短期分支 ࿱…...
算法leetcode|66. 加一(rust重拳出击)
文章目录 66. 加一:样例 1:样例 2:样例 3:提示: 分析:题解:rust:go:c:python:java: 66. 加一: 给定一个由 整数 组成的 非…...
MySQL备份Shell脚本
将此脚本添加到crontab计划中,自动留存最新的两份备份 #!/bin/bash # 数据库配置 DB_HOST"localhost" DB_USER"root" DB_PASS"Sxbdc123!#" DB_NAME"ww"# 备份目录 BACKUP_DIR"/opt/mysqlbak"# 备份文件名称 BA…...
Python批量查字典和爬取双语例句
最近,有网友反映,我的批量查字典工具换到其它的网站就不好用了。对此,我想说的是,互联网包罗万象,网站的各种设置也有所不同,并不是所有的在线字典都可以用Python爬取的。事实上,很多网站为了防…...
uni-app、H5实现瀑布流效果封装,列可以自定义
文章目录 前言一、效果二、使用代码三、核心代码总结前言 最近做项目需要实现uni-app、H5实现瀑布流效果封装,网上搜索有很多的例子,但是代码都是不够完整的,下面来封装一个uni-app、H5都能用的代码。在小程序中,一个个item渲染可能出现问题,也通过加锁来解决问题。 一、…...
vue echart3个饼图
概览:根据UI设计需要做3个饼图且之间有关联,并且处理后端返回的数据。 参考链接: echart 官网的一个案例,3个饼图 实现思路: 根据案例,把数据处理成对应的。 参考代码: 1.处理后端数据&am…...
LEARNING TO EXPLORE USING ACTIVE NEURAL SLAM 论文阅读
论文信息 题目:LEARNING TO EXPLORE USING ACTIVE NEURAL SLAM 作者:Devendra Singh Chaplot, Dhiraj Gandhi 项目地址:https://devendrachaplot.github.io/projects/Neural-SLAM 代码地址:https://github.com/devendrachaplot/N…...
item_search-ks-根据关键词取商品列表
一、接口参数说明: item_search-根据关键词取商品列表,点击更多API调试,请移步注册API账号点击获取测试key和secret 公共参数 请求地址: https://api-gw.onebound.cn/ks/item_search 名称类型必须描述keyString是调用key(http:…...
windows运行WPscan报错:无法打开库libcurl.dll
windows运行WPscan报错:无法打开库libcurl.dll 1.问题背景2.解决方案1.问题背景 在Windows上启动WPScan时: wpscan --url xxx.ru提示如下错误: Could not open library libcurl.dll: �� ������ ��������� ������. . Could not open library libcu...
web前端框架Javascript之JavaScript 异步编程史
早期的 Web 应用中,与后台进行交互时,需要进行 form 表单的提交,然后在页面刷新后给用户反馈结果。在页面刷新过程中,后台会重新返回一段 HTML 代码,这段 HTML 中的大部分内容与之前页面基本相同,这势必造成…...
Java多线程(1)---多线程认识、四种创建方式以及线程状态
目录 前言 一.Java的多线程 1.1多线程的认识 1.2Java多线程的创建方式 1.3Java多线程的生命周期 1.4Java多线程的执行机制 二.创建多线程的四种方式 2.1继承Thread类 ⭐创建线程 ⭐Thread的构造方法和常见属性 2.2.实现Runnable接口 ⭐创建线程 ⭐使用lambda表达…...
搭建Django+pyhon+vue自动化测试平台
Django安装 使用管理员身份运行pycharm使用local 1 pip install django -i https://pypi.tuna.tsinghua.edu.cn/simple 检查django是否安装成功 1 python -m django --version 创建项目 1 1 django-admin startproject test cd 切换至创建的项目中启动django项目…...
CASAIM自动化平面度检测设备3D扫描零部件形位公差尺寸测量
平面度是表面形状的度量,指示沿该表面的所有点是否在同一平面中,当两个表面需要连接在一起形成紧密连接时,平面度检测至关重要。 CASAIM自动化平面度检测设备通过搭载领先的激光三维测头和智能检测软件自动获取零部件高质量测量数据…...
突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合
强化学习(Reinforcement Learning, RL)是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程,然后使用强化学习的Actor-Critic机制(中文译作“知行互动”机制),逐步迭代求解…...
Day131 | 灵神 | 回溯算法 | 子集型 子集
Day131 | 灵神 | 回溯算法 | 子集型 子集 78.子集 78. 子集 - 力扣(LeetCode) 思路: 笔者写过很多次这道题了,不想写题解了,大家看灵神讲解吧 回溯算法套路①子集型回溯【基础算法精讲 14】_哔哩哔哩_bilibili 完…...
macOS多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用
文章目录 问题现象问题原因解决办法 问题现象 macOS启动台(Launchpad)多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用。 问题原因 很明显,都是Google家的办公全家桶。这些应用并不是通过独立安装的…...
【单片机期末】单片机系统设计
主要内容:系统状态机,系统时基,系统需求分析,系统构建,系统状态流图 一、题目要求 二、绘制系统状态流图 题目:根据上述描述绘制系统状态流图,注明状态转移条件及方向。 三、利用定时器产生时…...
LLM基础1_语言模型如何处理文本
基于GitHub项目:https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken:OpenAI开发的专业"分词器" torch:Facebook开发的强力计算引擎,相当于超级计算器 理解词嵌入:给词语画"…...
OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别
OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...
聊一聊接口测试的意义有哪些?
目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开,首…...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
中医有效性探讨
文章目录 西医是如何发展到以生物化学为药理基础的现代医学?传统医学奠基期(远古 - 17 世纪)近代医学转型期(17 世纪 - 19 世纪末)现代医学成熟期(20世纪至今) 中医的源远流长和一脉相承远古至…...
Golang——9、反射和文件操作
反射和文件操作 1、反射1.1、reflect.TypeOf()获取任意值的类型对象1.2、reflect.ValueOf()1.3、结构体反射 2、文件操作2.1、os.Open()打开文件2.2、方式一:使用Read()读取文件2.3、方式二:bufio读取文件2.4、方式三:os.ReadFile读取2.5、写…...
