diff --git a/lnsq/nsq.go b/lnsq/nsq.go index 3fd7a80..5c43d6f 100644 --- a/lnsq/nsq.go +++ b/lnsq/nsq.go @@ -27,7 +27,18 @@ func ConnectToNSQ(topic, channel string, handler nsq.Handler) (*nsq.Consumer, er return nil, fmt.Errorf("failed to connect to NSQD: %w", err) } - return consumer, nil + timeout := 10 * time.Second + deadline := time.Now().Add(timeout) + for time.Now().Before(deadline) { + stats := consumer.Stats() + if stats.Connections > 0 { + return consumer, nil + } + time.Sleep(100 * time.Millisecond) + } + + consumer.Stop() + return nil, fmt.Errorf("timeout waiting for NSQ connection after %v", timeout) } func NewProducer() (*nsq.Producer, error) {