@@ -30,35 +30,53 @@ import (
3030 errors "github.com/pkg/errors"
3131)
3232
33+ // ApiClientTransport wraps a runtime.ClientTransport with its associated API URL,
34+ // enabling tracking of which controller endpoint a transport communicates with.
3335type ApiClientTransport struct {
3436 runtime.ClientTransport
3537 ApiUrl * url.URL
3638}
3739
38- // ClientTransportPool abstracts the concept of multiple ` runtime.ClientTransport` (openapi interface) representing one
39- // target OpenZiti network. In situations where controllers are running in HA mode (multiple controllers) this
40- // interface can attempt to try a different controller during outages or partitioning .
40+ // ClientTransportPool manages multiple runtime.ClientTransport instances representing
41+ // different controller endpoints in a high-availability OpenZiti network. It provides
42+ // automatic failover capabilities when individual controllers become unavailable .
4143type ClientTransportPool interface {
4244 runtime.ClientTransport
4345
46+ // Add registers a new transport for the specified API URL.
4447 Add (apiUrl * url.URL , transport runtime.ClientTransport )
48+
49+ // Remove unregisters the transport for the specified API URL.
4550 Remove (apiUrl * url.URL )
4651
52+ // GetActiveTransport returns the currently selected transport.
4753 GetActiveTransport () * ApiClientTransport
54+
55+ // SetActiveTransport designates which transport to use for subsequent operations.
4856 SetActiveTransport (* ApiClientTransport )
57+
58+ // GetApiUrls returns all registered API URLs.
4959 GetApiUrls () []* url.URL
60+
61+ // IterateTransportsRandomly provides a channel for iterating through available transports
62+ // in random order.
5063 IterateTransportsRandomly () chan <- * ApiClientTransport
5164
65+ // TryTransportsForOp attempts to execute an operation, trying different transports
66+ // on connection failures.
5267 TryTransportsForOp (operation * runtime.ClientOperation ) (any , error )
68+
69+ // TryTransportForF executes a callback function, trying different transports
70+ // on connection failures.
5371 TryTransportForF (cb func (* ApiClientTransport ) (any , error )) (any , error )
5472}
5573
5674var _ runtime.ClientTransport = (ClientTransportPool )(nil )
5775var _ ClientTransportPool = (* ClientTransportPoolRandom )(nil )
5876
59- // ClientTransportPoolRandom selects a client transport (controller) at random until it is unreachable. Controllers
60- // are tried at random until a controller is reached. The newly connected controller is set for use on future requests
61- // until is too becomes unreachable.
77+ // ClientTransportPoolRandom implements a randomized failover strategy for controller selection.
78+ // It maintains an active transport and switches to randomly selected alternatives when the active
79+ // transport becomes unreachable.
6280type ClientTransportPoolRandom struct {
6381 pool cmap.ConcurrentMap [string , * ApiClientTransport ]
6482 current atomic.Pointer [ApiClientTransport ]
@@ -107,6 +125,7 @@ func (c *ClientTransportPoolRandom) GetActiveTransport() *ApiClientTransport {
107125 return active
108126}
109127
128+ // GetApiClientTransports returns a snapshot of all registered transports.
110129func (c * ClientTransportPoolRandom ) GetApiClientTransports () []* ApiClientTransport {
111130 var result []* ApiClientTransport
112131
@@ -117,6 +136,7 @@ func (c *ClientTransportPoolRandom) GetApiClientTransports() []*ApiClientTranspo
117136 return result
118137}
119138
139+ // NewClientTransportPoolRandom creates a new transport pool with randomized failover.
120140func NewClientTransportPoolRandom () * ClientTransportPoolRandom {
121141 return & ClientTransportPoolRandom {
122142 pool : cmap .New [* ApiClientTransport ](),
@@ -215,6 +235,7 @@ func (c *ClientTransportPoolRandom) TryTransportForF(cb func(*ApiClientTransport
215235 return lastResult , lastErr
216236}
217237
238+ // AnyTransport returns a randomly selected transport from the pool, or nil if empty.
218239func (c * ClientTransportPoolRandom ) AnyTransport () * ApiClientTransport {
219240 transportBuffer := c .pool .Items ()
220241 var keys []string
@@ -237,6 +258,8 @@ var _ ClientTransportPool = (*ClientTransportPoolRandom)(nil)
237258
238259var opError = & net.OpError {}
239260
261+ // errorIndicatesControllerSwap determines whether an error suggests the need to
262+ // switch to a different controller endpoint.
240263func errorIndicatesControllerSwap (err error ) bool {
241264 pfxlog .Logger ().WithError (err ).Debugf ("checking for network errror on type (%T) and its wrapped errors" , err )
242265
0 commit comments