forked from mtusi2026/mtusi2026lab5
feat(rabbitmq): add durable messaging client and broker config
This commit is contained in:
@@ -12,6 +12,7 @@ require (
|
|||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/jackc/pgx/v5 v5.11.0
|
github.com/jackc/pgx/v5 v5.11.0
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0
|
||||||
github.com/redis/go-redis/v9 v9.23.0
|
github.com/redis/go-redis/v9 v9.23.0
|
||||||
golang.org/x/crypto v0.58.0
|
golang.org/x/crypto v0.58.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA
|
|||||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
@@ -69,6 +71,8 @@ github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
|||||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||||
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type Config struct {
|
|||||||
Logging LoggingConfig
|
Logging LoggingConfig
|
||||||
Database DatabaseConfig
|
Database DatabaseConfig
|
||||||
Redis RedisConfig
|
Redis RedisConfig
|
||||||
|
Broker BrokerConfig
|
||||||
S3 S3Config
|
S3 S3Config
|
||||||
SessionStore string
|
SessionStore string
|
||||||
}
|
}
|
||||||
@@ -57,6 +58,14 @@ func (c RedisConfig) URL() (string, error) {
|
|||||||
return readSecretFile(c.URLFile)
|
return readSecretFile(c.URLFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BrokerConfig struct {
|
||||||
|
URLFile string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c BrokerConfig) URL() (string, error) {
|
||||||
|
return readSecretFile(c.URLFile)
|
||||||
|
}
|
||||||
|
|
||||||
type S3Config struct {
|
type S3Config struct {
|
||||||
Endpoint string
|
Endpoint string
|
||||||
Region string
|
Region string
|
||||||
@@ -108,6 +117,10 @@ func Load() Config {
|
|||||||
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
|
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Broker: BrokerConfig{
|
||||||
|
URLFile: getEnv("BROKER_URL_FILE", "/run/secrets/broker_url"),
|
||||||
|
},
|
||||||
|
|
||||||
S3: LoadS3(),
|
S3: LoadS3(),
|
||||||
|
|
||||||
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
|
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
const QueueName = "attachments.process.v1"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrConnect = errors.New("connect to rabbitmq")
|
||||||
|
ErrNotRouted = errors.New("rabbitmq message not routed")
|
||||||
|
ErrNack = errors.New("rabbitmq publish rejected")
|
||||||
|
ErrPrefetch = errors.New("rabbitmq prefetch must be positive")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
conn *amqp.Connection
|
||||||
|
channel *amqp.Channel
|
||||||
|
returns <-chan amqp.Return
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(url string) (*Client, error) {
|
||||||
|
conn, err := amqp.DialConfig(url, amqp.Config{
|
||||||
|
Dial: amqp.DefaultDial(5 * time.Second),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrConnect
|
||||||
|
}
|
||||||
|
|
||||||
|
channel, err := conn.Channel()
|
||||||
|
if err != nil {
|
||||||
|
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := channel.QueueDeclare(QueueName, true, false, false, false, nil); err != nil {
|
||||||
|
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := channel.Confirm(false); err != nil {
|
||||||
|
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Client{
|
||||||
|
conn: conn,
|
||||||
|
channel: channel,
|
||||||
|
returns: channel.NotifyReturn(make(chan amqp.Return, 1)),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Publish(ctx context.Context, messageID string, body []byte) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
stop := context.AfterFunc(ctx, func() {
|
||||||
|
_ = c.conn.CloseDeadline(time.Now())
|
||||||
|
})
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
confirmation, err := c.channel.PublishWithDeferredConfirm("", QueueName, true, false, amqp.Publishing{
|
||||||
|
ContentType: "application/json",
|
||||||
|
DeliveryMode: amqp.Persistent,
|
||||||
|
MessageId: messageID,
|
||||||
|
Body: body,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ack, err := confirmation.WaitContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ack {
|
||||||
|
return ErrNack
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case _, ok := <-c.returns:
|
||||||
|
if !ok {
|
||||||
|
return amqp.ErrClosed
|
||||||
|
}
|
||||||
|
return ErrNotRouted
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Consume(ctx context.Context, prefetch int) (<-chan amqp.Delivery, error) {
|
||||||
|
if prefetch < 1 {
|
||||||
|
return nil, ErrPrefetch
|
||||||
|
}
|
||||||
|
if err := c.channel.Qos(prefetch, 0, false); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c.channel.ConsumeWithContext(ctx, QueueName, "", false, false, false, false, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
return c.conn.CloseDeadline(time.Now().Add(time.Second))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user