blob: d32e0a6bbb9d7c3c212c67fed67dd203a048e7b6 [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
23 "github.com/opencord/voltha-lib-go/v7/pkg/log"
24 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
26)
27
28func init() {
29 // Setup this package so that it's log level can be modified at run time
30 var err error
31 logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{})
32 if err != nil {
33 panic(err)
34 }
35}
36
37// IsConnCanceled returns true, if error is from a closed gRPC connection.
38// ref. https://github.com/grpc/grpc-go/pull/1854
39func isConnCanceled(err error) bool {
40 if err == nil {
41 return false
42 }
43 // >= gRPC v1.23.x
44 s, ok := status.FromError(err)
45 if ok {
46 // connection is canceled or server has already closed the connection
47 return s.Code() == codes.Canceled || s.Message() == "transport is closing"
48 }
49
50 e, ok := status.FromError(err)
51 if ok {
52 // connection is canceled or server has already closed the connection
53 return e.Code() == codes.Canceled || e.Message() == "all SubConns are in TransientFailure"
54 }
55
56 // >= gRPC v1.10.x
57 if err == context.Canceled {
58 return true
59 }
60
61 // <= gRPC v1.7.x returns 'errors.New("grpc: the client connection is closing")'
62 return strings.Contains(err.Error(), "grpc: the client connection is closing")
63}