Пример #1
0
// SafelyCallUnaryHandler calls the handler h, recovering panics and timeout errors,
// converting them to yarpc errors. All other errors are passed trough.
func SafelyCallUnaryHandler(
	ctx context.Context,
	h transport.UnaryHandler, start time.Time,
	req *transport.Request,
	resq transport.ResponseWriter,
) (err error) {
	// We recover panics from now on.
	defer func() {
		if r := recover(); r != nil {
			log.Printf("Unary handler panicked: %v\n%s", r, debug.Stack())
			err = fmt.Errorf("panic: %v", r)
		}
	}()

	err = h.Handle(ctx, req, resq)

	// The handler stopped work on context deadline.
	if err == context.DeadlineExceeded && err == ctx.Err() {
		deadline, _ := ctx.Deadline()
		err = errors.HandlerTimeoutError(req.Caller, req.Service,
			req.Procedure, deadline.Sub(start))
	}
	return err
}
Пример #2
0
func (c *countInterceptor) Handle(
	ctx context.Context, req *transport.Request, resw transport.ResponseWriter, h transport.UnaryHandler) error {
	c.Count++
	return h.Handle(ctx, req, resw)
}
Пример #3
0
func (requestLogInterceptor) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, handler transport.UnaryHandler) error {
	fmt.Printf("received a request to %q from client %q (encoding %q)\n",
		req.Procedure, req.Caller, req.Encoding)
	return handler.Handle(ctx, req, resw)
}