Skip to content

Commit

Permalink
Minor connection name fix (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
m3talux authored Nov 20, 2024
1 parent d276634 commit f0867a2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.1

- Minor fix for correct usage of the `ConnectionName` parameter, and the possibility to declare it via environment variables ([PR](https://github.com/KardinalAI/gorabbit/pull/18)).

# 1.1.0

- Allow setting a connection name ([PR](https://github.com/KardinalAI/gorabbit/pull/8))
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ parameter.
| Password | The plain authentication password | guest |
| Vhost | The specific vhost to use when connection to CloudAMQP | |
| UseTLS | The flag that activates the use of TLS (amqps) | false |
| ConnectionName | The desired connection name | Gorabbit |
| KeepAlive | The flag that activates retry and re-connect mechanisms | true |
| RetryDelay | The delay between each retry and re-connection | 3 seconds |
| MaxRetry | The max number of message retry if it failed to process | 5 |
Expand Down Expand Up @@ -142,6 +143,7 @@ Here are the following supported environment variables:
* `RABBITMQ_PASSWORD`: Defines the password,
* `RABBITMQ_VHOST`: Defines the vhost,
* `RABBITMQ_USE_TLS`: Defines whether to use TLS or no.
* `RABBITMQ_CONNECTION_NAME`: Defines the desired connection name.

**Note that environment variables are all optional, so missing keys will be replaced by their corresponding default.**

Expand Down
1 change: 1 addition & 0 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func NewClientOptionsFromEnv() *ClientOptions {
}

defaultOpts.UseTLS = fromEnv.UseTLS
defaultOpts.ConnectionName = fromEnv.ConnectionName

return defaultOpts
}
Expand Down
6 changes: 6 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package gorabbit

import (
"context"
"fmt"
"net/url"
"time"

"github.com/google/uuid"
amqp "github.com/rabbitmq/amqp091-go"
)

Expand Down Expand Up @@ -70,6 +72,8 @@ func newConsumerConnection(
logger logger,
marshaller Marshaller,
) *amqpConnection {
connectionName = fmt.Sprintf("%s-consumer-%s", connectionName, uuid.NewString())

return newConnection(ctx, uri, connectionName, keepAlive, retryDelay, logger, connectionTypeConsumer, marshaller)
}

Expand All @@ -96,6 +100,8 @@ func newPublishingConnection(
logger logger,
marshaller Marshaller,
) *amqpConnection {
connectionName = fmt.Sprintf("%s-publisher-%s", connectionName, uuid.NewString())

conn := newConnection(ctx, uri, connectionName, keepAlive, retryDelay, logger, connectionTypePublisher, marshaller)

conn.maxRetry = maxRetry
Expand Down
4 changes: 4 additions & 0 deletions connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func newConnectionManager(
logger logger,
marshaller Marshaller,
) *connectionManager {
if connectionName == "" {
connectionName = libraryName
}

c := &connectionManager{
consumerConnection: newConsumerConnection(
ctx, uri, connectionName, keepAlive, retryDelay, logger, marshaller,
Expand Down
13 changes: 7 additions & 6 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ func (m mqttPublishing) HashCode() string {
}

type RabbitMQEnvs struct {
Host string `env:"RABBITMQ_HOST"`
Port uint `env:"RABBITMQ_PORT"`
Username string `env:"RABBITMQ_USERNAME"`
Password string `env:"RABBITMQ_PASSWORD"`
Vhost string `env:"RABBITMQ_VHOST"`
UseTLS bool `env:"RABBITMQ_USE_TLS"`
Host string `env:"RABBITMQ_HOST"`
Port uint `env:"RABBITMQ_PORT"`
Username string `env:"RABBITMQ_USERNAME"`
Password string `env:"RABBITMQ_PASSWORD"`
Vhost string `env:"RABBITMQ_VHOST"`
UseTLS bool `env:"RABBITMQ_USE_TLS"`
ConnectionName string `env:"RABBITMQ_CONNECTION_NAME"`
}

0 comments on commit f0867a2

Please sign in to comment.