feat: use postgres
This commit is contained in:
@@ -4,7 +4,6 @@ package lifecycleevents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -17,23 +16,10 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Config configures one Redis-backed user-lifecycle publisher.
|
||||
// Config configures one Redis-backed user-lifecycle publisher. The
|
||||
// connection is supplied externally by the runtime so multiple publishers
|
||||
// can share one *redis.Client.
|
||||
type Config struct {
|
||||
// Addr is the Redis network address in host:port form.
|
||||
Addr string
|
||||
|
||||
// Username is the optional Redis ACL username.
|
||||
Username string
|
||||
|
||||
// Password is the optional Redis ACL password.
|
||||
Password string
|
||||
|
||||
// DB is the Redis logical database index.
|
||||
DB int
|
||||
|
||||
// TLSEnabled enables TLS with a conservative minimum protocol version.
|
||||
TLSEnabled bool
|
||||
|
||||
// Stream identifies the Redis Stream key used for lifecycle events. The
|
||||
// default platform key is `user:lifecycle_events`.
|
||||
Stream string
|
||||
@@ -55,13 +41,13 @@ type Publisher struct {
|
||||
operationTimeout time.Duration
|
||||
}
|
||||
|
||||
// New constructs a Redis-backed lifecycle-event publisher from cfg.
|
||||
func New(cfg Config) (*Publisher, error) {
|
||||
// New constructs a Redis-backed lifecycle-event publisher backed by the
|
||||
// supplied client. The publisher does not own the client; the runtime is
|
||||
// responsible for closing it.
|
||||
func New(client *redis.Client, cfg Config) (*Publisher, error) {
|
||||
switch {
|
||||
case strings.TrimSpace(cfg.Addr) == "":
|
||||
return nil, errors.New("new redis lifecycle-event publisher: redis addr must not be empty")
|
||||
case cfg.DB < 0:
|
||||
return nil, errors.New("new redis lifecycle-event publisher: redis db must not be negative")
|
||||
case client == nil:
|
||||
return nil, errors.New("new redis lifecycle-event publisher: redis client must not be nil")
|
||||
case strings.TrimSpace(cfg.Stream) == "":
|
||||
return nil, errors.New("new redis lifecycle-event publisher: stream must not be empty")
|
||||
case cfg.StreamMaxLen <= 0:
|
||||
@@ -70,33 +56,17 @@ func New(cfg Config) (*Publisher, error) {
|
||||
return nil, errors.New("new redis lifecycle-event publisher: operation timeout must be positive")
|
||||
}
|
||||
|
||||
options := &redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
Protocol: 2,
|
||||
DisableIdentity: true,
|
||||
}
|
||||
if cfg.TLSEnabled {
|
||||
options.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
|
||||
}
|
||||
|
||||
return &Publisher{
|
||||
client: redis.NewClient(options),
|
||||
client: client,
|
||||
stream: cfg.Stream,
|
||||
streamMaxLen: cfg.StreamMaxLen,
|
||||
operationTimeout: cfg.OperationTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Close releases the underlying Redis client resources.
|
||||
// Close is a no-op: the client is owned by the runtime.
|
||||
func (publisher *Publisher) Close() error {
|
||||
if publisher == nil || publisher.client == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return publisher.client.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ping verifies that the configured Redis backend is reachable within the
|
||||
|
||||
Reference in New Issue
Block a user