Golang JSON-RPC 服务器在关闭时的控制

huangapple go评论102阅读模式
英文:

Golang JSON-RPC Server Control at Shutdown

问题

我正在为一个使用Golang内置的JSON-RPC服务器的程序编写一个关机处理程序,遇到了一些困难。你能帮我解决吗?

我停止接受传入的连接,停止接受现有连接的传入请求(发送一个错误,确保是真的),但是有一个我无法控制的时刻,或者至少我不知道如何控制。

问题是:

如何确保现有连接的现有请求的结果被Golang内置的JSON-RPC服务器的客户端接收到?

我想控制调用RPC方法的结构,但它不在我的代码中。例如,我有一个名为'User.Get'的RPC动作。我有一个名为'Get'的函数,我可以控制它。如何控制启动这个'Get'函数的代码?我需要修改Google的RPC服务器吗?还是有更有效的方法?

也许RPC服务器中有一个变量保存正在处理的活动请求的数量?

我在'https://golang.org/src/net/rpc/server.go'中看到了methodType中的NumCalls()numCalls。问题是这个字段不是公开的...而且.../rpc/server.go使用的sync.Map无法编译。我的Golang版本太旧了吗?:)

目前看来,最简单的方法是通过所有RPC动作函数来操作一个全局计数器。:-) 如果你知道更好的解决方案,请告诉我。谢谢。:-)

英文:

I am writing a Shutdown Handler for a Program which uses Golang's built-in JSON-RPC Server and I have faced a difficulty. Could you help me with it?

I stop accepting incomming Connections, stop accepting incoming Requests for existing Connections (send an Error, to be true) but I have a Moment which I can not control or, at least, I do not know how to control.

The Question is:

How do I guarantee that Results of exisiting Requests of existing Connections are given to Clients of Golang's built-in JSON-RPC Server?

I want to control that Structure which calls RPC Methods, but it is not in my Code. For Example, I have an RPC Action 'User.Get'. I have a function named 'Get' which I control. How do I control the Code which starts this 'Get' function? Do I have to modify Google's RPC Server or is there any more effecient way?

Maybe, there is some Variable in RPC Server which holds the Number of active Requests being processed?

I see a NumCalls() and numCalls in the methodType in https://golang.org/src/net/rpc/server.go. The Problem is that this Field is not public... Moreover, the .../rpc/server.go uses sync.Map which does not compile. My Version of Golang is too old? Golang JSON-RPC 服务器在关闭时的控制

Seems that for now the easiest Way is to manipulate a global Counter from all the RPC Action Functions. Golang JSON-RPC 服务器在关闭时的控制 If you know a better Solution, please, tell me. Thanks. Golang JSON-RPC 服务器在关闭时的控制

答案1

得分: 0

net/rpc的godoc示例中,它只展示了如何使用全局默认值。相反,你可以创建自己的rpc和http服务器实例。

import "net/rpc"
import "net/http"

rpcServer := rpc.NewServer()
// rpc相关代码
httpServer := &http.Server{
    Handler: rpcServer,
}
// 优雅关闭相关代码

对于优雅关闭,有几个包可供选择。我知道这个包:

英文:

In net/rpc godoc example it just shows how to use global defaults. Instead create your own rpc and http server instances.

import "net/rpc"
import "net/http"

rpcServer := rpc.NewServer()
// rpc stuff
httpServer := &http.Server{
    Handler: rpcServer,
}
// graceful shutdown stuff

For graceful shutdown, there's a few packages. I know of this one.

huangapple
  • 本文由 发表于 2017年7月8日 04:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/44979398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定