英文:
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?
Seems that for now the easiest Way is to manipulate a global Counter from all the RPC Action Functions. If you know a better Solution, please, tell me. Thanks.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论