server/rpc_codec: if c.codec.Write fails, reset write buffer and encode an error message about the encoding failure
When developing go-micro services, it is frequently possible to set invalid results in the response pointer. When this happens (as I and @trushton personally experienced), `sendResponse()` returns an error correctly explaining what happened (e.g. protobuf refused to encode a bad struct) but the `call()` function one above it in the stack ignores the returned error object.
Thus, invalid structs go un-encoded and the _client side times out_. @trushton and I first caught this in our CI builds when we left a protobuf.Empty field uninitialized (nil) instead of setting it to `&ptypes.Empty{}`. This resulted in an `proto: oneof field has nil value` error, but it was dropped and became a terribly confusing client timeout instead.
This patch is two independent changes:
* In rpc_codec, when a serialization failure occurs serialize an error message, which will correctly become a 500 for HTTP services, about the encoding failure. This means rpc_codec only returns an `error` when a socket failure occurs, which I believe is the behavior that rpc_service is expecting anyway.
* In rpc_service, log any errors returned by sendResponse instead of dropping the error object. This will make debugging client timeouts less of a hassle.
			
			
This commit is contained in:
		| @@ -7,6 +7,7 @@ import ( | ||||
| 	"github.com/micro/go-micro/codec/jsonrpc" | ||||
| 	"github.com/micro/go-micro/codec/protorpc" | ||||
| 	"github.com/micro/go-micro/transport" | ||||
| 	"github.com/pkg/errors" | ||||
| ) | ||||
|  | ||||
| type rpcPlusCodec struct { | ||||
| @@ -96,7 +97,11 @@ func (c *rpcPlusCodec) WriteResponse(r *response, body interface{}, last bool) e | ||||
| 		Header: map[string]string{}, | ||||
| 	} | ||||
| 	if err := c.codec.Write(m, body); err != nil { | ||||
| 		return err | ||||
| 		c.buf.wbuf.Reset() | ||||
| 		m.Error = errors.Wrapf(err, "Unable to encode body").Error() | ||||
| 		if err := c.codec.Write(m, nil); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	m.Header["Content-Type"] = c.req.Header["Content-Type"] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user