mq

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 3 Imported by: 2

README

MQ - lightweight message queue

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/mq

Getting Started

Consumer
import (
  "github.com/go-zoox/mq"
)

func main(t *testing.T) {
	m := mq.New(&mq.Config{
		RedisHost:     <RedisHost>,
		RedisPort:     <RedisPort>,
		RedisUsername: <RedisUsername>,
		RedisPassword: <RedisPassword>,
		RedisDB:       <RedisDB>,
	})

	m.Consume(context.TODO(), "default", func(msg *mq.Message) error {
		logger.Infof("received message: %s", string(msg.Body))
		return nil
	})
}
Producer
import (
  "github.com/go-zoox/mq"
)

func main(t *testing.T) {
	m := mq.New(&mq.Config{
		RedisHost:     <RedisHost>,
		RedisPort:     <RedisPort>,
		RedisUsername: <RedisUsername>,
		RedisPassword: <RedisPassword>,
		RedisDB:       <RedisDB>,
	})

	m.Send(context.TODO(), &mq.Message{
		Topic: "default",
		Body:  []byte("hello world"),
	})
}

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.0.1"

Version is the current version of the package.

Functions

This section is empty.

Types

type Config

type Config struct {
	RedisHost     string
	RedisPort     int
	RedisUsername string
	RedisPassword string
	RedisDB       int

	//
	Redis *redis.Client

	// MaxLen 最大消息数量,如果大于这个数量,旧消息会被删除,0表示不管
	MaxLen int64
	// Approx 配合 MaxLen 使用的,表示几乎精确的删除消息,也就是不完全精确,由于stream内部是流,所以设置此参数xadd会更加高效
	Approx bool
}

Config is the config for a mq.

type Handler

type Handler func(msg *Message) error

Handler is a mq handler.

type MQ

type MQ interface {
	Send(ctx context.Context, msg *Message) error
	Consume(ctx context.Context, topic, group, consumer, start string, batchSize int, h Handler) error
}

MQ is a simple message queue.

func New

func New(cfg *Config) MQ

New creates a new mq.

type Message added in v1.0.1

type Message struct {
	// ID 消息ID
	ID string

	// Topic 主题
	Topic string

	// Body 消息体
	Body []byte

	// Group 消费者组
	Group string

	// 消费者组里的消费者
	// 同一组里的消费者共享消息,也就是说同一组里的消费者不会重复消费消息
	// 不同组里的消费者会重复消费消息
	Consumer string
}

Message ...

Directories

Path Synopsis
cmd
mq

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳