Exemplo n.º 1
0
func (c *client) Call(ctx Context, serviceName, methodName string, req, resp thrift.TStruct) (bool, error) {
	call, err := c.sc.BeginCall(ctx, serviceName+"::"+methodName, &tchannel.CallOptions{Format: tchannel.Thrift})
	if err != nil {
		return false, err
	}

	writer, err := call.Arg2Writer()
	if err != nil {
		return false, err
	}
	if err := writeHeaders(writer, ctx.Headers()); err != nil {
		return false, err
	}
	if err := writer.Close(); err != nil {
		return false, err
	}

	writer, err = call.Arg3Writer()
	if err != nil {
		return false, err
	}

	protocol := thrift.NewTBinaryProtocolTransport(&readWriterTransport{Writer: writer})
	if err := req.Write(protocol); err != nil {
		return false, err
	}
	if err := writer.Close(); err != nil {
		return false, err
	}

	reader, err := call.Response().Arg2Reader()
	if err != nil {
		return false, err
	}

	headers, err := readHeaders(reader)
	if err != nil {
		return false, err
	}
	ctx.SetResponseHeaders(headers)
	if err := reader.Close(); err != nil {
		return false, err
	}

	success := !call.Response().ApplicationError()
	reader, err = call.Response().Arg3Reader()
	if err != nil {
		return success, err
	}

	protocol = thrift.NewTBinaryProtocolTransport(&readWriterTransport{Reader: reader})
	if err := resp.Read(protocol); err != nil {
		return success, err
	}
	if err := reader.Close(); err != nil {
		return success, err
	}

	return success, nil
}
Exemplo n.º 2
0
func writeArgs(call *tchannel.OutboundCall, headers map[string]string, req thrift.TStruct) error {
	writer, err := call.Arg2Writer()
	if err != nil {
		return err
	}
	if err := writeHeaders(writer, headers); err != nil {
		return err
	}
	if err := writer.Close(); err != nil {
		return err
	}

	writer, err = call.Arg3Writer()
	if err != nil {
		return err
	}

	wp := getProtocolWriter(writer)
	if err := req.Write(wp.protocol); err != nil {
		thriftProtocolPool.Put(wp)
		return err
	}
	thriftProtocolPool.Put(wp)
	return writer.Close()
}
Exemplo n.º 3
0
// SerializeThrift takes a thrift struct and returns the serialized bytes
// of that struct using the thrift binary protocol. This is a temporary
// measure before frames can be forwarded directly past the endpoint to the proper
// destinaiton.
func SerializeThrift(s athrift.TStruct) ([]byte, error) {
	var b []byte
	var buffer = bytes.NewBuffer(b)

	transport := athrift.NewStreamTransportW(buffer)
	if err := s.Write(athrift.NewTBinaryProtocolTransport(transport)); err != nil {
		return nil, err
	}

	if err := transport.Flush(); err != nil {
		return nil, err
	}
	return buffer.Bytes(), nil
}
Exemplo n.º 4
0
// WriteStruct writes the given Thrift struct to a writer. It pools TProtocols.
func WriteStruct(writer io.Writer, s thrift.TStruct) error {
	wp := getProtocolWriter(writer)
	err := s.Write(wp.protocol)
	thriftProtocolPool.Put(wp)
	return err
}