Exemplo n.º 1
0
func handlerTimeoutRawHandler(w http.ResponseWriter, req *http.Request) {
	start := time.Now()
	err := errors.HandlerTimeoutError("caller", "service",
		"handlertimeout/raw", time.Now().Sub(start))
	w.WriteHeader(http.StatusGatewayTimeout)
	fmt.Fprint(w, err.Error())
}
Exemplo n.º 2
0
func (handlerTimeoutRawHandler) Handle(ctx context.Context, args *raw.Args) (*raw.Res, error) {
	start := time.Now()
	err := errors.HandlerTimeoutError("caller", "service",
		"handlertimeout/raw", time.Now().Sub(start))
	err = errors.AsHandlerError("service", "handlertimeout/raw", err)
	err = tchannel.NewSystemError(tchannel.ErrCodeTimeout, err.Error())
	return nil, err
}
Exemplo n.º 3
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
}