0

I've created an NSQ producer in my Go microservice and I'm trying to create a method for it to attempt to reconnect after an NSQ restart.

I've looked into the nsq config documentation hoping to see if I could provide a failover method on restart/quit but I haven't had much luck. I'll post how I'm creating my NSQ producer:

type ProducerNSQ struct {
    p *nsq.Producer
}

func initConnectionNSQ() (*ProducerNSQ, error) {
    config := nsq.NewConfig()
    config.UserAgent = common.DeployedService()
    config.BackoffMultiplier = time.Duration(time.Second * 10)

    producer, err := nsq.NewProducer(dataConfig.NsqAddress, config)
    if err != nil {
        return nil, err
    }
    if err := producer.Ping(); err != nil {
        return nil, err
    }
    return &ProducerNSQ{p: producer}, nil
}

All help is much appreciated!

Jamie Belcher
  • 123
  • 2
  • 13

1 Answers1

0

create the producer in a goroutine and have a channel to send message to it for publishing. in the goroutine have an endless loop, receive messages from the channel and try to send to NSQ, if it fails re-connect and retry.

Gal Ben-Haim
  • 17,433
  • 22
  • 78
  • 131
  • But if the nsqd sends a close/error, the producer dies throwing `IO error - EOF` `beginning close` which is what I'd like to catch and gracefully handle. Will this only exit the current goroutine? Meaning additional calls will be unaffected? – Jamie Belcher Jan 17 '20 at 13:37