Skip to content

Releases: quadtrix/basicqueue

0.9.1-RC1

09 Aug 06:40
39b4654
Compare
Choose a tag to compare
0.9.1-RC1 Pre-release
Pre-release

Quadtrix BasicQueue package for Go

This package enables Go developers to use queues to communicate between goroutines. It is much more intuitive than channels. This package features:

  • Plaintext message queueing as well as JSON message queueing
  • Automatic queue cleanups
  • Registration of producers and consumers

It also supports logging through the Quadtrix servicelogger package

Example
To stop your goroutines via a queue, do something like this:

main.go:

func main() {
    queue_id := "example"
    slog, _ := servicelogger.New("", "logfile.log", servicelogger.LL_INFO, true, "100M", 5)
    stopQueue, _ := basicqueue.NewEncryptedJsonQueue(&slog, basicqueue.BQT_BROADCAST, "stopqueue", -1, true, time.Minute)
    stopQueue.RegisterProducer(queue_id)
    stopQueue.RegisterConsumer(queue_id)
    goroutine1 := example_package.New(stopQueue)
    go goroutine1.Run()
    stopQueue.AddJsonMessage(queue_id, "main", "goroutine1", "STOP", "")
    for !goroutine1.Stopped {
        time.Sleep(time.Second)
    }
    slog.logInfo("main", "main", "goroutine1 is stopped")
}

example_package.go:

package example_package

import (
 "github.com/quadtrix/basicqueue"
)

type GoRoutine1 struct {
   queue_id    string
   stopQueue *basicqueue.BasicQueue
   Stopped     bool
}

func New(stopQueue *basicqueue.BasicQueue) (gr GoRoutine1) {
   gr.Stopped = false
   gr.queuePolling()
   return gr
}

func (gr *GoRoutine1) queuePolling() {
  queueHistory := []string{}
 for !gr.Stopped {
   if gr.stopQueue.PollWithHistory(queue_id, queueHistory) {
     message, err := gr.ReadJsonWithHistory(queue_id, queueHistory)
     if err != nil {
         // To make sure it always stops, ignore errors and exit immediately
         gr.Stopped = true
         return
     }
     queueHistory = append(queueHistory, message.MessageID)
     if message.Destination == "goroutine1" || message.Destination == "all" {
         switch message.MessageType {
         case "STOP":
             gr.Stopped = true
         }
     }
   }
   time.Sleep(time.Second) // to make sure the CPU doesn't get overloaded
 }

What's Changed

Full Changelog: v0.9...v0.9.1-RC1