Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn off Ack and Nagle? #1135

Open
ORESoftware opened this issue Feb 17, 2024 · 2 comments
Open

turn off Ack and Nagle? #1135

ORESoftware opened this issue Feb 17, 2024 · 2 comments

Comments

@ORESoftware
Copy link

ORESoftware commented Feb 17, 2024

Description

Is there a way to turn off Ack or Nagle (TCP_NODELAY) for TCP connections?
https://chat.openai.com/share/dba5c438-7bcd-41ed-8d14-f7c61199e531

I guess it would affect both sides of transmission? or can it be one-way?

@amills-vibeirl
Copy link

Here is how you do it with redis:

	opt := &redis.Options{
		Addr: "localhost:6379",
		Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
			conn, err := net.DialTimeout(network, addr, 5*time.Second)
			if err != nil {
				return nil, err
			}
			tcpConn, ok := conn.(*net.TCPConn)
			if !ok {
				return nil, err // Optionally, replace with a more specific error
			}
			if err := tcpConn.SetNoDelay(true); err != nil {
				return nil, err
			}
			return tcpConn, nil
		},
	}

and with RabbitMQ:

// CustomDialer connects to RabbitMQ using a custom net.Dialer to disable Nagle's algorithm.
func CustomDialer(pool *RabbitMQConnectionPool) (*amqp.Connection, error) {
	//// custom dialer

	dialer := &net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
	}

	conn, err := amqp.DialConfig(pool.connString, amqp.Config{
		Dial: func(network, addr string) (net.Conn, error) {
			c, err := dialer.Dial(network, addr)
			if err != nil {
				return nil, err
			}

			// Assuming c is a *net.TCPConn, disable Nagle's algorithm.
			if tcpConn, ok := c.(*net.TCPConn); ok {
				if err := tcpConn.SetNoDelay(true); err != nil {
					vbl.Stdout.WarnF("Failed to disable Nagle's algorithm: %v", err)
					// Handle error or proceed, depending on your error handling strategy.
				}
			}

			return c, nil
		},
	})
	return conn, err
}

I am looking for a way to do this with Kafka as well

@pascal-hofmann
Copy link

You either have to disable delayed ACK on the receiving side, or disable the nagle algorithm on the sending side.

For confluent-kafka-go you have to set socket.nagle.disable to true in the config passed to NewProducer to disable the nagle algorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants