thrift mysql连接池_Golang 实现Thrift客户端连接池方式

1 前言

阅读文章之前,请先了解一下thrift相关知识。thrift官方并没有提供客户端连接池的实现方案,而我们在实际使用时,thrift客户端必须复用,来保证较为可观的吞吐量,并避免在高QPS调用情况下,不断的创建、释放客户端所带来的机器端口耗尽问题。

本文会详细讲解如何实现一个简单可靠的thrift客户端连接池,并通过对照实验来说明thrift客户端连接池所带来的好处。

由于篇幅的原因,本文只粘出关键代码,源代码请查看Thrift Client Pool Demo

1.1 运行环境

Golang版本: go1.14.3 darwin/amd64

Thrift Golang库版本: 0.13.0

Thrift IDL编辑器版本: 0.13.0

1.2 .thrift文件

namespace java com.czl.api.thrift.model

namespace cpp com.czl.api

namespace php com.czl.api

namespace py com.czl.api

namespace js com.czl.apixianz

namespace go com.czl.api

struct ApiRequest {

1: required i16 id;

}

struct ApiResponse{

1:required string name;

}

// service1

service ApiService1{

ApiResponse query(1:ApiRequest request)

}

// service2

service ApiService2{

ApiResponse query(1:ApiRequest request)

}

注:请通过安装Thrift IDL编译器,并生成客户端、服务端代码。

1.3 对照实验说明

通过脚本开启100个协程并发调用rpc服务10分钟,统计这段时间内,未使用thrift客户端连接池与使用客户端连接池服务的平均吞吐量、Thrift API调用平均延迟、机器端口消耗等数据进行性能对比。

实验一: 未使用thrift客户端连接池

实验二: 使用thrift客户端连接池

2 Thrift客户端连接池实现

2.1 连接池的功能

首先,我们要明确一下连接池的职责,这里我简单的总结一下,连接池主要功能是维护连接的创建、释放,通过缓存连接来复用连接,减少创建连接所带来的开销,提高系统的吞吐量,一般连接池还会有连接断开的重连机制、超时机制等。这里我们可以先定义出大部分连接池都会有的功能,只是定义,可以先不管每个功能的具体实现。每一个空闲Thrift客户端其实底层都维护着一条空闲TCP连接,空闲Thrift客户端与空闲连接在这里其实是同一个概念。

......

// Thrift客户端创建方法,留给业务去实现

type ThriftDial func(addr string, connTimeout time.Duration) (*IdleClient, error)

// 关闭Thrift客户端,留给业务实现

type ThriftClientClose func(c *IdleClient) error

// Thrift客户端连接池

type ThriftPool struct {

// Thrift客户端创建逻辑,业务自己实现

Dial ThriftDial

// Thrift客户端关闭逻辑,业务自己实现

Close ThriftClientClose

// 空闲客户端,用双端队列存储

idle list.List

// 同步锁,确保count、status、idle等公共数据并发操作安全

lock *sync.Mutex

// 记录当前已经创建的Thrift客户端,确保MaxConn配置

count int32

// Thrift客户端连接池状态,目前就open和stop两种

status uint32

// Thrift客户端连接池相关配置

config *ThriftPoolConfig

}

// 连接池配置

type ThriftPoolConfig struct {

// Thrfit Server端地址

Addr string

// 最大连接数

MaxConn int32

// 创建连接超时时间

ConnTimeout time.Duration

// 空闲客户端超时时间,超时主动释放连接,关闭客户端

IdleTimeout time.Duration

// 获取Thrift客户端超时时间

Timeout time.Duration

// 获取Thrift客户端失败重试间隔

interval time.Duration

}

// Thrift客户端

type IdleClient struct {

// Thrift传输层,封装了底层连接建立、维护、关闭、数据读写等细节

Transport thrift.TTransport

// 真正的Thrift客户端,业务创建传入

RawClient interface{}

}

// 封装了Thrift客户端

type idleConn struct {

// 空闲Thrift客户端

c *IdleClient

// 最近一次放入空闲队列的时间

t time.Time

}

// 获取Thrift空闲客户端

func (p *ThriftPool) Get() (*IdleClient, error) {

// 1. 从空闲池中获取空闲客户端,获取到更新数据,返回,否则执行第2步

// 2. 创建新到Thrift客户端,更新数据,返回Thrift客户端

......

}

// 归还Thrift客户端

func (p *ThriftPool) Put(client *IdleCLient) error {

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5 是一个 Python 的 GUI 库,Thrift 是一个跨语言的服务框架。在 PyQt5 中实现 Thrift RPC 连接主要需要以下步骤: 1. 安装 thrift 库 在终端中执行以下命令安装 thrift 库: ``` pip install thrift ``` 2. 编写 thrift 文件 定义 Thrift 服务的接口和数据类型,将其保存为 .thrift 文件。 例如,定义一个简单的服务接口: ``` service Calculator { i32 add(1:i32 num1, 2:i32 num2), i32 sub(1:i32 num1, 2:i32 num2), } ``` 3. 生成 Python 代码 使用 thrift 工具生成 Python 代码。在终端中执行以下命令: ``` thrift -r --gen py calculator.thrift ``` 4. 编写 PyQt5 界面 使用 PyQt5 设计界面,添加按钮等控件。 5. 编写 Thrift 客户端 在 PyQt5 代码中编写 Thrift 客户端,调用 Thrift 服务。 例如,实现 Thrift 客户端代码: ```python from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.protocol import TMultiplexedProtocol from calculator import Calculator class ThriftClient(object): def __init__(self, host, port): self.transport = TSocket.TSocket(host, port) self.transport = TTransport.TBufferedTransport(self.transport) protocol = TBinaryProtocol.TBinaryProtocol(self.transport) self.client = Calculator.Client(TMultiplexedProtocol(protocol, "Calculator")) def open(self): self.transport.open() def close(self): self.transport.close() def add(self, num1, num2): return self.client.add(num1, num2) def sub(self, num1, num2): return self.client.sub(num1, num2) ``` 6. 连接 Thrift 服务 在 PyQt5 代码中连接 Thrift 服务,调用 Thrift 客户端。 例如,连接 Thrift 服务: ```python client = ThriftClient("localhost", 9090) client.open() result = client.add(1, 2) client.close() ``` 这样就可以在 PyQt5 界面中实现 Thrift RPC 连接了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值