blob: a8046f6b1e5fe8fc883f191213dc2ea0e135d1f6 [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 }
Sridhar Ravindra64b19ca2026-01-26 22:19:07 +053033
Naveen Sampath04696f72022-06-13 15:19:14 +053034 s, ok := status.FromError(err)
35 if ok {
Sridhar Ravindra64b19ca2026-01-26 22:19:07 +053036 switch {
37 case s.Code() == codes.Canceled || s.Message() == "transport is closing":
38 // >= gRPC v1.23.x
39 // connection is canceled or server has already closed the connection
40 return true
41 case s.Message() == "all SubConns are in TransientFailure":
42 return true
43 case s.Code() == codes.Unavailable || s.Message() == "error reading from server: EOF":
44 // >= gRPC v1.76.x
45 // connection is unavailable or server has already closed the connection
46 return true
47 case s.Code() == codes.Internal || s.Code() == codes.DeadlineExceeded:
48 // connection is interrupted
49 return true
50 }
Naveen Sampath04696f72022-06-13 15:19:14 +053051 }
52
53 // >= gRPC v1.10.x
54 if err == context.Canceled {
55 return true
56 }
57
58 // <= gRPC v1.7.x returns 'errors.New("grpc: the client connection is closing")'
59 return strings.Contains(err.Error(), "grpc: the client connection is closing")
60}