MessagePack support
Som-Som-CC opened this issue · comments
MessagePack (a.k.a. msgpack) is a great data interchange format. It is often cited as binary JSON. Flexible like JSON, but more efficient. Not as snappy as Protocol Buffers/gRPC, but more flexible and still fast.
The aim of this enhancement:
- As a client send request payload as JSON, in the meanwhile add
Accept
header indicating the server that the client supports msgpack, ifclient.MsgPack(true)
was called. - As a client be able to process msgpack responses, if MIME type is
application/msgpack
. - As a server be able to decode msgpack payload, if MIME type is
application/msgpack
. - As a server prefer sending response encoded as msgpack, if
Accept
header indicates support. Otherwise use JSON.
Go standard JSON library uses "json" tagging. The msgpack package chosen must be able to use that tagging for smooth interworking.
sequenceDiagram
note over C: MessagePack discovery phase
C->>S: POST: CT:application/json, Accept:application/msgpack,application/json, body=JSON
S->>C: 201: CT=application/msgpack, body=msgpack
note over C: MessagePack is known to be supported
C->>S: PUT: CT:application/msgpack, Accept:application/msgpack,application/json, body=msgpack
S->>C: 200: CT=application/msgpack, body=msgpack
https://github.com/vmihailenco/msgpack/ seems to be a nicely maintained msgpack implementation with SetCustomStructTag
functions.
For large structs it seems to be twice as fast as encoding/json with half the number of allocations (though memory use on a par or even higher) and somewhat more dense binary data. That is what one would expect when reading msgpack specification.
You may ask why MessagePack when there are more popular and snappier protocols. Such as Protobuf, used by gRPC, Connect RPC or Twirp.
There can be many reasons. For example, these are much more than versatile than a RESTful resource manipulation. Protobuf is not a drop-in replacement for JSON, hence its efficiency. And most importantly, these libs provide all the bells and whistles what this restful lib provides.