From d3babf0d31202d6f783fd5fcac70fd7d4c9e21a9 Mon Sep 17 00:00:00 2001 From: Zlatko Bratkovic Date: Tue, 27 Feb 2024 11:09:18 +0100 Subject: [PATCH] MINOR: add option to ignore socket failures on start this add option to start HAProxy later --- runtime/interface.go | 4 +-- .../options/do-not-check-runtime-on-init.go | 26 +++++++++++++++++++ runtime/options/options.go | 7 ++--- runtime/runtime_client.go | 7 +++-- runtime/runtime_single_client.go | 17 +++++++++--- 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 runtime/options/do-not-check-runtime-on-init.go diff --git a/runtime/interface.go b/runtime/interface.go index 7ef0aa2f..ca9e4d9f 100644 --- a/runtime/interface.go +++ b/runtime/interface.go @@ -161,9 +161,9 @@ func New(ctx context.Context, opt ...options.RuntimeOption) (Runtime, error) { } if c.options.MasterSocketData != nil { - err = c.initWithMasterSocket(ctx, c.options.MasterSocketData.MasterSocketPath, c.options.MasterSocketData.Nbproc) + err = c.initWithMasterSocket(ctx, c.options) } else { - err = c.initWithSockets(ctx, c.options.Sockets) + err = c.initWithSockets(ctx, c.options) } if err != nil { return nil, err diff --git a/runtime/options/do-not-check-runtime-on-init.go b/runtime/options/do-not-check-runtime-on-init.go new file mode 100644 index 00000000..a153d643 --- /dev/null +++ b/runtime/options/do-not-check-runtime-on-init.go @@ -0,0 +1,26 @@ +/* +Copyright 2022 HAProxy Technologies + +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 options + +type doNotCheckRuntimeOnInit struct{} + +func (d doNotCheckRuntimeOnInit) Set(o *RuntimeOptions) error { + o.DoNotCheckRuntimeOnInit = true + return nil +} + +var DoNotCheckRuntimeOnInit = doNotCheckRuntimeOnInit{} //nolint:gochecknoglobals diff --git a/runtime/options/options.go b/runtime/options/options.go index 10e82b00..efe7db01 100644 --- a/runtime/options/options.go +++ b/runtime/options/options.go @@ -17,9 +17,10 @@ limitations under the License. package options type RuntimeOptions struct { - MapsDir *string - MasterSocketData *masterSocketData - Sockets map[int]string + MapsDir *string + MasterSocketData *masterSocketData + Sockets map[int]string + DoNotCheckRuntimeOnInit bool } type RuntimeOption interface { diff --git a/runtime/runtime_client.go b/runtime/runtime_client.go index 9b3d12f5..74a28512 100644 --- a/runtime/runtime_client.go +++ b/runtime/runtime_client.go @@ -49,7 +49,8 @@ const ( maxBufSize = 8192 ) -func (c *client) initWithSockets(ctx context.Context, socketPath map[int]string) error { +func (c *client) initWithSockets(ctx context.Context, opt options.RuntimeOptions) error { + socketPath := opt.Sockets c.runtimes = make([]SingleRuntime, 0) for process, path := range socketPath { runtime := SingleRuntime{} @@ -63,7 +64,9 @@ func (c *client) initWithSockets(ctx context.Context, socketPath map[int]string) return nil } -func (c *client) initWithMasterSocket(ctx context.Context, masterSocketPath string, nbproc int) error { +func (c *client) initWithMasterSocket(ctx context.Context, opt options.RuntimeOptions) error { + masterSocketPath := opt.MasterSocketData.MasterSocketPath + nbproc := c.options.MasterSocketData.Nbproc if nbproc == 0 { nbproc = 1 } diff --git a/runtime/runtime_single_client.go b/runtime/runtime_single_client.go index 4142803f..62342fdb 100644 --- a/runtime/runtime_single_client.go +++ b/runtime/runtime_single_client.go @@ -21,6 +21,8 @@ import ( "net" "strings" "time" + + "github.com/haproxytech/client-native/v6/runtime/options" ) const ( @@ -57,15 +59,22 @@ type SingleRuntime struct { // give the path to the master socket path, and non 0 number for workers. Process is for // nbproc > 1. In master-worker mode it's the same as the worker number, but when having // multiple stats socket lines bound to processes then use the correct process number -func (s *SingleRuntime) Init(ctx context.Context, socketPath string, worker int, process int) error { +func (s *SingleRuntime) Init(ctx context.Context, socketPath string, worker int, process int, opt ...options.RuntimeOptions) error { + var runtimeOptions options.RuntimeOptions + if len(opt) > 0 { + runtimeOptions = opt[0] + } + s.socketPath = socketPath s.jobs = make(chan Task) s.worker = worker s.process = process go s.handleIncomingJobs(ctx) - // check if we have a valid scket - if _, err := s.ExecuteRaw("help"); err != nil { - return err + if !runtimeOptions.DoNotCheckRuntimeOnInit { + // check if we have a valid socket + if _, err := s.ExecuteRaw("help"); err != nil { + return err + } } return nil }