go-nsq Documentation

repository·master·Indexed 25 days ago

https://github.com/nsqio/go-nsq

The official Go client library for NSQ, a distributed real-time messaging platform. go-nsq provides the necessary implementations for Go developers to build producers and consumers that integrate with NSQ nodes, including support for message publishing, subscription management, and connection handling.

Tokens
6.2K
Snippets
6
Records
60
Agent score
83%

What's inside go-nsq

  1. Overview of go-nsq

    master
    go-nsq is the official Go package for interacting with [NSQ][nsq]. It provides the necessary client implementations to build applications that produce or consume messages within the NSQ ecosystem.
  2. Initialize NSQ configuration with NewConfig

    master
    To create a new NSQ configuration, you must use the NewConfig() function. Using a struct literal for Config will cause a panic. Once a Config object is passed into a high-level type (like a Consumer or Producer), its values are copied and are no longer mutable. You can modify the configuration before passing it to these types by setting fields directly or using the Set() method.
  3. Configure TLS settings

    master

    You can configure TLS for your NSQ client using specific tls_* options via the Set() method. These options modify the underlying TlsConfig field.

    Available TLS Options:

    • tls_v1: Boolean to enable TLS negotiation.
    • tls_root_ca_file: String path to a file containing the root CA.
    • tls_insecure_skip_verify: Boolean indicating whether to verify server certificates.
    • tls_cert: String path to the public key certificate file.
    • tls_key: String path to the private key file.
    • tls_min_version: String indicating the minimum TLS version: 'ssl3.0', 'tls1.0', 'tls1.1', or 'tls1.2'.
  4. Configure logging for a Conn instance

    master

    You can assign a custom logger to a Conn instance using SetLogger. The logger must implement the logger interface, which requires an Output(calldepth int, s string) method (the standard library log.Logger satisfies this).

    You can also set the logging level or specify a custom format string. The format string should be a printf-compatible string with a single %s argument used for the connection address.

  5. Serialize and Deserialize Messages

    master

    Messages can be serialized to an io.Writer or deserialized from a byte slice using the following methods:

    • WriteTo(w io.Writer): Serializes the message (Timestamp, Attempts, ID, and Body) into the provided writer. It is recommended to use a buffered writer to minimize system calls.
    • DecodeMessage(b []byte): Deserializes a byte slice into a *Message. The expected wire format is: 8-byte nanosecond timestamp, 2-byte attempts, 16-byte ASCII hex encoded ID, and the N-byte body.
  6. Add concurrent message handlers

    master

    To increase processing throughput, use AddConcurrentHandlers to spawn multiple goroutines for message handling. The concurrency parameter determines the number of goroutines.

    Warning: This method panics if called after the consumer has already connected to an nsqd or nsqlookupd instance.

  7. Handle connection lifecycle and state

    master

    The Conn type provides methods to monitor the connection state and RDY counts:

    • Close(): Idempotently initiates a graceful connection close.
    • IsClosing(): Returns true if the connection is currently in the process of closing.
    • RDY(): Returns the current RDY count.
    • MaxRDY(): Returns the maximum RDY count negotiated with nsqd.
    • LastRDY(): Returns the previously set RDY count.
    • LastRdyTime(): Returns the time of the last non-zero RDY update.
    • LastMessageTime(): Returns the time the last message was received.
    • RemoteAddr(): Returns the destination nsqd address.
  8. Adjust Max In-Flight messages dynamically

    master

    Use ChangeMaxInFlight(maxInFlight int) to update the maximum number of messages the consumer instance is allowed to have in-flight across all connections.

    Setting ChangeMaxInFlight(0) will effectively pause the flow of messages to the consumer.

  9. Publish multiple messages asynchronously with Producer.MultiPublishAsync

    master
    Use MultiPublishAsync to send a slice of message bodies without waiting for the nsqd response. Similar to PublishAsync, you can provide a doneChan to receive a *ProducerTransaction once the operation completes.
  10. Publish messages synchronously with Producer.Publish

    master
    Use Publish to synchronously send a single message body to a specific topic. This method blocks until the message is successfully published or an error is returned.
  11. Configure Producer logging

    master

    The Producer allows custom logging via the SetLogger, SetLoggerForLevel, and SetLoggerLevel methods. The logger must implement the following interface:

    type logger interface {
    	Output(calldepth int, s string)
    }
    • SetLogger(l logger, lvl LogLevel): Sets the same logger for all log levels.
    • SetLoggerForLevel(l logger, lvl LogLevel): Sets a specific logger for a specific LogLevel.
    • SetLoggerLevel(lvl LogLevel): Sets the global package logging level.