This commit is contained in:
crusader 2018-03-23 00:41:28 +09:00
parent 2423b22685
commit 2f3869c451
3 changed files with 10 additions and 1 deletions

View File

@ -73,6 +73,10 @@ type ServerRequestCodec struct {
codec codec.Codec codec codec.Codec
} }
func (src *ServerRequestCodec) HasResponse() bool {
return src.req.ID != nil
}
// Method returns the RPC method for the current request. // Method returns the RPC method for the current request.
// //
// The method uses a dotted notation as in "Service.Method". // The method uses a dotted notation as in "Service.Method".
@ -149,7 +153,7 @@ func (src *ServerRequestCodec) WriteError(w io.Writer, status int, err error) er
func (src *ServerRequestCodec) writeServerResponse(w io.Writer, res *serverResponse) error { func (src *ServerRequestCodec) writeServerResponse(w io.Writer, res *serverResponse) error {
// ID is null for notifications and they don't have a response. // ID is null for notifications and they don't have a response.
if res.ID != nil { if src.req.ID != nil {
encoder := json.NewEncoder(src.codec.Encode(w)) encoder := json.NewEncoder(src.codec.Encode(w))
// Not sure in which case will this happen. But seems harmless. // Not sure in which case will this happen. But seems harmless.
if err := encoder.Encode(res); nil != err { if err := encoder.Encode(res); nil != err {

View File

@ -15,6 +15,7 @@ type ServerCodec interface {
type ServerRequestCodec interface { type ServerRequestCodec interface {
RegistryCodec RegistryCodec
HasResponse() bool
WriteResponse(w io.Writer, reply interface{}) error WriteResponse(w io.Writer, reply interface{}) error
WriteError(w io.Writer, status int, err error) error WriteError(w io.Writer, status int, err error) error
} }

View File

@ -239,6 +239,10 @@ func handleRequest(s *rpcServlet, requestCodec protocol.ServerRequestCodec) {
result, err := s.sh.Invoke(s.ctx, requestCodec) result, err := s.sh.Invoke(s.ctx, requestCodec)
if !requestCodec.HasResponse() {
return
}
rs := &responseState{ rs := &responseState{
requestCodec: requestCodec, requestCodec: requestCodec,
result: result, result: result,