优化结构
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// Package mq exposes the broker-agnostic messaging seam used by services.
|
||||
package mq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrUnavailable = errors.New("message broker unavailable")
|
||||
|
||||
const (
|
||||
AtMostOnce byte = 0
|
||||
AtLeastOnce byte = 1
|
||||
ExactlyOnce byte = 2
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Topic string
|
||||
Payload []byte
|
||||
QoS byte
|
||||
Retain bool
|
||||
}
|
||||
|
||||
func (m Message) DecodeJSON(target any) error { return json.Unmarshal(m.Payload, target) }
|
||||
|
||||
type Handler func(context.Context, Message)
|
||||
|
||||
type Client interface {
|
||||
Publish(context.Context, string, []byte, byte, bool) error
|
||||
Subscribe(context.Context, string, byte, Handler) error
|
||||
Unsubscribe(context.Context, ...string) error
|
||||
Connected() bool
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Enabled bool
|
||||
Broker string
|
||||
ClientID string
|
||||
Username string
|
||||
Password string
|
||||
KeepAlive time.Duration
|
||||
CleanSession bool
|
||||
ConnectTimeout time.Duration
|
||||
}
|
||||
|
||||
func PublishJSON(ctx context.Context, client Client, topic string, value any, qos byte, retain bool) error {
|
||||
if client == nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
payload, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.Publish(ctx, topic, payload, qos, retain)
|
||||
}
|
||||
Reference in New Issue
Block a user