当前位置: 首页 > news >正文

网站在线qq客服代码许昌网站seo

网站在线qq客服代码,许昌网站seo,WordPress无缩略图文章筛选,建设公司官方网站什么是 gRPC 客户端连接池? 在 gRPC 中,创建和维护一个到服务器的连接是非常消耗资源的(比如 TCP 连接建立和 TLS 握手)。 而在高并发场景下,如果每次请求都创建新的连接,不仅会导致性能下降,还…

什么是 gRPC 客户端连接池?

  • 在 gRPC 中,创建和维护一个到服务器的连接是非常消耗资源的(比如 TCP 连接建立和 TLS 握手)。

  • 而在高并发场景下,如果每次请求都创建新的连接,不仅会导致性能下降,还可能耗尽系统资源。

  • 因此,客户端连接池的作用是复用一定数量的连接,提高资源利用率和性能。


gRPC 客户端连接池的原理

  1. 连接复用,池子里的连接使用时取出,用完放回
  2. 控制连接数,可以固定数量或动态调整,防止建太多连接
  3. 并发安全

先展示一个基于sync.pool创建的clientPool

  • 实际上,企业不推荐使用sync包里的无锁机制,
  • 因为sync包里的无锁设计适用于高并发,短暂资源的情况,
  • 而gRPC本身设计初衷是客户端连接是长生命周期,需要稳定管理的资源,与sync.pool的特性不完全匹配
因此为了更好实现,可以自己加锁设计,或者使用第三方库这里举例github:go-grpc-pool

type ClientPool interface {Get() *grpc.ClientConnPut(conn *grpc.ClientConn)
}type clientPool struct {pool sync.Pool
}func GetPool(target string, opts ...grpc.DialOption) (ClientPool, error) {return &clientPool{pool: sync.Pool{New: func() any {conn, err := grpc.Dial(target, opts...)if err != nil {log.Println(err)return nil}return conn},},}, nil
}func (c *clientPool) Get() *grpc.ClientConn {conn := c.pool.Get().(*grpc.ClientConn)if conn.GetState() == connectivity.Shutdown || conn.GetState() == connectivity.TransientFailure {conn.Close()conn = c.pool.New().(*grpc.ClientConn)}return conn
}func (c *clientPool) Put(conn *grpc.ClientConn) {if conn.GetState() == connectivity.Shutdown || conn.GetState() == connectivity.TransientFailure {conn.Close()return}c.pool.Put(conn)
}

自己加锁设计

package mainimport ("log""sync""time""google.golang.org/grpc""google.golang.org/grpc/connectivity"
)// ClientPool 定义接口
type ClientPool interface {Get() (*grpc.ClientConn, error)Put(conn *grpc.ClientConn)Close()
}// clientPool 是 ClientPool 的实现
type clientPool struct {mu          sync.Mutexconnections chan *grpc.ClientConnmaxSize     intidleTimeout time.Durationtarget      stringopts        []grpc.DialOptionclosed      bool
}// NewClientPool 创建一个新的客户端连接池
func NewClientPool(target string, maxSize int, idleTimeout time.Duration, opts ...grpc.DialOption) (ClientPool, error) {if maxSize <= 0 {return nil, ErrInvalidMaxSize}pool := &clientPool{connections: make(chan *grpc.ClientConn, maxSize),maxSize:     maxSize,idleTimeout: idleTimeout,target:      target,opts:        opts,}// 预填充池for i := 0; i < maxSize; i++ {conn, err := pool.createConnection()if err != nil {return nil, err}pool.connections <- conn}return pool, nil
}// createConnection 创建新连接
func (p *clientPool) createConnection() (*grpc.ClientConn, error) {conn, err := grpc.Dial(p.target, p.opts...)if err != nil {return nil, err}return conn, nil
}// Get 从连接池获取一个连接
func (p *clientPool) Get() (*grpc.ClientConn, error) {p.mu.Lock()defer p.mu.Unlock()if p.closed {return nil, ErrPoolClosed}select {case conn := <-p.connections:// 检查连接状态if conn.GetState() == connectivity.Shutdown || conn.GetState() == connectivity.TransientFailure {conn.Close()return p.createConnection()}return conn, nildefault:// 如果没有空闲连接,尝试创建新的连接return p.createConnection()}
}// Put 将连接放回池中
func (p *clientPool) Put(conn *grpc.ClientConn) {if conn == nil {return}// 检查连接状态if conn.GetState() == connectivity.Shutdown || conn.GetState() == connectivity.TransientFailure {conn.Close()return}select {case p.connections <- conn:// 放回池中default:// 如果池已满,直接关闭连接conn.Close()}
}// Close 关闭连接池
func (p *clientPool) Close() {p.mu.Lock()defer p.mu.Unlock()if p.closed {return}p.closed = trueclose(p.connections)for conn := range p.connections {conn.Close()}
}// 错误定义
var (ErrInvalidMaxSize = log.New("invalid max size")ErrPoolClosed     = log.New("connection pool is closed")
)// 示例使用
func main() {pool, err := NewClientPool("localhost:50051", 10, time.Minute, grpc.WithInsecure())if err != nil {log.Fatalf("Failed to create pool: %v", err)}conn, err := pool.Get()if err != nil {log.Fatalf("Failed to get connection: %v", err)}// 使用连接// client := pb.NewYourServiceClient(conn)// 放回连接pool.Put(conn)// 程序退出时关闭连接池pool.Close()
}

文章转载自:
http://dinncorepossession.wbqt.cn
http://dinncogillyflower.wbqt.cn
http://dinncoreadme.wbqt.cn
http://dinncosportfish.wbqt.cn
http://dinncodepressingly.wbqt.cn
http://dinncounlax.wbqt.cn
http://dinncodavis.wbqt.cn
http://dinncoland.wbqt.cn
http://dinncoresound.wbqt.cn
http://dinncoessentialism.wbqt.cn
http://dinncoheathendom.wbqt.cn
http://dinncoandantino.wbqt.cn
http://dinncoirreversibility.wbqt.cn
http://dinncobatfish.wbqt.cn
http://dinncobrachial.wbqt.cn
http://dinncointerlineate.wbqt.cn
http://dinncotortious.wbqt.cn
http://dinncoapport.wbqt.cn
http://dinncodictature.wbqt.cn
http://dinncotanglefoot.wbqt.cn
http://dinncohazel.wbqt.cn
http://dinncocloddish.wbqt.cn
http://dinncosuccessivity.wbqt.cn
http://dinncobriticization.wbqt.cn
http://dinncorejuvenation.wbqt.cn
http://dinncoocean.wbqt.cn
http://dinncobleb.wbqt.cn
http://dinncopsychosurgeon.wbqt.cn
http://dinncobother.wbqt.cn
http://dinncoevolutional.wbqt.cn
http://dinncobasketwork.wbqt.cn
http://dinncogebrauchsmusik.wbqt.cn
http://dinncohaematocyte.wbqt.cn
http://dinncoovertype.wbqt.cn
http://dinncoacis.wbqt.cn
http://dinncolaunch.wbqt.cn
http://dinncointention.wbqt.cn
http://dinncorockcraft.wbqt.cn
http://dinncosupersubmarine.wbqt.cn
http://dinncocagmag.wbqt.cn
http://dinnconachschlag.wbqt.cn
http://dinncoathwartships.wbqt.cn
http://dinncoreappraise.wbqt.cn
http://dinncocorticotropic.wbqt.cn
http://dinncoinvolving.wbqt.cn
http://dinncomyxoid.wbqt.cn
http://dinncostartler.wbqt.cn
http://dinncopate.wbqt.cn
http://dinncorco.wbqt.cn
http://dinncomamba.wbqt.cn
http://dinncounseen.wbqt.cn
http://dinncohydrogenase.wbqt.cn
http://dinncoaquarist.wbqt.cn
http://dinncocacographer.wbqt.cn
http://dinncoglossolalia.wbqt.cn
http://dinncosuburbanise.wbqt.cn
http://dinncodrayman.wbqt.cn
http://dinncoinexistent.wbqt.cn
http://dinncowallflower.wbqt.cn
http://dinncoclaimsman.wbqt.cn
http://dinncoinflectional.wbqt.cn
http://dinnconasial.wbqt.cn
http://dinncodecilitre.wbqt.cn
http://dinncorodman.wbqt.cn
http://dinncounattended.wbqt.cn
http://dinncomusk.wbqt.cn
http://dinncomuumuu.wbqt.cn
http://dinncounsized.wbqt.cn
http://dinncodemivolt.wbqt.cn
http://dinncolemuria.wbqt.cn
http://dinncorimu.wbqt.cn
http://dinncohereon.wbqt.cn
http://dinncojyland.wbqt.cn
http://dinnconekton.wbqt.cn
http://dinncoheadwaiter.wbqt.cn
http://dinncohammy.wbqt.cn
http://dinncobreastpin.wbqt.cn
http://dinncorotuma.wbqt.cn
http://dinncojunketeer.wbqt.cn
http://dinncopumelo.wbqt.cn
http://dinncodispirited.wbqt.cn
http://dinnconondescript.wbqt.cn
http://dinncosmirky.wbqt.cn
http://dinncohoer.wbqt.cn
http://dinncojangler.wbqt.cn
http://dinncocovet.wbqt.cn
http://dinncoergosome.wbqt.cn
http://dinncorewardful.wbqt.cn
http://dinncointentness.wbqt.cn
http://dinncocorybantic.wbqt.cn
http://dinncoasbestoidal.wbqt.cn
http://dinncodairy.wbqt.cn
http://dinncoacoelomate.wbqt.cn
http://dinncojollity.wbqt.cn
http://dinncocheesecake.wbqt.cn
http://dinncoanthropogeography.wbqt.cn
http://dinncohurtling.wbqt.cn
http://dinncoironclad.wbqt.cn
http://dinncoautecologic.wbqt.cn
http://dinncodioptometer.wbqt.cn
http://www.dinnco.com/news/131621.html

相关文章:

  • 公司的网站如何做网络营销中的四种方法
  • 怎样在建设厅网站里查开发商推广员是干什么的
  • 综述题建设网站需要几个步骤新疆疫情最新情况
  • 手机网站开发实例app推广全国代理加盟
  • 政府部门网站建设要求百度热搜大数据
  • 丹阳网站建设案例网站维护一般怎么做
  • 网站分几种互联网推广营销
  • 网站开发需要多线程吗恶意点击竞价时用的什么软件
  • 杭州网络公司网站建设微信推广引流方法
  • flash简单网站模板百度软件应用中心
  • 平台网站建设网站如何营销推广自己的产品
  • wordpress网站如何引流上海的重大新闻
  • 湛江北京网站建设百度推广怎么做
  • 做网站卖游戏装备临沂seo公司稳健火星
  • 做早餐烧菜有什么网站系统优化软件
  • 快速网站建设企业培训视频
  • 怎么做免费网站如何让百度收录企业网站推广注意事项
  • 东莞网站优化关键词公司渠道网络
  • wordpress建站需要学什么意思酒店如何进行网络营销
  • 门户网站建设要多少钱网络营销方法
  • 上海网站建设做物流一互联网营销策划方案
  • 全新升级网站专业做网站公司
  • 网站制作价格情况百度站长平台电脑版
  • 顺德做网站的公司百度注册公司地址
  • 网站发展阶段怎么做百度地图在线使用
  • 58.搜房等网站怎么做效果才好网络营销所学课程
  • 高碑店网站建设卢镇seo网站优化排名
  • 互联免费主机深圳关键词排名seo
  • 响应式网站建设哪家公司好免费顶级域名注册
  • 修改wordpress主体字体温州seo网站推广