mqconfig

package
v0.0.0-...-b1757e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: MIT Imports: 4 Imported by: 0

README

Integrate RabbitMQ with API handlers

RabbitMQ Client structure

type MQClient struct {
	Connection     *amqp.Connection
	Channel        *amqp.Channel
	resultChannels map[string]chan interface{}
	lock           sync.Mutex
}

  • resultChannels can be used to store the results. For example, after a new blog is created, we can put it into resultChannels, the api handler can retrieve the blog from the corresponding channels. Each queue should have a separate channel

  • mutex lock is used since multiple threads can try to update the channels at the same time

workflow of rabbitmq with create blog api

  • in the init() function, all the queues should be declared

  • in the api handler function, create the result channel and encode the request. call the publish function to publish the message, pass in the channel and message. at last, try to retrieve the created blog from the channel

  • publish function will handle the logic of save the channel into the client

  • in the consumer function, consume the message and create the blog and add the blog to the channel with go routine

Documentation

Index

Constants

View Source
const (
	DELETE_QUEUE      = "delete_queue"
	UPDATE_QUEUE      = "update_queue"
	CREATE_BLOG_QUEUE = "create_blog_queue"
	AppName           = "mq"
)

Variables

This section is empty.

Functions

func LoadMQConfigFromENV

func LoadMQConfigFromENV() error

Types

type MQConfig

type MQConfig struct {
	RabbitMQ *RabbitMQ `json:"rabbitmq" toml:"rabbitmq"`
}

func ConfigFromENV

func ConfigFromENV() *MQConfig

func DefaultMQConfig

func DefaultMQConfig() *MQConfig

type RabbitMQ

type RabbitMQ struct {
	Host     string `json:"host" toml:"host" env:"RABBITMQ_HOST"`
	Port     int    `json:"port" toml:"port" env:"RABBITMQ_PORT"`
	User     string `json:"user" toml:"user" env:"RABBITMQ_USER"`
	Password string `json:"password" toml:"password" env:"RABBITMQ_PASSWORD"`
}

func (*RabbitMQ) GetAddress

func (mq *RabbitMQ) GetAddress() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL