Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
/ semaphore Public archive

A simple semaphore implementation in golang using channels

Notifications You must be signed in to change notification settings

vada-ir/semaphore

Repository files navigation

Semaphore

A simple semaphore implementation in golang using channels

Build Status Coverage Status

Usage

package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/vada-ir/semaphore"
)

func crawl(index int, s semaphore.Semaphore) {
	defer s.Release(1)
	fmt.Printf("crawling the site #%d\n", index)
	r := time.Duration(rand.Intn(2))
	time.Sleep(time.Second * r)
	fmt.Printf("job #%d is done\n", index)
}

func main() {
	rand.Seed(time.Now().Unix())
	s := semaphore.NewSemaphore(5)
	for i := 1; i < 100; i++ {
		s.Acquire(1)

		go crawl(i, s)
	}

	// Wait for all permits
	s.Acquire(s.PermitCount())
}

About

A simple semaphore implementation in golang using channels

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages