Files
mxaccessgw/clients/go/mxgateway/options.go
T
Joseph Doherty 5a207f622a fix(client-go): keepalive, stream error classification, auth composition, CLI batch reuse (CGO-001..008, #257)
- Dial/DialGalaxy set HTTP/2 keepalive (30 s / 10 s / PermitWithoutStream), configurable via
  Options.KeepAliveTime/KeepAliveTimeout (CGO-001, D7)
- auth installed with chain interceptors and attached per call by Client/GalaxyClient (CGO-002)
- a server-sent Canceled status or a context deadline ends a stream with a terminal error;
  only the caller's own cancellation is silent (CGO-003)
- stream context cancelled when the stream ends (CGO-004)
- dialConnected keeps the last connection error in the returned error (CGO-005)
- WatchDeployEvents closes events before errs; CLI drains buffered events (CGO-006)
- Register/AddItem/AddItem2 return MalformedReplyError on a payload-less reply (CGO-007)
- CLI batch reuses dialed connections; bench-read-bulk backs off on failures (CGO-008)
2026-12-03 09:51:57 -05:00

140 lines
5.2 KiB
Go

package mxgateway
import (
"crypto/tls"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// Options configures gateway connections.
type Options struct {
// Endpoint is the gateway host:port address to dial.
Endpoint string
// APIKey is the bearer token attached to outgoing gRPC metadata.
APIKey string
// Plaintext disables TLS and uses insecure credentials when true.
Plaintext bool
// CACertFile points to a PEM file used to verify the gateway certificate.
CACertFile string
// ServerNameOverride overrides the TLS SNI/SAN name presented to the gateway.
ServerNameOverride string
// DialTimeout bounds the blocking Dial; zero applies a built-in default.
DialTimeout time.Duration
// CallTimeout bounds each unary RPC; zero applies a built-in default and
// negative disables the bound entirely.
CallTimeout time.Duration
// KeepAliveTime is the interval between HTTP/2 keepalive pings. Zero applies
// DefaultKeepAliveTime (30 s); negative disables client keepalive. Pings are
// sent even while no RPC is active so a half-open connection under an idle
// event stream is detected. The gateway permits client pings at this rate by
// default; an interval shorter than the server's minimum draws a GOAWAY
// "too_many_pings".
KeepAliveTime time.Duration
// KeepAliveTimeout is how long to wait for a ping acknowledgement before the
// connection is closed and in-flight RPCs fail. Zero applies
// DefaultKeepAliveTimeout (10 s).
KeepAliveTimeout time.Duration
// TLSConfig supplies a custom TLS configuration; takes precedence over
// CACertFile when TransportCredentials is unset.
TLSConfig *tls.Config
// TransportCredentials, when non-nil, overrides every other transport-level
// option and is used as-is.
TransportCredentials credentials.TransportCredentials
// DialOptions are appended to the gRPC dial options after the defaults.
DialOptions []grpc.DialOption
// RequireCertificateValidation forces TLS certificate verification even when
// no CACertFile is pinned. Default false: the gateway's self-signed cert is
// accepted without verification (internal-tool posture).
RequireCertificateValidation bool
// EventBufferSize is the number of data results the channel returned by
// Session.Events/EventsAfter buffers before a slow consumer is cancelled with
// ErrSlowConsumer. Zero or negative applies DefaultEventBufferSize. It does not
// affect SubscribeEvents, which blocks instead of dropping.
EventBufferSize int
}
// Default HTTP/2 keepalive values (D7), identical across the five gateway clients.
const (
// DefaultKeepAliveTime is the default Options.KeepAliveTime.
DefaultKeepAliveTime = 30 * time.Second
// DefaultKeepAliveTimeout is the default Options.KeepAliveTimeout.
DefaultKeepAliveTimeout = 10 * time.Second
)
// keepAliveParams returns the keepalive parameters for the options, and false when
// keepalive is disabled. PermitWithoutStream keeps pinging while no call is active.
func (o Options) keepAliveParams() (keepalive.ClientParameters, bool) {
if o.KeepAliveTime < 0 {
return keepalive.ClientParameters{}, false
}
params := keepalive.ClientParameters{
Time: o.KeepAliveTime,
Timeout: o.KeepAliveTimeout,
PermitWithoutStream: true,
}
if params.Time == 0 {
params.Time = DefaultKeepAliveTime
}
if params.Timeout == 0 {
params.Timeout = DefaultKeepAliveTimeout
}
return params, true
}
// DefaultEventBufferSize is the default Options.EventBufferSize.
const DefaultEventBufferSize = 1024
func (o Options) eventBufferSize() int {
if o.EventBufferSize <= 0 {
return DefaultEventBufferSize
}
return o.EventBufferSize
}
// BrowseChildrenOptions configures lazy Galaxy hierarchy walks performed by
// (*GalaxyClient).Browse and (*LazyBrowseNode).Expand. All fields are optional;
// the zero value matches the dashboard default (no filters, all attributes per
// the server default).
type BrowseChildrenOptions struct {
// CategoryIds restricts results to the listed Galaxy category ids when set.
CategoryIds []int32
// TemplateChainContains restricts results to objects whose template chain
// contains any of the listed template tag names.
TemplateChainContains []string
// TagNameGlob restricts results to objects whose tag name matches the glob
// pattern when non-empty.
TagNameGlob string
// IncludeAttributes overrides the server default for attribute inclusion when
// non-nil. The pointer form mirrors the proto's optional field.
IncludeAttributes *bool
// AlarmBearingOnly limits results to alarm-bearing objects when true.
AlarmBearingOnly bool
// HistorizedOnly limits results to historized objects when true.
HistorizedOnly bool
}
// RedactedAPIKey returns a display-safe representation of the configured API
// key for diagnostics and CLI output.
func (o Options) RedactedAPIKey() string {
return RedactAPIKey(o.APIKey)
}
// RedactAPIKey hides credential material while keeping enough shape for
// troubleshooting whether a key was supplied.
func RedactAPIKey(apiKey string) string {
if apiKey == "" {
return ""
}
if len(apiKey) <= 8 {
return "<redacted>"
}
prefix, suffix := apiKey[:4], apiKey[len(apiKey)-4:]
return prefix + strings.Repeat("*", len(apiKey)-8) + suffix
}