From 43fe36a2aa6a38a397a361c68ec8785cbe6a54f5 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Fri, 27 Oct 2023 13:52:56 +0300 Subject: [PATCH] resmgr,log,net: ditch unused gRPC helper bits. Since we're not a gRPC proxy any more, remove unused gRPC logger and net helper bits. Signed-off-by: Krisztian Litkey --- pkg/log/grpc-logger.go | 94 -------------------------- pkg/resmgr/main/main.go | 2 - pkg/utils/net.go | 144 ---------------------------------------- 3 files changed, 240 deletions(-) delete mode 100644 pkg/log/grpc-logger.go diff --git a/pkg/log/grpc-logger.go b/pkg/log/grpc-logger.go deleted file mode 100644 index 086506e69..000000000 --- a/pkg/log/grpc-logger.go +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2019-2020 Intel Corporation. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package log - -import ( - "fmt" - "google.golang.org/grpc/grpclog" -) - -// SetGrpcLogger sets up a logger for (google.golang.org/)grpc. -func SetGrpcLogger(source string, rate *Rate) { - var l Logger - - if source == "" { - l = Default() - } else { - l = log.get(source) - } - - if rate != nil { - l = RateLimit(l, *rate) - } - - grpclog.SetLoggerV2(&grpclogger{Logger: l}) -} - -// grpclogger implements grpclog.LoggerV2 interface for our logger. -type grpclogger struct { - Logger -} - -func (g grpclogger) Info(args ...interface{}) { - g.Logger.Debug("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Infoln(args ...interface{}) { - g.Logger.Debug("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Infof(format string, args ...interface{}) { - g.Logger.Debug(format, args...) -} - -func (g grpclogger) Warning(args ...interface{}) { - g.Logger.Warn("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Warningln(args ...interface{}) { - g.Logger.Warn("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Warningf(format string, args ...interface{}) { - g.Logger.Warn(format, args...) -} - -func (g grpclogger) Error(args ...interface{}) { - g.Logger.Error("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Errorln(args ...interface{}) { - g.Logger.Error("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Errorf(format string, args ...interface{}) { - g.Logger.Error(format, args...) -} - -func (g grpclogger) Fatal(args ...interface{}) { - g.Logger.Fatal("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Fatalln(args ...interface{}) { - g.Logger.Fatal("%s", fmt.Sprint(args...)) -} - -func (g grpclogger) Fatalf(format string, args ...interface{}) { - g.Logger.Fatal(format, args...) -} - -func (g grpclogger) V(l int) bool { - return true -} diff --git a/pkg/resmgr/main/main.go b/pkg/resmgr/main/main.go index 7f23a6660..9fdb51869 100644 --- a/pkg/resmgr/main/main.go +++ b/pkg/resmgr/main/main.go @@ -74,8 +74,6 @@ func (m *Main) Run() error { } func (m *Main) setupLoggers() { - rate := logger.Rate{Limit: logger.Every(1 * time.Minute)} - logger.SetGrpcLogger("grpc", &rate) logger.SetStdLogger("stdlog") logger.SetupDebugToggleSignal(syscall.SIGUSR1) } diff --git a/pkg/utils/net.go b/pkg/utils/net.go index 964196cd4..91b529646 100644 --- a/pkg/utils/net.go +++ b/pkg/utils/net.go @@ -16,85 +16,11 @@ package utils import ( "errors" - "fmt" "net" "os" "syscall" - "time" - - "google.golang.org/grpc" ) -// WaitForServer waits for a gRPC server to start accepting connections on a socket. -func WaitForServer(socket string, timeout time.Duration, opts ...interface{}) error { - var errChecker []func(error) bool - var dialOpts []grpc.DialOption - var connp **grpc.ClientConn - - for _, o := range opts { - switch o.(type) { - case func(error) bool: - errChecker = append(errChecker, o.(func(error) bool)) - case grpc.DialOption: - dialOpts = append(dialOpts, o.(grpc.DialOption)) - case []grpc.DialOption: - dialOpts = append(dialOpts, o.([]grpc.DialOption)...) - case **grpc.ClientConn: - if connp != nil { - return fmt.Errorf("WaitForServer: multiple net.Conn pointer options given") - } - connp = o.(**grpc.ClientConn) - default: - return fmt.Errorf("WaitForServer: invalid option of type %T", o) - } - } - - if len(errChecker) < 1 { - errChecker = []func(error) bool{isFatalDialError} - } - - if len(dialOpts) == 0 { - dialOpts = []grpc.DialOption{ - grpc.WithInsecure(), - grpc.WithBlock(), - grpc.FailOnNonTempDialError(true), - grpc.WithTimeout(timeout), - grpc.WithDialer(func(socket string, timeout time.Duration) (net.Conn, error) { - conn, err := net.Dial("unix", socket) - return conn, err - }), - } - } - - start := time.Now() - for { - conn, err := grpc.Dial(socket, dialOpts...) - if err == nil { - if connp != nil { - *connp = conn - } else { - conn.Close() - } - return nil - } - - for _, f := range errChecker { - if f(err) { - return err - } - } - - switch { - case timeout >= 0 && start.Add(timeout).Before(time.Now()): - return err - case timeout < 0 || timeout > time.Second: - time.Sleep(time.Second) - default: - time.Sleep(timeout / 2) - } - } -} - // IsListeningSocket returns true if connections are accepted on the socket. func IsListeningSocket(socket string) (bool, error) { conn, err := net.Dial("unix", socket) @@ -109,73 +35,3 @@ func IsListeningSocket(socket string) (bool, error) { return false, err } - -// Check if a socket connection error looks fatal. -// -// Notes: -// Hmm... I wonder if it is really so difficult or I am just doing -// it wrong ? We would like to find out if a connection attempt to -// a unix-domain socket fails with a fatal error, in which case we -// don't want to stick around retrying it later. -// -// We treat errors which the originating layer considers a timeout -// or a temporary error as non-fatal one. Otherwise, we single out -// a few special errors: -// - EPERM: fatal error -// - EACCES: fatal error -// - ENOENT: non-fatal, server might still come around -// - ECONNREFUSED: non-fatal, maybe a lingering socket -// - -type temporary interface { - Temporary() bool -} - -type timeout interface { - Timeout() bool -} - -type origin interface { - Origin() error -} - -func isFatalDialError(err error) bool { - for { - if e, ok := err.(temporary); ok { - if e.Temporary() { - return false - } - } - if e, ok := err.(timeout); ok { - if e.Timeout() { - return false - } - } - - switch err.(type) { - case *net.OpError: - err = err.(*net.OpError).Err - continue - case *os.SyscallError: - ne := err.(*os.SyscallError) - switch { - case os.IsPermission(ne): - return true - case os.IsNotExist(ne): - return false - case ne.Err == syscall.ECONNREFUSED: - return true - default: - err = ne - continue - } - default: - if oe, ok := err.(origin); ok { - err = oe.Origin() - continue - } - } - - return true - } -}