13 JSON-RPC: a tale of interfaces
Posted 天际凯迪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13 JSON-RPC: a tale of interfaces相关的知识,希望对你有一定的参考价值。
JSON-RPC: a tale of interfaces
27 April 2010
Here we present an example where Go‘s interfaces made it easy to refactor some existing code to make it more flexible and extensible. Originally, the standard library‘s RPC package used a custom wire format called gob. For a particular application, we wanted to use JSON as an alternate wire format.
We first defined a pair of interfaces to describe the functionality of the existing wire format, one for the client, and one for the server (depicted below).
type ServerCodec interface { ReadRequestHeader(*Request) error ReadRequestBody(interface{}) error WriteResponse(*Response, interface{}) error Close() error }
On the server side, we then changed two internal function signatures to accept the ServerCodec
interface instead of our existing gob.Encoder
. Here‘s one of them:
func sendResponse(sending *sync.Mutex, req *Request, reply interface{}, enc *gob.Encoder, errmsg string)
became
func sendResponse(sending *sync.Mutex, req *Request, reply interface{}, enc ServerCodec, errmsg string)
We then wrote a trivial gobServerCodec
wrapper to reproduce the original functionality. From there it is simple to build a jsonServerCodec
.
After some similar changes to the client side, this was the full extent of the work we needed to do on the RPC package. This whole exercise took about 20 minutes! After tidying up and testing the new code, the final changeset was submitted.
In an inheritance-oriented language like Java or C++, the obvious path would be to generalize the RPC class, and create JsonRPC and GobRPC subclasses. However, this approach becomes tricky if you want to make a further generalization orthogonal to that hierarchy. (For example, if you were to implement an alternate RPC standard). In our Go package, we took a route that is both conceptually simpler and requires less code be written or changed.
A vital quality for any codebase is maintainability. As needs change, it is essential to adapt your code easily and cleanly, lest it become unwieldy to work with. We believe Go‘s lightweight, composition-oriented type system provides a means of structuring code that scales.
Related articles
- HTTP/2 Server Push
- Introducing HTTP Tracing
- Generating code
- Introducing the Go Race Detector
- Go maps in action
- go fmt your code
- Organizing Go code
- Debugging Go programs with the GNU Debugger
- The Go image/draw package
- The Go image package
- The Laws of Reflection
- Error handling and Go
- "First Class Functions in Go"
- Profiling Go Programs
- A GIF decoder: an exercise in Go interfaces
- Introducing Gofix
- Godoc: documenting Go code
- Gobs of data
- C? Go? Cgo!
- JSON and Go
- Go Slices: usage and internals
- Go Concurrency Patterns: Timing out, moving on
- Defer, Panic, and Recover
- Share Memory By Communicating
以上是关于13 JSON-RPC: a tale of interfaces的主要内容,如果未能解决你的问题,请参考以下文章
[CF1166C]A Tale of Two Lands题解
[POI2013] MOR-Tales of seafaring
BZOJ3417Poi2013 Tales of seafaring 分层图BFS
to the monkey and the frog.for an eternity of ten thousand tales.什么意思?
Could not find proper version of cocoapods(1.11.2) in any of the sources
Mysql:Changes in MySQL 5.7.13 (2016-06-02, General Availability):maximum length of MySQL user names