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.

By Andrew Gerrand

Related articles

原文地址:https://www.cnblogs.com/kaid/p/9698461.html

时间: 2024-08-26 01:26:49

13 JSON-RPC: a tale of interfaces的相关文章

测试JSON RPC远程调用(JSON客户端)

#include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSon客户端 Author: Kagula LastUpdateDate:2014-05-17 描述:测试JSON RPC远程调用 测试环境:Windows 8.1.Visual Studio 2013 SP1 curl-7.36.0 CPPCMS 1.0.4(JSON服务端) Java Servlet (JSON服务端) */ sta

測试JSON RPC远程调用(JSONclient)

#include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Author: Kagula LastUpdateDate:2014-05-17 描写叙述:測试JSON RPC远程调用 測试环境:Windows 8.1.Visual Studio 2013 SP1 curl-7.36.0 CPPCMS 1.0.4(JSON服务端) Java Servlet (JSON服务端) *

基于php的json rpc原理及应用

json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列的实现.这种远程过程调用可以使用http作为传输协议,也可以使用其它传输协议,传输的内容是json消息体. 下面我们code一套基于php的rpc框架,此框架中包含rpc的服务端server,和应用端client: (一)PHP服务端RPCserver jsonRPCServer.php class jsonRPCServer { /** *处理

JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{&amp;quot;options&amp;quot;:&amp;quot;[{

JS 循环遍历JSON数据 分类: JS技术 JS JQuery2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/"9/"},{/"text/":/"李家湾/",/"valu e/":/"10

Go语言_RPC_Go语言的RPC

一 标准库的RPC RPC(Remote Procedure Call,远程过程调用)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络细节的应用程序通信协议.简单的说就是要像调用本地函数一样调用服务器的函数. RPC协议构建于TCP或UDP,或者是 HTTP之上,允许开发者直接调用另一台计算机上的程序,而开发者无需额外地为这个调用过程编写网络通信相关代码,使得开发包括网络分布式程序在内的应用程序更加容易. Go语言的标准库已经提供了RPC框架和不同的RPC实现. 下面是一个服务器的

[JavaScript] 7.JS JSON

是什么? JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格式.JSO N 与 XML 具有相同的特性,例如易于人编写和阅读,易于机器生成和解析.但是 JSON 比 XML 数据传输的有效性要高出很多.JSON 完全独立与编程语言,使用文本格式保存. JSON 数据有两种结构: Name-Value 对构成的集合,类似于 Java 中的 Map. Value 的有序列表,类似于 Java 中的 Array. 一个 JSON 格式的数据示例: { "

18 A GIF decoder: an exercise in Go interfaces

A GIF decoder: an exercise in Go interfaces 25 May 2011 Introduction At the Google I/O conference in San Francisco on May 10, 2011, we announced that the Go language is now available on Google App Engine. Go is the first language to be made available

atitit.基于http json api 接口设计 最佳实践 总结o7

atitit.基于http  json  api 接口设计 最佳实践 总结o7 1. 需求:::serverand android 端接口通讯 2 2. 接口开发的要点 2 2.1. 普通參数 meth,param, 2 2.2. 全部的參数定义 2 2.3. key,dynami key)韩式 static key? 2 2.4. 防篡改 sign 2 2.5. Encry加密 3 2.6. zip压缩:: 3 2.7. 首先压缩韩式加密??? 3 3. 选型大全:rim ,ws, http 

架构设计:系统间通信(14)——RPC实例Apache Thrift 下篇(2)

(接上篇<架构设计:系统间通信(13)--RPC实例Apache Thrift 下篇(1)>) 3.正式开始编码 我已经在CSDN的资源区上传了这个示例工程的所有代码(http://download.csdn.net/detail/yinwenjie/9289999).读者可以直接到资源下载站进行下载(不收积分哦~~^_^).这篇文章将紧接上文,主要介绍这个工程几个主要的类代码. 3-1.编写服务端主程序 服务端主程序的类名:processor.MainProcessor,它负责在服务端启动A