From be3bd9796736f64ea7e8aa5bba63779f93aabff9 Mon Sep 17 00:00:00 2001 From: Kenneth Bingham Date: Tue, 5 Mar 2024 10:43:13 -0500 Subject: [PATCH] consolidate redundant unix function defs --- common/profiler/cpu_darwin.go | 59 ------------------- common/profiler/cpu_linux.go | 59 ------------------- .../profiler/{cpu_freebsd.go => cpu_unix.go} | 2 + common/profiler/cpu_windows.go | 2 + router/monitor_darwin.go | 47 --------------- router/monitor_linux.go | 47 --------------- .../{monitor_freebsd.go => monitor_unix.go} | 2 + router/monitor_windows.go | 2 + tunnel/intercept/tproxy/tproxy_freebsd.go | 2 +- 9 files changed, 9 insertions(+), 213 deletions(-) delete mode 100644 common/profiler/cpu_darwin.go delete mode 100644 common/profiler/cpu_linux.go rename common/profiler/{cpu_freebsd.go => cpu_unix.go} (97%) delete mode 100644 router/monitor_darwin.go delete mode 100644 router/monitor_linux.go rename router/{monitor_freebsd.go => monitor_unix.go} (97%) diff --git a/common/profiler/cpu_darwin.go b/common/profiler/cpu_darwin.go deleted file mode 100644 index 1f46c466a..000000000 --- a/common/profiler/cpu_darwin.go +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright NetFoundry Inc. - - 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 - - https://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 profiler - -import ( - "github.com/michaelquigley/pfxlog" - "os" - "os/signal" - "runtime/pprof" - "syscall" -) - -type CPU struct { - path string - shutdownC chan struct{} -} - -func NewCPU(path string) (*CPU, error) { - return NewCPUWithShutdown(path, nil) -} - -func NewCPUWithShutdown(path string, shutdownC chan struct{}) (*CPU, error) { - f, err := os.Create(path) - if err != nil { - return nil, err - } - if err := pprof.StartCPUProfile(f); err != nil { - return nil, err - } - pfxlog.Logger().Infof("cpu profiling to [%s]", path) - return &CPU{path: path, shutdownC: shutdownC}, nil -} - -func (cpu *CPU) Run() { - signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, syscall.SIGUSR2) - - select { - case <-signalChan: - case <-cpu.shutdownC: - } - - pprof.StopCPUProfile() - pfxlog.Logger().Info("stopped profiling cpu") -} diff --git a/common/profiler/cpu_linux.go b/common/profiler/cpu_linux.go deleted file mode 100644 index bd2adc942..000000000 --- a/common/profiler/cpu_linux.go +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright NetFoundry Inc. - - 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 - - https://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 profiler - -import ( - "github.com/michaelquigley/pfxlog" - "os" - "os/signal" - "runtime/pprof" - "syscall" -) - -type CPU struct { - path string - shutdownC <-chan struct{} -} - -func NewCPU(path string) (*CPU, error) { - return NewCPUWithShutdown(path, nil) -} - -func NewCPUWithShutdown(path string, shutdownC <-chan struct{}) (*CPU, error) { - f, err := os.Create(path) - if err != nil { - return nil, err - } - if err := pprof.StartCPUProfile(f); err != nil { - return nil, err - } - pfxlog.Logger().Infof("cpu profiling to [%s]", path) - return &CPU{path: path, shutdownC: shutdownC}, nil -} - -func (cpu *CPU) Run() { - signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, syscall.SIGUSR2) - - select { - case <-signalChan: - case <-cpu.shutdownC: - } - - pprof.StopCPUProfile() - pfxlog.Logger().Info("stopped profiling cpu") -} diff --git a/common/profiler/cpu_freebsd.go b/common/profiler/cpu_unix.go similarity index 97% rename from common/profiler/cpu_freebsd.go rename to common/profiler/cpu_unix.go index bd2adc942..abda81781 100644 --- a/common/profiler/cpu_freebsd.go +++ b/common/profiler/cpu_unix.go @@ -1,3 +1,5 @@ +//go:build linux || darwin || freebsd + /* Copyright NetFoundry Inc. diff --git a/common/profiler/cpu_windows.go b/common/profiler/cpu_windows.go index db9e8f083..d3cb2585b 100644 --- a/common/profiler/cpu_windows.go +++ b/common/profiler/cpu_windows.go @@ -1,3 +1,5 @@ +//go:build windows + /* Copyright NetFoundry Inc. diff --git a/router/monitor_darwin.go b/router/monitor_darwin.go deleted file mode 100644 index aeddf58de..000000000 --- a/router/monitor_darwin.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - Copyright NetFoundry Inc. - - 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 - - https://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 router - -import ( - "github.com/michaelquigley/pfxlog" - "github.com/openziti/ziti/router/forwarder" - "os" - "os/signal" - "syscall" -) - -type routerMonitor struct { - forwarder *forwarder.Forwarder - closeNotify <-chan struct{} -} - -func newRouterMonitor(forwarder *forwarder.Forwarder, closeNotify <-chan struct{}) *routerMonitor { - return &routerMonitor{forwarder: forwarder, closeNotify: closeNotify} -} - -func (routerMonitor *routerMonitor) Monitor() { - signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, syscall.SIGUSR1) - for { - select { - case <-signalChan: - pfxlog.Logger().Info("\n" + routerMonitor.forwarder.Debug()) - case <-routerMonitor.closeNotify: - return - } - } -} diff --git a/router/monitor_linux.go b/router/monitor_linux.go deleted file mode 100644 index aeddf58de..000000000 --- a/router/monitor_linux.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - Copyright NetFoundry Inc. - - 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 - - https://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 router - -import ( - "github.com/michaelquigley/pfxlog" - "github.com/openziti/ziti/router/forwarder" - "os" - "os/signal" - "syscall" -) - -type routerMonitor struct { - forwarder *forwarder.Forwarder - closeNotify <-chan struct{} -} - -func newRouterMonitor(forwarder *forwarder.Forwarder, closeNotify <-chan struct{}) *routerMonitor { - return &routerMonitor{forwarder: forwarder, closeNotify: closeNotify} -} - -func (routerMonitor *routerMonitor) Monitor() { - signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, syscall.SIGUSR1) - for { - select { - case <-signalChan: - pfxlog.Logger().Info("\n" + routerMonitor.forwarder.Debug()) - case <-routerMonitor.closeNotify: - return - } - } -} diff --git a/router/monitor_freebsd.go b/router/monitor_unix.go similarity index 97% rename from router/monitor_freebsd.go rename to router/monitor_unix.go index aeddf58de..d600ae387 100644 --- a/router/monitor_freebsd.go +++ b/router/monitor_unix.go @@ -1,3 +1,5 @@ +//go:build linux || darwin || freebsd + /* Copyright NetFoundry Inc. diff --git a/router/monitor_windows.go b/router/monitor_windows.go index 9d38eb4c3..4c9c5b998 100644 --- a/router/monitor_windows.go +++ b/router/monitor_windows.go @@ -1,3 +1,5 @@ +//go:build windows + /* Copyright NetFoundry Inc. diff --git a/tunnel/intercept/tproxy/tproxy_freebsd.go b/tunnel/intercept/tproxy/tproxy_freebsd.go index 38b2c775d..3f7ea2433 100644 --- a/tunnel/intercept/tproxy/tproxy_freebsd.go +++ b/tunnel/intercept/tproxy/tproxy_freebsd.go @@ -22,5 +22,5 @@ import ( ) func New(config Config) (intercept.Interceptor, error) { - return nil, errors.New("tproxy not supported on darwin") + return nil, errors.New("tproxy not supported on FreeBSD") }