douju2053
douju2053
2017-07-07 20:49

关机时的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 douchungu0859 4年前

    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.

    点赞 评论 复制链接分享

相关推荐