https://github.com/nqd/mq
A Golang message queue that does struct encoding and matches topics like RabbitMQ.
https://github.com/nqd/mq
golang matching message queue topic
Last synced: about 2 months ago
JSON representation
A Golang message queue that does struct encoding and matches topics like RabbitMQ.
- Host: GitHub
- URL: https://github.com/nqd/mq
- Owner: nqd
- License: mit
- Created: 2019-06-10T15:06:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-08T14:46:27.000Z (over 6 years ago)
- Last Synced: 2024-06-19T16:47:12.780Z (over 1 year ago)
- Topics: golang, matching, message, queue, topic
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MQ [](http://travis-ci.org/nqd/mq)
A Golang message queue that does struct encoding and matches topics like RabbitMQ.
## Installation
## Basic Usage
```{go}
import "github.com/nqd/mq"
m := mq.NewMQ()
// Async subscription
sub, err := m.Subscribe("foo", func(m []byte) {
fmt.Printf("Received a message %s\n", string(m))
})
// ...
err := m.Publish("foo", []byte("hello world"))
// MQ with a Golang struct
type todo struct {
Title string
Finish bool
}
sub, err := m.Subscribe("bar", func(t *todo) {
log.Printf("received a todo %+v\n", t)
})
// ...
mtd := &todo{
Title: "get mq work",
Finish: false,
}
err := m.Publish("bar", mtd)
// ...
// Unsubscribe
err := sub.Unsubscribe()
// Close the MQ
m.Close()
```
## Wildcard subscription
MQ supports the use of wildcard with amqp-like topics:
- topic is splitted according to delimiter, default `.`,
- wildcard one (default `*`) matches exactly one word,
- and wildcard some (default `#`) matches zero or many words.
Example of wildcard one:
```{go}
_, err := m.Subscribe("foo.*.baz", func(s string) {
// will receive s = "hello world"
})
_, err := m.Subscribe("foo.*", func(s string) {
// will not be called
})
err := m.Publish("foo.bar", "hello world")
```
Example of wildcard some:
```{go}
_, err := m.Subscribe("foo.#", func(s string) {
// will receive s = "hello my world"
})
_, err := m.Subscribe("#", func(s string) {
// will receive s = "hello my world"
})
})
_, err := m.Subscribe("foo.bar.baz.#", func(s string) {
// will receive s = "hello my world"
})
err := m.Publish("foo.bar.baz", "hello my world")
```
More concrete example of RabbitMQ could be found at [RabbitMQ tutorial - Topics](https://www.rabbitmq.com/tutorials/tutorial-five-go.html). The topic matching github.com/nqd/mq/matcher implement trie topic routing, inherited much from [fast-topic-matching](https://github.com/tylertreat/fast-topic-matching).
## Benchmark
This is the result running in an Ubuntu 16.04 64bit, i7-5600U CPU @ 2.60GHz × 4.
Subscribe all 1000 topics:
```{bash}
BenchmarkMQSubscribe-4 2000 693580 ns/op 144124 B/op 4001 allocs/op
```
Publish all 100000 messages to a MQ with 1000 subscribers
```{bash}
BenchmarkMQPublish-4 3 453835682 ns/op 104704792 B/op 1442775 allocs/op
```
## See also
- [fast-topic-matching](https://github.com/tylertreat/fast-topic-matching)
- [qlobber](https://github.com/davedoesdev/qlobber)
- Very fast and scalable topic routing [1](https://www.rabbitmq.com/blog/2010/09/14/very-fast-and-scalable-topic-routing-part-1/), [2](https://www.rabbitmq.com/blog/2011/03/28/very-fast-and-scalable-topic-routing-part-2/)