【发布时间】: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
}
我面临两个问题:
- 同时从多个 goroutine 调用 store.GetStories,但 rpc.Client 顺序处理请求,那么构建 dbClient 以使我能够处理 100 个并发请求到 store.GetStories 的最佳方法是什么?
- 每当我重新启动 rpc 服务器时,dbClient.client 都会断开连接,并且 dbClient.client.Call 会给出错误 rpc.ErrShutDown。那么,检查连接和重新连接的最优化方法是什么?我有一个投票 goroutine,正在寻找更多想法
【问题讨论】:
-
使用一个工作池,在你的情况下有 100 个工作人员,每个工作人员使用独立的 rpc 客户端。
-
回复
#2使用 exponential back-off and retry 算法。