# README
Rabbit
rabbitmq connection pool based on amqp and turbocookedrabbit.
Usage
go get -u github.com/go-cinch/common/rabbit
import (
"context"
"fmt"
"github.com/go-cinch/common/rabbit"
"github.com/streadway/amqp"
"google.golang.org/protobuf/types/known/emptypb"
)
func main() {
// 1. init rabbit instance
rb := rabbit.New(
"amqp://guest:[email protected]:5672/",
rabbit.WithName("my app"),
)
err := rb.Error
if err != nil {
panic(err)
}
// 2.1. get exchange and declare if not exist
ex := rb.Exchange(
rabbit.WithExchangeName("exchange1"),
)
err = ex.Error
if err != nil {
panic(err)
}
// 2.2. get exchange without declare
ex = rb.Exchange(
rabbit.WithExchangeName("exchange1"),
rabbit.WithExchangeDeclare(false),
)
// 3.1. send json msg to exchange
err = ex.PublishJson("{}")
if err != nil {
panic(err)
}
// 3.2. send proto msg to exchange
var p emptypb.Empty
err = ex.PublishProto(&p)
if err != nil {
panic(err)
}
// 3.3. send byte msg to exchange
err = ex.PublishByte([]byte("ok"))
if err != nil {
panic(err)
}
// 4.1. get queue and declare if not exist
q1 := ex.Queue(
rabbit.WithQueueName("q1"),
)
err = q1.Error
if err != nil {
panic(err)
}
// 4.2. get queue without declare
q1 = ex.Queue(
rabbit.WithQueueName("q1"),
rabbit.WithQueueDeclare(false),
)
// 4.3. get queue without declare, and bind route keys
q1 = ex.Queue(
rabbit.WithQueueName("q1"),
rabbit.WithQueueDeclare(false),
rabbit.WithQueueBind(true),
rabbit.WithQueueRouteKeys("key1", "key2"),
)
// 4.4. declare queue q2 with dead letter queue dlx
ex.QueueWithDeadLetter(
rabbit.WithQueueName("q2"),
rabbit.WithQueueRouteKeys("key1"),
rabbit.WithQueueDeadLetterName("dlx"),
rabbit.WithQueueDeadLetterKey("key1.dead"),
rabbit.WithQueueMessageTTL(30000),
)
// 4.4. consume queue 50 pieces of data
err = q1.ConsumeOne(50, consumer)
if err != nil {
panic(err)
}
// 4.5 consume handle
err = q1.Consume(consumer)
if err != nil {
panic(err)
}
}
func consumer(ctx context.Context, q string, d amqp.Delivery) bool {
fmt.Println(ctx, q)
return true
}
Options
Rabbit
WithCtx
- contextWithName
- app nameWithHeartbeat
- pool heartbeat, default 1 secondWithTimeout
- connection timeout, default 10 secondWithMaxChannel
- max channel count, default 50WithMaxConnection
- max connection count, default 10WithHealthCheckInterval
- healthcheck interval, default 100 milli second
ExchangeOptions
WithExchangeName
- nameWithExchangeKind
- kind, default directWithExchangeDurable
- durable, default trueWithExchangeDeclare
- declare, default trueWithExchangeAutoDelete
- auto delete, default falseWithExchangeInternal
- internal exchange, do not accept publishings, default falseWithExchangeNoWait
- declare without waiting for a confirmation from the server, default falseWithExchangeNamePrefix
- exchange name prefixWithExchangeArgs
- other args
QueueOptions
WithQueueName
- nameWithQueueDurable
- durable, default trueWithQueueDeclare
- declare, default trueWithQueueBind
- bind route key, default trueWithQueueRouteKeys
- route key arrayWithQueueAutoDelete
- auto delete, default falseWithQueueExclusive
- the server will ensure that this is the sole consumer from this queue, default falseWithQueueNoWait
- declare without waiting for a confirmation from the server, default falseWithQueueArgs
- other argsWithQueueBindArgs
- other bind argsWithQueueDeadLetterName
- dead letter nameWithQueueDeadLetterKey
- dead letter route keyWithQueueMessageTTL
- msg expiration
PublishOptions
WithPublishCtx
- contextWithPublishMaxRetryCount
- max retry count, default 3WithPublishTimeout
- timeout, default 10000 milli secondWithPublishReconnectInterval
- reconnect interval, default 1000 milli secondWithPublishIdleInterval
- idle interval, default 1000 milli secondWithPublishRouteKey
- route keyWithPublishContentType
- content typeWithPublishHeaders
- headersWithPublishDeadLetter
- publish to dead letter queue, default falseWithPublishDeadLetterFirstQueue
- original queue name
ConsumeOptions
WithConsumeQosPrefetchCount
- qos, default 2WithConsumeConsumer
- consumer nameWithConsumeAutoAck
- auto confirm, default falseWithConsumeExclusive
- exclusive, default falseWithConsumeNoWait
- do not wait for the server to confirm the request and immediately begin deliveries, default falseWithConsumeNackRequeue
- requeue when delivery ack is false, default falseWithConsumeNackRetry
- retry when delivery ack is false, default falseWithConsumeNackMaxRetryCount
- max retry count when NackRetry is true, default 5WithConsumeAutoRequestId
- auth generate request idWithConsumeOneContext
- consume one ctxWithConsumeArgs
- other args
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author