https://github.com/slclub/goqueue
Circular queue. When you finish pulling or pushing, you don't need to use lock(sync.Mutex)
https://github.com/slclub/goqueue
Last synced: 3 months ago
JSON representation
Circular queue. When you finish pulling or pushing, you don't need to use lock(sync.Mutex)
- Host: GitHub
- URL: https://github.com/slclub/goqueue
- Owner: slclub
- Created: 2020-05-13T07:36:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-16T09:20:51.000Z (about 3 years ago)
- Last Synced: 2025-02-04T09:36:57.883Z (3 months ago)
- Language: Go
- Size: 28.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## QueueRing No Lock
### Summary
Using ring message queue to realize no lock,but used the time.Sleep.
It is quicker than sync.mutex finish the queue security. Also different from many to one communication realized by channel.
I used the channel also. If the data is much larger it will performance much better.
It is't an single server. It is like a plugin.### Install
Only run on the go environment. Go version is 1.11+ that is better.
- Download with go get
`$ go get -u github.com/slclub/goqueue`
- go mod
`$ go mod download`
### Quick Start
Let's go straight to show the code.
```go
import (
"fmt"
"github.com/slclub/goqueue"
"time"
)func main() {
// create a queue, if you use the default config, It is over.
ring := goqueue.NewQueue()//// Set the ring queue size.
//ring.Set("capacity", 1024*1024)
//// Set the writer pointer number
//ring.Set("pool_size", 20)
//// When you use Set method , you should invoke Reload method flush them.
//ring.Reload()//send routine. you can add more routines.
go func() {
send_str := "0 your first message 1"
ring.WriteString(send_str)
}()go func() {
// Total length of message.
rev_len := 0for {
rev_str := ring.ReadString(0)
if rev_str != "" {
fmt.Println("MSG:", rev_str, "REV_LEN:", rev_len)
}
}
}()time.Sleep(30 * time.Second)
}
```
### Mind
- many writer pointer write data in different routines to the ring.
- Only one reader pointer read data from the ring, also you can use a single routine.### Use environment recommend.
- Logger plugin
Write data to file### User
- [glog](https://github.com/slclub/glog) - A small log system