Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 |
| 17 | package vpagent |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "strings" |
| 22 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
| 24 | "google.golang.org/grpc/status" |
| 25 | ) |
| 26 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 27 | // IsConnCanceled returns true, if error is from a closed gRPC connection. |
| 28 | // ref. https://github.com/grpc/grpc-go/pull/1854 |
| 29 | func 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 | } |