NewRegistry connects to the registry DB and starts a background poller.
(ctx stdctx.Context, databaseURL string, pollInterval time.Duration, log *slog.Logger)
| 47 | |
| 48 | // NewRegistry connects to the registry DB and starts a background poller. |
| 49 | func NewRegistry(ctx stdctx.Context, databaseURL string, pollInterval time.Duration, log *slog.Logger) (*Registry, error) { |
| 50 | if databaseURL == "" { |
| 51 | return nil, nil |
| 52 | } |
| 53 | cfg, err := pgxpool.ParseConfig(databaseURL) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | pool, err := pgxpool.NewWithConfig(ctx, cfg) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | if err := pool.Ping(ctx); err != nil { |
| 62 | pool.Close() |
| 63 | return nil, err |
| 64 | } |
| 65 | r := &Registry{ |
| 66 | pool: pool, |
| 67 | byHost: make(map[string]*Entry), |
| 68 | pollDur: pollInterval, |
| 69 | stop: make(chan struct{}), |
| 70 | stopped: make(chan struct{}), |
| 71 | log: log, |
| 72 | } |
| 73 | if err := r.refresh(ctx); err != nil { |
| 74 | pool.Close() |
| 75 | return nil, err |
| 76 | } |
| 77 | go r.poll() |
| 78 | return r, nil |
| 79 | } |
| 80 | |
| 81 | // Close stops the poller and closes the pool. |
| 82 | func (r *Registry) Close() { |