Skip to content

The library does not allow for a timeout different from the default (30s) when connecting #26

Description

@dihedron

It is quaite easy to implement a configurable timeout when connecting to the server: instead of this, in func New(opts):

	// try all available URLs in a loop and quit as soon as it
	// can successfully establish a connection to one of them
	for _, url := range opts.URLs {
		if opts.UseTLS {
			tlsConfig := &tls.Config{}

			if opts.SkipVerifyTLS {
				tlsConfig.InsecureSkipVerify = true
			}

			ac, err = amqp.DialTLS(url, tlsConfig)
		} else {
			ac, err = amqp.Dial(url)
		}

		if err == nil {
			// yes, we made it!
			break
		}
	}

we might do something like this:

		config := amqp.Config{
			Dial: func(network, addr string) (net.Conn, error) {
				conn, err := net.DialTimeout(network, addr, opts.ConnectionTimeout)
				if err != nil {
					return nil, err
				}

				// Heartbeating hasn't started yet, don't stall forever on a dead server.
				// A deadline is set for TLS and AMQP handshaking. After AMQP is established,
				// the deadline is cleared in openComplete.
				if err := conn.SetDeadline(time.Now().Add(opts.ConnectionTimeout)); err != nil {
					return nil, err
				}

				return conn, nil
			},
		}

		if opts.UseTLS {
			config.TLSClientConfig = &tls.Config{}
			if opts.SkipVerifyTLS {
				config.TLSClientConfig.InsecureSkipVerify = true
			}
		}

		ac, err = amqp.DialConfig(url, config)

This change entains adding ConnectionTimeout time.Duration to the options, declaring a new DefaultConnectionTimeout of 30s, and handling the lack of a sensible value in the options inside func applyDefaults().

Please let me know if you're interested in a pull request; you can check it out in my fork, in the feature/replace-logger-with-slog branch (where I'm also playing with replacing the passed in logger with log/slog - to be proposed in a separate issue).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions