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

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

  1. 測试JSON RPC远程调用(JSONclient)

    #include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Auth ...

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

    JS 循环遍历JSON数据 分类: JS技术 JS JQuery2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options&q ...

  3. JSON 的含义?

    JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格式.JS ON 与 XML 具有相同的特性,例如易于人编写和阅读,易于机器生成和解析.但是 JSON ...

  4. 18 A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练

    A GIF decoder: an exercise in Go interfaces  一个GIF解码器:go语言接口训练 25 May 2011 Introduction At the Googl ...

  5. Go语言_RPC_Go语言的RPC

    一 标准库的RPC RPC(Remote Procedure Call,远程过程调用)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络细节的应用程序通信协议.简单的说就是要像调用本地函数 ...

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

    atitit.基于http  json  api 接口设计 最佳实践 总结o7 1. 需求:::服务器and android 端接口通讯 2 2. 接口开发的要点 2 2.1. 普通参数 meth,p ...

  7. 21 JSON and Go go语言和json

    JSON and Go 25 January 2011 Introduction JSON (JavaScript Object Notation) is a simple data intercha ...

  8. 包含utf8字符的 pickle 转 json的大坑处理过程

    背景:希望将pickle转换为json,由于pickle里有utf8的字符,因此转换失败. 转换代码如下: ''' Convert a pkl file into json file ''' impo ...

  9. RPC是什么?科普一下

    RPC概念及分类 RPC全称为Remote Procedure Call,翻译过来为“远程过程调用”.目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程通信和相互调用 ...

随机推荐

  1. 【2018ICPC沈阳】

    哈密顿距离转切比雪夫距离,多种颜色跟两种颜色没有区别,记录最大最小次大次小即可. 圆求交点.

  2. PHP-从零开始使用Solr搜索引擎服务(下)

    前言: 原文地址: http://www.cnblogs.com/JimmyBright/p/7156085.html 前面在配置完成Solr服务之后,在浏览器上可以打开Solr的管理界面,这个界面几 ...

  3. 洛谷 P3235 [HNOI2014]江南乐 解题报告

    P3235 [HNOI2014]江南乐 Description 两人进行 T 轮游戏,给定参数 F ,每轮给出 N 堆石子,先手和后手轮流选择石子数大于等于 F 的一堆,将其分成任意(大于1)堆,使得 ...

  4. linux内核分析 第八周 理解进程调度时机跟踪分析进程调度与进程切换的过程

    笔记: 实验:使用gdb跟踪分析一个schedule()函数

  5. 构建工具-----Gradle(二)-----myeclipse 10和myeclipse2015安装gradle插件----其他版本的myeclipse类似

    我们需要给myeclipse安装gradle的插件.这样myeclipse就能识别到gradle项目了,直接加载进去即可. 我们先安装配置系统命令行的gradle,挺简单的,下载后配置环境变量即可,详 ...

  6. Linux下vim 快捷键

    vim按d表示剪切 按dd剪切一行 vim命令:命令模式 /关键字 n继续向下查找vim的多行注释: 1.按ctrl + v进入 visual block模式 2.按上下选中要注释的行 3.按大写字母 ...

  7. C#线程篇---线程池如何管理线程(6完结篇)

    C#线程基础在前几篇博文中都介绍了,现在最后来挖掘一下线程池的管理机制,也算为这个线程基础做个完结. 我们现在都知道了,线程池线程分为工作者线程和I/O线程,他们是怎么管理的? 对于Microsoft ...

  8. Java入门:Java中获取键盘输入值的三种方法

    Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代 ...

  9. vue 混入的理解

  10. c# dev GridControl多选当前行显示样式问题

    由于Dev GridControl在支持多选的时候,如果如果焦点行单独加了样式,Appearance-->FocusedRow  &  HideSelectionRow 这个时候,鼠标焦 ...