Update mengyastore

This commit is contained in:
2026-05-13 12:11:46 +08:00
parent ad54b1eb7e
commit 37983f3b60
156 changed files with 10064 additions and 5681 deletions

View File

@@ -15,7 +15,7 @@ import (
"mengyastore-backend/internal/storage"
)
// Client manages a single AMQP connection, a publish channel, and naming for one env.
// Client 封装单条 AMQP 连接、发布用 Channel以及单环境的命名。
type Client struct {
conn *amqp.Connection
pubCh *amqp.Channel
@@ -32,7 +32,7 @@ type Client struct {
consumerSite *storage.SiteStore
}
// New connects to RabbitMQ and declares exchange + queue + binding (idempotent).
// New 连接 RabbitMQ 并声明交换机 + 队列 + 绑定(幂等)。
func New(amqpURL, env string) (*Client, error) {
if amqpURL == "" {
return nil, fmt.Errorf("empty amqp url")
@@ -141,7 +141,7 @@ func (c *Client) passiveQueueLocked() error {
return err
}
// PublishOrderEmail publishes a persistent JSON message to the order-email routing key.
// PublishOrderEmail 向 order-email 路由键发布一条持久化 JSON 消息。
func (c *Client) PublishOrderEmail(ctx context.Context, p OrderEmailPayload) error {
body, err := json.Marshal(p)
if err != nil {
@@ -190,7 +190,7 @@ func (c *Client) PublishOrderEmail(ctx context.Context, p OrderEmailPayload) err
return nil
}
// QueueInspectInfo returns current queue depth and consumer count (passive declare).
// QueueInspectInfo 返回当前队列深度与消费者数(被动声明查询)。
func (c *Client) QueueInspectInfo() (messages int, consumers int, err error) {
c.mu.Lock()
defer c.mu.Unlock()
@@ -216,7 +216,7 @@ func (c *Client) QueueInspectInfo() (messages int, consumers int, err error) {
return msgs, cons, err
}
// Ping checks that the publish channel can query the declared queue (liveness for /api/health).
// Ping 检查发布 Channel 能否对声明的队列做被动查询(供 /api/health 探活)。
func (c *Client) Ping() error {
c.mu.Lock()
defer c.mu.Unlock()
@@ -231,10 +231,10 @@ func (c *Client) Ping() error {
return err
}
// Env returns the sanitized environment suffix used in exchange/queue names.
// Env 返回交换机/队列名使用的环境后缀(已规范化)。
func (c *Client) Env() string { return c.env }
// StartConsumer runs the order-email consumer until ctx is cancelled (run in a goroutine).
// StartConsumer 在 ctx 取消前运行订单邮件消费者(通常在 goroutine 中调用)。
func (c *Client) StartConsumer(ctx context.Context, site *storage.SiteStore) {
c.mu.Lock()
c.consumerCtx = ctx
@@ -248,7 +248,7 @@ func (c *Client) StartConsumer(ctx context.Context, site *storage.SiteStore) {
RunOrderEmailConsumer(ctx, conn, env, site)
}
// Close releases the publish channel and connection.
// Close 关闭发布 Channel 与连接。
func (c *Client) Close() {
c.closing.Do(func() {
c.mu.Lock()

View File

@@ -11,7 +11,7 @@ import (
"mengyastore-backend/internal/storage"
)
// RunOrderEmailConsumer runs until ctx is done. Must be started in its own goroutine.
// RunOrderEmailConsumer 运行至 ctx 结束,请在独立 goroutine 中启动。
func RunOrderEmailConsumer(ctx context.Context, conn *amqp.Connection, env string, site *storage.SiteStore) {
if conn == nil || site == nil {
return

View File

@@ -2,7 +2,7 @@ package mq
import "mengyastore-backend/internal/email"
// OrderEmailPayload is the JSON body published to RabbitMQ (no SMTP secrets).
// OrderEmailPayload 发布到 RabbitMQ 的订单邮件 JSON 负载(不含 SMTP 口令)。
type OrderEmailPayload struct {
ToEmail string `json:"toEmail"`
ToName string `json:"toName"`

View File

@@ -9,15 +9,15 @@ import (
const routingKeyOrderEmail = "order.email.notify"
// OrderEmailRoutingKey is the binding / publish key for order notification messages.
// OrderEmailRoutingKey 订单通知消息的绑定键 / 发布 routing key。
func OrderEmailRoutingKey() string { return routingKeyOrderEmail }
// ExchangeName returns the durable topic exchange for this app + env (isolation from other apps on same broker).
// ExchangeName 返回本应用 + 环境对应的持久化 topic 交换机名(与 Broker 上其它应用隔离)。
func ExchangeName(env string) string {
return fmt.Sprintf("ex.mengyastore.%s.events", sanitizeEnv(env))
}
// QueueName returns the durable order-email queue name for this env.
// QueueName 返回本环境下持久化的订单邮件队列名。
func QueueName(env string) string {
return fmt.Sprintf("q.mengyastore.%s.order_email", sanitizeEnv(env))
}