• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang transport.Request类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中go/uber/org/yarpc/transport.Request的典型用法代码示例。如果您正苦于以下问题:Golang Request类的具体用法?Golang Request怎么用?Golang Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Request类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Call

func (c jsonClient) Call(ctx context.Context, reqMeta yarpc.CallReqMeta, reqBody interface{}, resBodyOut interface{}) (yarpc.CallResMeta, error) {
	treq := transport.Request{
		Caller:   c.ch.Caller(),
		Service:  c.ch.Service(),
		Encoding: Encoding,
	}
	meta.ToTransportRequest(reqMeta, &treq)

	encoded, err := json.Marshal(reqBody)
	if err != nil {
		return nil, encoding.RequestBodyEncodeError(&treq, err)
	}

	treq.Body = bytes.NewReader(encoded)
	tres, err := c.ch.GetUnaryOutbound().Call(ctx, &treq)

	if err != nil {
		return nil, err
	}

	dec := json.NewDecoder(tres.Body)
	if err := dec.Decode(resBodyOut); err != nil {
		return nil, encoding.ResponseBodyDecodeError(&treq, err)
	}

	if err := tres.Body.Close(); err != nil {
		return nil, err
	}

	return meta.FromTransportResponse(tres), nil
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:31,代码来源:outbound.go


示例2: ToTransportRequest

// ToTransportRequest fills the given transport request with information from
// the given ReqMeta.
func ToTransportRequest(reqMeta yarpc.CallReqMeta, req *transport.Request) {
	if reqMeta == nil {
		return
	}
	req.Procedure = reqMeta.GetProcedure()
	req.ShardKey = reqMeta.GetShardKey()
	req.RoutingKey = reqMeta.GetRoutingKey()
	req.RoutingDelegate = reqMeta.GetRoutingDelegate()
	req.Headers = transport.Headers(reqMeta.GetHeaders())
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:12,代码来源:req.go


示例3: buildTransportRequest

func (c thriftClient) buildTransportRequest(
	reqMeta yarpc.CallReqMeta,
	reqBody envelope.Enveloper,
) (*transport.Request, protocol.Protocol, error) {

	proto := c.p
	if !c.Enveloping {
		proto = disableEnvelopingProtocol{
			Protocol: proto,
			Type:     wire.Reply, // we only decode replies with this instance
		}
	}

	treq := transport.Request{
		Caller:   c.ch.Caller(),
		Service:  c.ch.Service(),
		Encoding: Encoding,
	}
	meta.ToTransportRequest(reqMeta, &treq)
	// Always override the procedure name to the Thrift procedure name.
	treq.Procedure = procedureName(c.thriftService, reqBody.MethodName())

	value, err := reqBody.ToWire()
	if err != nil {
		// ToWire validates the request. If it failed, we should return the error
		// as-is because it's not an encoding error.
		return nil, nil, err
	}

	reqEnvelopeType := reqBody.EnvelopeType()
	if reqEnvelopeType != wire.Call && reqEnvelopeType != wire.OneWay {
		return nil, nil, encoding.RequestBodyEncodeError(
			&treq, errUnexpectedEnvelopeType(reqEnvelopeType),
		)
	}

	var buffer bytes.Buffer
	err = proto.EncodeEnveloped(wire.Envelope{
		Name:  reqBody.MethodName(),
		Type:  reqEnvelopeType,
		SeqID: 1, // don't care
		Value: value,
	}, &buffer)
	if err != nil {
		return nil, nil, encoding.RequestBodyEncodeError(&treq, err)
	}

	treq.Body = &buffer
	return &treq, proto, nil
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:50,代码来源:outbound.go


示例4: Call

func (c *cacheFilter) Call(ctx context.Context, request *transport.Request, out transport.UnaryOutbound) (*transport.Response, error) {
	data := *c

	// Read the entire request body to match against the cache
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, err
	}
	request.Body = ioutil.NopCloser(bytes.NewReader(body))

	if v, ok := data[string(body)]; ok {
		fmt.Println("cache hit")
		return &transport.Response{
			Headers: v.Headers,
			Body:    ioutil.NopCloser(bytes.NewReader(v.Body)),
		}, nil
	}

	fmt.Println("cache miss")
	res, err := out.Call(ctx, request)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	resBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	data[string(body)] = entry{Headers: res.Headers, Body: resBody}
	res.Body = ioutil.NopCloser(bytes.NewReader(resBody))
	return res, nil
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:34,代码来源:cache.go


示例5: CallOneway

func (c jsonClient) CallOneway(ctx context.Context, reqMeta yarpc.CallReqMeta, reqBody interface{}) (transport.Ack, error) {
	treq := transport.Request{
		Caller:   c.ch.Caller(),
		Service:  c.ch.Service(),
		Encoding: Encoding,
	}
	meta.ToTransportRequest(reqMeta, &treq)

	var buff bytes.Buffer
	if err := json.NewEncoder(&buff).Encode(reqBody); err != nil {
		return nil, encoding.RequestBodyEncodeError(&treq, err)
	}

	treq.Body = &buff

	return c.ch.GetOnewayOutbound().CallOneway(ctx, &treq)
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:17,代码来源:outbound.go


示例6: NewRequestMatcher

// NewRequestMatcher constructs a new RequestMatcher from the given testing.T
// and request.
//
// The request's contents are read in their entirety and replaced with a
// bytes.Reader.
func NewRequestMatcher(t *testing.T, r *transport.Request) RequestMatcher {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("failed to read request body: %v", err)
	}

	// restore a copy of the body so that the caller can still use the request
	// object
	r.Body = bytes.NewReader(body)
	return RequestMatcher{t: t, req: r, body: body}
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:16,代码来源:reqres.go


示例7: requestToRequestRecord

func (r *Recorder) requestToRequestRecord(request *transport.Request) requestRecord {
	requestBody, err := ioutil.ReadAll(request.Body)
	if err != nil {
		r.logger.Fatal(err)
	}
	request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
	return requestRecord{
		Caller:          request.Caller,
		Service:         request.Service,
		Procedure:       request.Procedure,
		Encoding:        string(request.Encoding),
		Headers:         request.Headers.Items(),
		ShardKey:        request.ShardKey,
		RoutingKey:      request.RoutingKey,
		RoutingDelegate: request.RoutingDelegate,
		Body:            requestBody,
	}
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:18,代码来源:recorder.go


示例8: handleOnewayRequest

func handleOnewayRequest(
	ctx context.Context,
	span opentracing.Span,
	treq *transport.Request,
	onewayHandler transport.OnewayHandler,
) error {
	// we will lose access to the body unless we read all the bytes before
	// returning from the request
	var buff bytes.Buffer
	if _, err := io.Copy(&buff, treq.Body); err != nil {
		return err
	}
	treq.Body = &buff

	go func() {
		// ensure the span lasts for length of the request in case of errors
		defer span.Finish()

		err := internal.SafelyCallOnewayHandler(ctx, onewayHandler, treq)
		updateSpanWithErr(span, err)
	}()
	return nil
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:23,代码来源:handler.go



注:本文中的go/uber/org/yarpc/transport.Request类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang zap.New函数代码示例发布时间:2022-05-28
下一篇:
Golang thrift.Response类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap