options.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package grpcclient
  2. import (
  3. "time"
  4. "google.golang.org/grpc/keepalive"
  5. )
  6. // Option configures a Client.
  7. type Option func(*Client)
  8. // WithAPIKey sets the API key used for every StreamAlerts stream.
  9. // The key is sent as "Bearer <key>" in the grpc-metadata
  10. // "authorization" header on each stream.
  11. //
  12. // The format is "<company_id>:<source_id>:<secret>".
  13. func WithAPIKey(k string) Option {
  14. return func(c *Client) { c.apiKey = k }
  15. }
  16. // WithMaxRetries sets the maximum number of retry attempts for transient
  17. // errors (RATE_LIMITED, UNAVAILABLE). Zero disables retries.
  18. // The backoff between retries follows Error.retry_after_ms from the server
  19. // when available, otherwise exponential backoff starting at 100ms.
  20. func WithMaxRetries(n int) Option {
  21. return func(c *Client) { c.maxRetries = n }
  22. }
  23. // WithKeepalive configures the gRPC keepalive parameters on the underlying
  24. // connection. Time is the interval between keepalive probes; Timeout is how
  25. // long the peer has to respond before the connection is closed.
  26. func WithKeepalive(time, timeout time.Duration) Option {
  27. return func(c *Client) {
  28. c.keepalive = keepalive.ClientParameters{
  29. Time: time,
  30. Timeout: timeout,
  31. PermitWithoutStream: false,
  32. }
  33. }
  34. }
  35. // WithInsecure disables transport security. Appropriate for development
  36. // only (e.g. connecting to ingestd on localhost). In production, connections
  37. // to ingestd should use mTLS or at minimum TLS, configured via a
  38. // grpc.DialOption built with credentials.NewTLS.
  39. func WithInsecure() Option {
  40. return func(c *Client) { c.insecure = true }
  41. }