@@ -3,12 +3,10 @@ package cn
33import (
44 "context"
55 "fmt"
6- "strconv"
76 "sync"
87 "time"
98
109 clientnative "github.com/haproxytech/client-native/v6"
11- "github.com/haproxytech/client-native/v6/models"
1210
1311 "github.com/haproxytech/client-native/v6/configuration"
1412 configuration_options "github.com/haproxytech/client-native/v6/configuration/options"
@@ -84,72 +82,32 @@ func ConfigureRuntimeClient(ctx context.Context, confClient configuration.Config
8482 // If master socket is set and a valid unix socket, use only this
8583 if haproxyOptions .MasterRuntime != "" && misc .IsUnixSocketAddr (haproxyOptions .MasterRuntime ) {
8684 masterSocket := haproxyOptions .MasterRuntime
87- // if nbproc is set, set nbproc sockets
88- if globalConf .Nbproc > 0 {
89- nbproc := int (globalConf .Nbproc )
90- ms := runtime_options .MasterSocket (masterSocket , nbproc )
91- runtimeClient , err = runtime_api .New (ctx , mapsDir , ms , waitForRuntimeOption )
92- if err == nil {
93- return runtimeClient
94- }
95- log .Warningf ("Error setting up runtime client with master socket: %s : %s" , masterSocket , err .Error ())
96- } else {
97- // if nbproc is not set, use master socket with 1 process
98- ms := runtime_options .MasterSocket (masterSocket , 1 )
99- runtimeClient , err = runtime_api .New (ctx , mapsDir , ms , waitForRuntimeOption )
100- if err == nil {
101- return runtimeClient
102- }
103- log .Warningf ("Error setting up runtime client with master socket: %s : %s" , masterSocket , err .Error ())
85+ // nbproc has been removed, use master socket with 1 process
86+ ms := runtime_options .MasterSocket (masterSocket )
87+ runtimeClient , err = runtime_api .New (ctx , mapsDir , ms , waitForRuntimeOption )
88+ if err == nil {
89+ return runtimeClient
10490 }
91+ log .Warningf ("Error setting up runtime client with master socket: %s : %s" , masterSocket , err .Error ())
10592 }
93+
10694 runtimeAPIs := globalConf .RuntimeAPIs
107- // if no master socket set, read from first valid socket if nbproc <= 1
108- if globalConf .Nbproc <= 1 {
109- sockets := make (map [int ]string )
110- for _ , r := range runtimeAPIs {
111- if misc .IsUnixSocketAddr (* r .Address ) {
112- sockets [1 ] = * r .Address
113- socketsL := runtime_options .Sockets (sockets )
114- runtimeClient , err = runtime_api .New (ctx , mapsDir , socketsL , waitForRuntimeOption )
115- if err == nil {
116- muSocketsList .Lock ()
117- socketsList = sockets
118- muSocketsList .Unlock ()
119- return runtimeClient
120- }
121- log .Warningf ("Error setting up runtime client with socket: %s : %s" , * r .Address , err .Error ())
122- }
123- }
124- } else {
125- // else try to find process specific sockets and set them up
126- sockets := make (map [int ]string )
127- for _ , r := range runtimeAPIs {
128- //nolint:govet
129- if misc .IsUnixSocketAddr (* r .Address ) && r .Process != "" {
130- process , err := strconv .ParseInt (r .Process , 10 , 64 )
131- if err == nil {
132- sockets [int (process )] = * r .Address
133- }
95+ // if no master socket set, read from first valid socket
96+ sockets := make (map [int ]string )
97+ for _ , r := range runtimeAPIs {
98+ if misc .IsUnixSocketAddr (* r .Address ) {
99+ socketsL := runtime_options .Socket (* r .Address )
100+ runtimeClient , err = runtime_api .New (ctx , mapsDir , socketsL , waitForRuntimeOption )
101+ if err == nil {
102+ muSocketsList .Lock ()
103+ socketsList = sockets
104+ muSocketsList .Unlock ()
105+ return runtimeClient
134106 }
107+ log .Warningf ("Error setting up runtime client with socket: %s : %s" , * r .Address , err .Error ())
135108 }
136- // no process specific settings found, Issue a warning and return empty runtime client
137- if len (sockets ) == 0 {
138- log .Warning ("Runtime API not configured, found multiple processes and no stats sockets bound to them." )
139- return runtimeClient
140- // use only found process specific sockets issue a warning if not all processes have a socket configured
141- }
142- if len (sockets ) < int (globalConf .Nbproc ) {
143- log .Warning ("Runtime API not configured properly, there are more processes then configured sockets" )
144- }
145-
146- socketLst := runtime_options .Sockets (sockets )
147- runtimeClient , err = runtime_api .New (ctx , mapsDir , socketLst , waitForRuntimeOption )
148- if err == nil {
149- return runtimeClient
150- }
151- log .Warningf ("Error setting up runtime client with sockets: %v : %s" , sockets , err .Error ())
152109 }
110+
153111 if err != nil {
154112 log .Warning ("Runtime API not configured, not using it: " + err .Error ())
155113 } else {
@@ -162,40 +120,55 @@ func ConfigureRuntimeClient(ctx context.Context, confClient configuration.Config
162120}
163121
164122// ReconfigureRuntime check if runtime client need be reconfigured by comparing the current configuration with the old
165- // one (i.e. runtimeAPIsOld) and returns a callback that ReloadAgent can use to reconfigure the runtime client.
166- func ReconfigureRuntime (client clientnative.HAProxyClient , runtimeAPIsOld [] * models. RuntimeAPI ) (callbackNeeded bool , callback func (), err error ) {
123+ // one and returns a callback that ReloadAgent can use to reconfigure the runtime client.
124+ func ReconfigureRuntime (client clientnative.HAProxyClient ) (callbackNeeded bool , callback func (), err error ) {
167125 cfg , err := client .Configuration ()
168126 if err != nil {
169127 return false , nil , err
170128 }
171- _ , globalConf , err := cfg .GetGlobalConfiguration ("" )
172- if err != nil {
173- return false , nil , err
174- }
175- runtimeAPIsNew := globalConf .RuntimeAPIs
129+
176130 reconfigureRuntime := false
177- if len (runtimeAPIsOld ) != len (runtimeAPIsNew ) {
131+
132+ // client Runtime is not reconfigured yet, so client.Runtime() return the "old" runtime
133+ oldRuntime , err := client .Runtime ()
134+ // In case we have an error, we need to reconfigure
135+ if err != nil {
178136 reconfigureRuntime = true
179- } else {
180- for _ , runtimeOld := range runtimeAPIsOld {
181- if runtimeOld .Address == nil {
137+ }
138+
139+ if ! reconfigureRuntime {
140+ // If we are not using stats socket (i.e. using master socket)
141+ // Return immediately and do not reconfigure
142+ // Do not try to compare the master socket path with stats socket paths
143+ if ! oldRuntime .IsStatsSocket () {
144+ return false , nil , nil
145+ }
146+
147+ _ , globalConf , err := cfg .GetGlobalConfiguration ("" )
148+ if err != nil {
149+ return false , nil , err
150+ }
151+ runtimeAPIsNew := globalConf .RuntimeAPIs
152+
153+ oldSocketPath := oldRuntime .SocketPath ()
154+
155+ // Now check if the new configuration contains the "old" runtime socket we are using
156+ // If yes, no need to reconfigure, socket path still exists.
157+ // If not found, only then reconfigure
158+ // This, only if stats socket, not for master socket
159+ found := false
160+ for _ , runtimeNew := range runtimeAPIsNew {
161+ if runtimeNew .Address == nil {
182162 continue
183163 }
184- found := false
185- for _ , runtimeNew := range runtimeAPIsNew {
186- if runtimeNew .Address == nil {
187- continue
188- }
189- if * runtimeNew .Address == * runtimeOld .Address {
190- found = true
191- break
192- }
193- }
194- if ! found {
195- reconfigureRuntime = true
164+ if * runtimeNew .Address == oldSocketPath {
165+ found = true
196166 break
197167 }
198168 }
169+ if ! found {
170+ reconfigureRuntime = true
171+ }
199172 }
200173
201174 if reconfigureRuntime {
0 commit comments