【问题标题】:How to manage rpc.Client in golanggolang中如何管理rpc.Client
【发布时间】:2021-06-30 06:49:10
【问题描述】:

我有一个包商店如下。

package store

type dbClient struct {
   client rpc.Client
}

func init() {
    // init dbClient
}
type Args struct{}
type Reply struct{
   Stories []interface{}
}

func GetStories() ([]interface{}, error) {
   args := Args{}
   var reply Reply
   err := dbClient.client.Call("Database.GetStories", &Args, &reply)
   return reply.Stories, err
}

我面临两个问题:

  1. 同时从多个 goroutine 调用 store.GetStories,但 rpc.Client 顺序处理请求,那么构建 dbClient 以使我能够处理 100 个并发请求到 store.GetStories 的最佳方法是什么?
  2. 每当我重新启动 rpc 服务器时,dbClient.client 都会断开连接,并且 dbClient.client.Call 会给出错误 rpc.ErrShutDown。那么,检查连接和重新连接的最优化方法是什么?我有一个投票 goroutine,正在寻找更多想法

【问题讨论】:

  • 使用一个工作池,在你的情况下有 100 个工作人员,每个工作人员使用独立的 rpc 客户端。
  • 回复 #2 使用 exponential back-off and retry 算法。

标签: go rpc


【解决方案1】:

最简单的解决方案是使用根据您的用例制定的方法:https://golang.org/pkg/net/rpc/#Client.Go

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2015-07-29
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多