示例#1
0
func (self *ProtobufClient) sendResponse(response *protocol.Response) {
	self.requestBufferLock.RLock()
	req, ok := self.requestBuffer[*response.RequestId]
	self.requestBufferLock.RUnlock()
	if !ok {
		return
	}

	deleteRequest := false
	switch rt := response.GetType(); rt {
	case protocol.Response_END_STREAM,
		protocol.Response_HEARTBEAT,
		protocol.Response_ERROR:
		deleteRequest = true
	case protocol.Response_QUERY:
		// do nothing
	default:
		panic(fmt.Errorf("Unknown response type: %s", rt))
	}

	self.requestBufferLock.Lock()
	req, ok = self.requestBuffer[*response.RequestId]
	if deleteRequest {
		delete(self.requestBuffer, *response.RequestId)
	}
	self.requestBufferLock.Unlock()
	if !ok {
		return
	}

	log.Debug("ProtobufClient yielding to %s %s", req.r.Name(), response)
	req.r.Yield(response)
}
func (self *ProtobufRequestHandler) WriteResponse(conn net.Conn, response *protocol.Response) error {
	if response.Size() >= MAX_RESPONSE_SIZE {
		f, s := splitResponse(response)
		err := self.WriteResponse(conn, f)
		if err != nil {
			return err
		}
		return self.WriteResponse(conn, s)
	}

	data, err := response.Encode()
	if err != nil {
		log.Error("error encoding response: %s", err)
		return err
	}

	buff := bytes.NewBuffer(make([]byte, 0, len(data)+8))
	binary.Write(buff, binary.LittleEndian, uint32(len(data)))
	_, err = conn.Write(append(buff.Bytes(), data...))
	if err != nil {
		log.Error("error writing response: %s", err)
		return err
	}
	return nil
}