douju2053 2017-07-07 20:49
浏览 36
已采纳

关机时的Golang JSON-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条回答 默认 最新

  • douchungu0859 2017-07-09 02:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型