Skip to content

Commit

Permalink
MINOR: add option to ignore socket failures on start
Browse files Browse the repository at this point in the history
this add option to start HAProxy later
  • Loading branch information
oktalz committed Feb 27, 2024
1 parent cac55e0 commit d3babf0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions runtime/options/do-not-check-runtime-on-init.go
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions runtime/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions runtime/runtime_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}
Expand Down
17 changes: 13 additions & 4 deletions runtime/runtime_single_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net"
"strings"
"time"

"github.com/haproxytech/client-native/v6/runtime/options"
)

const (
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d3babf0

Please sign in to comment.