trpc.group/trpc-go/trpc-go@v1.0.3/server/full_link_timeout.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package server
    15  
    16  import (
    17  	"context"
    18  
    19  	"trpc.group/trpc-go/trpc-go/errs"
    20  	"trpc.group/trpc-go/trpc-go/filter"
    21  )
    22  
    23  // mayConvert2FullLinkTimeout infers whether an error is caused by a full-link
    24  // timeout. If so, it returns the full-link timeout error.
    25  func mayConvert2FullLinkTimeout(
    26  	ctx context.Context,
    27  	req interface{},
    28  	next filter.ServerHandleFunc,
    29  ) (interface{}, error) {
    30  	rsp, err := next(ctx, req)
    31  	if e, ok := err.(*errs.Error); ok &&
    32  		e.IsTimeout(errs.ErrorTypeFramework) &&
    33  		e.Code != errs.RetClientTimeout {
    34  		e.Code = errs.RetServerFullLinkTimeout
    35  	}
    36  	return rsp, err
    37  }
    38  
    39  // mayConvert2NormalTimeout infers whether an error is caused by a server
    40  // timeout. If so, it returns the server timeout error.
    41  func mayConvert2NormalTimeout(
    42  	ctx context.Context,
    43  	req interface{},
    44  	next filter.ServerHandleFunc,
    45  ) (interface{}, error) {
    46  	rsp, err := next(ctx, req)
    47  	if e, ok := err.(*errs.Error); ok &&
    48  		e.IsTimeout(errs.ErrorTypeFramework) &&
    49  		e.Code != errs.RetClientTimeout {
    50  		e.Code = errs.RetServerTimeout
    51  	}
    52  	return rsp, err
    53  }