priorityqueue

package module
v0.0.0-...-2f105ca Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: MIT Imports: 2 Imported by: 0

README

Probabilistic priority queue

The packet implements a priority queue with a high priority return probability:

  1. A high-level item has a better chance of returning faster from the queue, but this condition is not very strict.
  2. Within each level, the queue works according to the FIFO principle.
  3. The queue only uses the standard golang "channel" data type.

Example

see github.com/iostrovok/priorityqueue/console


package main

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/iostrovok/priorityqueue"
)

const (
	countTest = 100 // count test for each level
	lengthCh  = 25  // length internal channel for each level
)

var (
	levels = []int{1, 10, 20, 30, 100}
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	q, out := priorityqueue.New(ctx, lengthCh, levels)

	// call before pushing to avoid the deadlock
	go reader(out)

	w := sync.WaitGroup{}
	for _, pr := range levels {
		w.Add(1)
		go func(pr int) {
			// push N test with selected priority
			for i := 0; i < countTest; i++ {
				q.Push(pr, pr)
			}

			w.Done()
		}(pr)
	}

	// this will be processed with priority 30
	q.Push(55, 55)
	// this will be processed with priority 100
	q.Push(100, 110)
	// this will be processed with priority 1
	q.Push(0, 0)

	// here we just are waiting for all writers are done
	w.Wait()

	// here we just are waiting for reader is done
	time.Sleep(1 * time.Second)
	// correct stop
	cancel()
}

func reader(out chan interface{}) {
	for {
		select {
		case res, ok := <-out:
			if !ok {
				return
			}
			fmt.Printf("%d - ", res)
		}
	}
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IQueue

type IQueue interface {
	Push(data interface{}, priority int)
}

func New

func New(ctx context.Context, length int, priority []int) (IQueue, chan interface{})

New creates a new priority queue and returns channel to get result. - ctx context.Context for final canceling - length is length internal channel for each level. Total memory usage is length * len(priority). - priority is a list of priorities.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is main top structure.

func (*Queue) Push

func (q *Queue) Push(data interface{}, priority int)

Push pushes a value with a given priority into the queue.

type SimpleQueue

type SimpleQueue struct {
	// contains filtered or unexported fields
}

SimpleQueue is simple case of main top structure.

func (*SimpleQueue) Push

func (q *SimpleQueue) Push(data interface{}, _ int)

Push pushes a value with a given priority into the queue.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL