semaphore

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Report Card testworkflow

Resizable Semaphore

A resizable semaphore for Golang.

Inspired by and possible due to https://github.com/eapache/channels.

Feature Overview

  • Conforms to typical semaphore interfaces
  • Semaphore can be resized, up or down, at runtime

Example

General Usage
package main

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

	"github.com/tlkamp/go-semaphore"
)

func main() {
	sem := semaphore.New(2)
	wg := &sync.WaitGroup{}

	for i := 0; i <= 10; i++ {
		err := sem.Acquire(context.Background())
		if err != nil {
			fmt.Println(err)
			return
		}

		wg.Add(1)
		go func(n int) {
			defer sem.Release()
			defer wg.Done()

			time.Sleep(3 * time.Second)
			fmt.Printf("Processed: %d\n", n)
		}(i)
	}

	wg.Wait()
	fmt.Println("Complete")
}
Resize the Semaphore
package main

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

	"github.com/tlkamp/go-semaphore"
)

func longFn(i int) {
	time.Sleep(200 * time.Millisecond)
	fmt.Printf("Processed: %d\n", i)
}

func main() {
	sem := semaphore.New(1)
	wg := &sync.WaitGroup{}

	// Set a timer and then increase the semaphore substantially
	go func() {
		time.Sleep(3 * time.Second)
		sem.Resize(10)
		fmt.Printf("===> Resized to %d\n", sem.Cap())
	}()

	for i := 0; i <= 100; i++ {
		err := sem.Acquire(context.Background())
		if err != nil {
			fmt.Println(err)
			return
		}

		wg.Add(1)
		go func(n int) {
			defer sem.Release()
			defer wg.Done()

			longFn(n)
		}(i)
	}

	wg.Wait()
	fmt.Println("Complete")
}

Contribute

Contributions are accepted in the form of issues and PRs.

PRs must have:

  • Test cases
  • Documentation
  • Example (if applicable)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ResizableSemaphore

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

func New

func New(n int) *ResizableSemaphore

ResizeableSemaphore returns an initialized semaphore with n slots.

func (*ResizableSemaphore) Acquire

func (r *ResizableSemaphore) Acquire(ctx context.Context) error

Acquire will attempt to acquire a slot. Will return an error if the context is canceled.

func (*ResizableSemaphore) Cap

func (r *ResizableSemaphore) Cap() int

Cap returns the capacity of the semaphore (total slots available)

func (*ResizableSemaphore) Len

func (r *ResizableSemaphore) Len() int

Len returns the length of the semaphore (the actively used slots)

func (*ResizableSemaphore) Release

func (r *ResizableSemaphore) Release()

Release frees up a slot.

func (*ResizableSemaphore) Resize

func (r *ResizableSemaphore) Resize(n int)

Resize resizes the underlying channel, increasing or reducing available slots.

func (*ResizableSemaphore) String

func (r *ResizableSemaphore) String() string

func (*ResizableSemaphore) TryAcquire added in v1.1.0

func (r *ResizableSemaphore) TryAcquire() bool

TryAcquire will acquire a slot if any are available. A boolean indicating whether or not a slot was acquired is returned.

Jump to

Keyboard shortcuts

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