1. 程式人生 > >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.

相關推薦

13 JSON-RPC: a tale of interfaces

signature ace fec vid lex class it is () present JSON-RPC: a tale of interfaces 27 April 2010 Here we present an example where Go‘s inte

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 t

A tale of two viewports

終於能看到中文版的PPK大牛寫的viewport故事了,感謝翻譯的作者。看完之後,之前需要混亂的概念也逐漸清晰了,以下記錄一些要點,以備不時翻看,詳情內容請移步原文和譯文網址拜讀。 以下為本人摘錄和自己理解的內容(其中摘錄部分均來自上述兩篇譯文中)

Intelligent Parking: A Tale of Five Cities

We can see the precursors of this nightmarish scenario in action today. Traffic and pollution already constitute major societal challenges that demand smar

A Tale of Three Buttons: Exploring React Native Styling

A Tale of Three Buttons: Exploring React Native StylingTaking a broad view of styling in React Native.The final solution of this article is available for d

A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets(中英雙語)

文章標題 A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets 且談Apache Spark的API三劍客:RDD、DataFrame和Dataset When to use them and why 什麼時候用他們,為什麼

Brain-Computer Interfaces and Augmented Reality: A State of the Art

Brain-Computer Interfaces and Augmented Reality: A State of the Art 腦機介面和增強現實:最先進的技術 文章目錄 Brain-Computer Interfaces and Augmented R

遇到問題---org.apache.struts2.json.JSONWriter can not access a member of class

啟動tomcat後 訪問某個action 跳轉到jsp是報錯  org.apache.struts2.json.JSONWriter can not access a member of class

org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp...

就會 log 要去 相同 source 系列 交互 ali who 版權聲明:本文為博主原創文章。未經博主同意不得轉載。 https://blog.csdn.net/bruce_6/art

後端程序員之路 52、A Tour of Go-2

run arrays primes var auto 程序 pointer ase tex # flowcontrol - for - for i := 0; i < 10; i++ { - for ; sum < 1000;

freedom is a kind of responsibility

重寫 小學 三個月 一個 經濟 創新 裏的 它的 整形 張維迎教授在2017年7月1日北大國發院2017屆畢業典禮上的發言《自由是一種責任》 張維迎:自由是一種責任 本文為張維迎教授在2017年7月1日北大國發院2017屆畢業典禮上的發言 同學們好!首先祝賀大

03 Complementing a Strand of DNA

osal vco str tga truct nat dual dataset vid Problem In DNA strings, symbols ‘A‘ and ‘T‘ are complements of each other, as are ‘C‘ and ‘G

error: ‘kEmptyString’ is not a member of ‘google::protobuf::internal’

ring anaconda uil ble /usr nac space locate bin 最近安裝caffe,突然報這個錯: .build_release/src/caffe/proto/caffe.pb.h: In member function ‘void caf

Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers(暴力)

esp for int ans logs and codeforce style inf 枚舉質數,判斷是否超過臨界值。臨界值就是將不是因子中不含這個素數的數的個數乘以x和y的較小值,是否小於當前最小值。 #include <algorithm> #inclu

A glimpse of Support Vector Machine

gui 機器 相同 即使 vector ref kernel 好的 imp 支持向量機(support vector machine, 以下簡稱svm)是機器學習裏的重要方法,特別適用於中小型樣本、非線性、高維的分類和回歸問題。本篇希望在正篇提供一個svm的簡明

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

perm eve mit can source string per ted idt 在java中寫switch代碼時,參數用的是string,jdk用的是1.8,但是還是報錯,說不支持1.7版本以下的,然後查找了項目中的一些文件,打開一個文件如下,發現是1.6的版本,好奇

[React] Create a queue of Ajax requests with redux-observable and group the results.

exe new sta fault merge tasks return cer convert With redux-observable, we have the power of RxJS at our disposal - this means tasks that

A Bite Of React(1)

lec add constant ant app span javascrip imp this react: component and views : produce html abd add them on a page( in the dom) <impor

sbt編譯spark程序提示value toDF is not a member of Seq()

但是 編譯 sse url main函數 case 一個 fine clas sbt編譯spark程序提示value toDF is not a member of Seq() 前提 使用Scala編寫的Spark程序,在sbt編譯打包的時候提示value toDF is

Spring Boot:Action:Consider defining a bean of type '*.*.*' in your configuration解決方案

rri framework 成功 ould script ota http per apache   果然不看教程直接使用在遇到問題會懵逼,連解決問題都得搜半天還不一定能幫你解決了。。。 ***************************APPLICATION FAIL