搜狗workflow异步调度框架例程分析——00


关于搜狗的workflow不在赘述,可以查看知乎介绍 https://zhuanlan.zhihu.com/p/165638263
B站也有视频。
从本篇开始根据workflow提供的源码例程为切入点开始分析如何使用workflow搭建异步调度的C++后端代码

workflow例程:00

在这里插入图片描述

这个代码很简单,简单到官方都没有给出关于他的任何文档,连编号都是00。但是对于首次接触sougou_workflow框架来说也需要花费时间看一下。
那么我们来一起看一下这个代码:

一、创建http服务

WFHttpServer server([](WFHttpTask *task) {
    task->get_resp()->append_output_body("<html>Hello World!</html>");
    });

我们可以看到这里建立了一个WFHttpServer的对象

1、WFHttpServer类

//WFHttpServer.h
using WFHttpServer = WFServer<protocol::HttpRequest,
							  protocol::HttpResponse>;

//WFServer.h
template<class REQ, class RESP>
class WFServer : public WFServerBase
{
public:
	WFServer(const struct WFServerParams *params,
			 std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
		WFServerBase(params),
		process(std::move(proc))
	{
	}

	WFServer(std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
		WFServerBase(&SERVER_PARAMS_DEFAULT),
		process(std::move(proc))
	{
	}

protected:
	virtual CommSession *new_session(long long seq, CommConnection *conn);

protected:
	std::function<void (WFNetworkTask<REQ, RESP> *)> process;
};

而WFHttpServer其实是关于WFServer的一个模板。
根据类的构造函数可以看到,WFSever的参数初始化有两种方法:

  1. 带入param,proc两个参数
  2. 带入proc两个参数(默认模式 SERVER_PARAMS_DEFAULT)

随后通过WFServerBase初始化第一个参数,通过WFNetworkTask初始化第二个参数。
下面是第一个参数的struct

struct WFServerParams
{
	size_t max_connections;		//最大连接数
	int peer_response_timeout;	/* timeout of each read or write operation */
	int receive_timeout;	/* timeout of receiving the whole message */
	int keep_alive_timeout;		//保持连接的超时时间
	size_t request_size_limit;	//请求最大字节限制
	int ssl_accept_timeout;	/* if not ssl, this will be ignored */
};

//而SERVER_PARAM_DEFAULT如下,是WFSeverParams的默认值
static constexpr struct WFServerParams SERVER_PARAMS_DEFAULT =
{
	.max_connections		=	2000,
	.peer_response_timeout	=	10 * 1000,
	.receive_timeout		=	-1,
	.keep_alive_timeout		=	60 * 1000,
	.request_size_limit		=	(size_t)-1,
	.ssl_accept_timeout		=	10 * 1000,
};

从代码中可以看到例程使用的是默认参数模式。

2、WFHttpTask类

//WFTaskFactory.h
using WFHttpTask = WFNetworkTask<protocol::HttpRequest,
								 protocol::HttpResponse>;

//WFTask.h
template<class REQ, class RESP>
class WFNetworkTask : public CommRequest
{
	//这个类过长,只选择需要用到的分析
	RESP *get_resp() { return &this->resp; }
}

//HttpMessage.h
bool append_output_body(const std::string& buf)
	{
		return this->append_output_body(buf.c_str(), buf.size());
	}

在函数中调用了该类中的get_resp()->append_output_body()函数,这个函数的是为了添加要执行的HTML语句,例程中使用了一个HellowWorld简单代替。

经过上述操作,我们已经成功建立了一个完整的Http服务。
接下来我们需要在服务器端启动服务。

二、开启/关闭服务

if (server.start(8888) == 0) { // start server on port 8888
        getchar(); // press "Enter" to end.
        server.stop();
    }

sougou_workflow很吸引人的地方就是一句程序就可以启动服务,像上述代码一样,只需要server.start(post)即可开启;而server.stop()即可关闭服务。

三、测试

在这里插入图片描述
运行服务后使用curl命令访问本地8888端口,获取response,成功。

四、总结

使用sougou_workflow建立http服务,只需要三步

  1. 初始化服务
  2. 编写html文件
  3. server.start
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个旅者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值