queues

package module
v0.0.0-...-cc2c36c Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2022 License: MIT Imports: 2 Imported by: 0

README ¶

Queues

CI Go Report Card Go Reference

A simple sync.WaitGroup like queue and goroutine execution controller.

Features

  • Limit maximum goroutine execution/count.
  • Wait for all the goroutines to finish.
  • Run goroutines like a queue batch.

Import

import "github.com/hsblhsn/queues"

Example

🔗 Go Playground: Limit maximum goroutine counts to 3.
package main

import (
	"fmt"
	"time"

	"github.com/hsblhsn/queues"
)

func main() {
	q := queues.New(3)
	for i := 0; i < 30; i++ {
		q.Add(1)
		go func(n int) {
			defer q.Done()
			time.Sleep(time.Second)
			fmt.Println(n)
		}(i)
	}
	q.Wait()
}
🔗 Go Playground: Batched queue for async jobs.
package main

import (
	"fmt"
	"time"

	"github.com/hsblhsn/queues"
)

func main() {
	urls := []string{
		"https://google.com",
		"https://github.com",
		"https://twitter.com",
		"https://facebook.com",
		"https://youtube.com",
	}
	q := queues.New(2)
	for _, v := range urls {
		q.Add(1)
		go crawl(q, v)
	}
	q.Wait()
}

func crawl(q *queues.Q, url string) {
	defer q.Done()
	fmt.Println("Crawling: ", url)
	time.Sleep(time.Second)
}

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Q ¶

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

Q is a queue. Uses Go Channel and sync.WaitGroup under the hood.

func New ¶

func New(max uint) *Q

New returns a new queue with the given max size. No more than max size items can be added to the queue. It uses Golang's channel under the hood. Think this function as make(chan int, maxSize). So, if you set the max size to 0, it will not work as a queue or buffer.

func (*Q) Add ¶

func (q *Q) Add(delta int)

Add adds an item to the queue. It uses wg.Add(n) to keep track of the number of items in the queue.

func (*Q) Done ¶

func (q *Q) Done()

Done removes an item from the queue. It uses wg.Done() to keep track of the number of items in the queue.

func (*Q) Exit ¶

func (q *Q) Exit()

Exit calls q.Done() first then calls runtime.Goexit(). If called inside a goroutine, the goroutine will exit immediately. See https://golang.org/pkg/runtime/#Goexit for the documentation.

func (*Q) Wait ¶

func (q *Q) Wait()

Wait for the queue to be empty. It uses wg.Wait() to wait for the queue to be empty.

Jump to

Keyboard shortcuts

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