Comments (6)
Why are you trying to handle the RPC specification yourself? Go already provides a very capable net/rpc library which allows plugging in a specific codec (for encode and decode functions). My go/codec library includes support for this without you having to do the work.
//RPC Server
go func() {
for {
conn, err := listener.Accept()
rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
rpc.ServeCodec(rpcCodec)
}
}()
//RPC Communication (client side)
conn, err := net.Dial("tcp", "localhost:5555")
rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
client := rpc.NewClientWithCodec(rpcCodec)
If you want to force a specific "schema", then you need to use a specific "type" e.g. decode into *Request directly (not a variant of interface{}).
var r Request
err := dec.Decode(&r)
When you decode into an interface{}, you are asking the library to do what it considers best. We provide a few options (RawToString, map and slice type) as hints to it, but we drew the line there. Those options are clearly generally useful, with the understanding that numbers are always decoded schema-less into the largest sized numerical type (int64, uint64, float64). This is sort of in line with the philosophy of other standard packages (encoding/json, reflect, etc).
from go.
I did think about using that RPC format (and appreciate that it exists), but I consider msgpack-rpc a better choice in my application as it is a cross language RPC format based on msgpack. I also use 0mq for trasport atm which means I can't use net/rpc (which is tcp.)
The thing is that it expects it to be exactly as in the format spec, which unfortunately leaves me to put it into and take it out of the array myself. If I would do my own RPC format I could easily encode and decode a custom message struct as I see fit! I have tried that with your library and that is really smooth!
In the end I feel I still need to support msgpack-rpc, in which case I need to make sure a Go uint32 is encoded as msgpack uint32: 0xce (I think it was) and not 0xcc (as it got optimized to now.)
Hope you understand my dilemma.
from go.
This functionality is already provided.
There's codec.GoRpc (which implements the net/rpc communication protocol) and codec.MsgpackSpecRpc (which implements the msgpack defined spec rpc communication protocol, which you are now trying to re-implement).
http://godoc.org/github.com/ugorji/go/codec#MsgpackSpecRpc
In the example snippet I posted above, I used the MsgpackSpecRpc, which is what you need. The sample usage on the docs show this also.
The net/rpc library is flexible enough to allow us configure both the communication protocol and the encoding format so that integrating msgpack was seamless on both parts.
from go.
Oh, I missed that it was actually implementing the msgpack-rpc, I thought it was a Go specific rpc packed with msgpack. I'm not familiar with GoRpc so I didn't know it used a reader/writer.
I guess my task will instead be to create a 0mq reader/writer interface. Thanks for pointing that out. :)
from go.
For my purpose at the moment (internally in the application) I will use a custom message format, which will be easier to encode and decode, and almost the same size as a msgpack-rpc version. I will for sure use the built-in rpc version for the client API later, thanks again for pointing me in that direction. Here is the short message version:
type Message struct {
Command string `codec:"c"`
Args interface{} `codec:"a,omitempty"`
}
from go.
BTW,
RE: #12 (comment)
msgpack format strives to store numbers using the minimum number of bytes. Consequently, unsigned 0-255 will always be stored as msgpack uint8 (even if Go source was a uint64). Thus, a uint32 will not necessarily be encoded as 0xce. Remember that msgpack is used even by languages which don't have defined-precision types (e.g. python, ruby), and all implementations use the value to determine how to encode it.
Also, look at msgpack.go parseCustomHeader method at https://github.com/ugorji/go/blob/master/codec/msgpack.go#L673 to see how I parsed the request by hand. Had to do this, because we wanted to read the header byte-by-byte so as not to hang forever waiting for input which may never come (testing some corner cases turned this up - but I don't remember the details).
from go.
Related Issues (20)
- omitempty handling for arrays disagrees with the standard encoding/json HOT 2
- codec.MsgpackHandle.RawToString is confusingly named HOT 2
- Go 1.20 - invalid memory address or nil pointer dereference HOT 5
- Msgpack deserialization with codecgen produces wrong result HOT 3
- Unexpected encoding with interface{} key using Canonical and MapKeyAsString.
- Decoding a slice of structs encoded as arrays into a slice of structs without the same number of fields HOT 2
- Duplicate key inside of a map triggers a "cannot decode unsigned integer" msgpack decode error HOT 1
- Efficient direct conversion between supported formats? HOT 2
- Expose Handle.Name() in some form through Encoder and Decoder for use by codec.Selfer HOT 1
- `io.ErrUnexpectedEOF` should be returned instead of `io.EOF` when only partial data is present
- Add `DecodeOptions.IntType` and `DecodeOptions.FloatType` ? HOT 1
- Docs do not seem to mention case-sensitive field handling
- mem leak? HOT 1
- Tests use rand.Intn(1) for boolean rand, but always returns 0
- Decoder accepts indefinite-length CBOR strings containing chunks with mismatched major types HOT 1
- UTF-8 validation isn't applied to CBOR text strings nested inside an indefinite-length text string HOT 2
- Add support for github.com/go-json-experiment/json / UnmarshalerV2 ? HOT 1
- runtime error with golang master (furture go 1.22)
- Leading zeros is not allowed in rfc7159
- attr.go
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.