zmq4 icon indicating copy to clipboard operation
zmq4 copied to clipboard

Router/Req autoreconnect not taking effect?

Open freitzzz opened this issue 4 months ago • 0 comments

Hey

I'm building a pub/sub server (many-to-one), in which pub clients use REQ sockets and the sub server uses a ROUTER socket, similar to what's happening in this pic:

image

The thing is, sometimes the sub server loses connection in the network and all the pub clients log this error:

zmq4: no connections available)

To make my system fault resilient, I'm looking into including an auto-reconnect feat to pub clients. I notice the library also includes that option, but even after enabling it, pub clients still log the same message. Is it expected? Does auto-reconnect only work in certain architectures like REQ/REP?


Some of my source-code:

// Wraps [zmq4.Socket] so context can be accessed for extracting dependencies.
type Socket struct {
	zmq4.Socket
	ctx context.Context
}


// Creates a new sub [zmq4-Socket] wrapper with a custom context.
// The context must contain all the dependencies required by the socket.
func NewSubSocket(ctx context.Context) Socket {
	return Socket{
		Socket: zmq4.NewRouter(ctx),
		ctx:    ctx,
	}
}

// Creates a new sub [zmq4-Socket] wrapper with a custom context.
// The context must contain all the dependencies required by the socket.
func NewPubSocket(ctx context.Context) Socket {
	return Socket{
		Socket: zmq4.NewReq(ctx, zmq4.WithAutomaticReconnect(true)),
		ctx:    ctx,
	}
}


func (s Socket) PublishAndForget(m msg) {
	logging.LogInfo("publishing msg with topic: %d", m.Topic)

	b, err := encode(m)
	if err != nil {
		log.Printf("failed to encode message, %v\n", err)
	}

	err = s.Send(zmq4.NewMsg(b))
	if err != nil {
		log.Printf("failed to publish message, %v\n", err)
	}
}

freitzzz avatar Oct 17 '24 09:10 freitzzz