blob: 5b62368508dc30f09f384ef4b69c6facab6f94d7 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16// Package vpagent Common Logger initialization
17package vpagent
18
19import (
20 "context"
21 "strings"
22
Naveen Sampath04696f72022-06-13 15:19:14 +053023 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25)
26
Naveen Sampath04696f72022-06-13 15:19:14 +053027// IsConnCanceled returns true, if error is from a closed gRPC connection.
28// ref. https://github.com/grpc/grpc-go/pull/1854
29func isConnCanceled(err error) bool {
30 if err == nil {
31 return false
32 }
33 // >= gRPC v1.23.x
34 s, ok := status.FromError(err)
35 if ok {
36 // connection is canceled or server has already closed the connection
37 return s.Code() == codes.Canceled || s.Message() == "transport is closing"
38 }
39
40 e, ok := status.FromError(err)
41 if ok {
42 // connection is canceled or server has already closed the connection
43 return e.Code() == codes.Canceled || e.Message() == "all SubConns are in TransientFailure"
44 }
45
46 // >= gRPC v1.10.x
47 if err == context.Canceled {
48 return true
49 }
50
51 // <= gRPC v1.7.x returns 'errors.New("grpc: the client connection is closing")'
52 return strings.Contains(err.Error(), "grpc: the client connection is closing")
53}