Weighted Semaphore for Variable-Cost Work

Bound the total weight of concurrent work, acquire and release arbitrary weights, and wake waiters when capacity frees up.

GoResilience

Code

gosnippet
105 lines
package main

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

type WeightedSemaphore struct {
	mu       sync.Mutex
	cond     *sync.Cond
	capacity int64
	used     int64
}

func NewWeightedSemaphore(capacity int64) (*WeightedSemaphore, error) {
	if capacity < 1 {
		return nil, fmt.Errorf("capacity must be positive")
	}
	sem := &WeightedSemaphore{capacity: capacity}
	sem.cond = sync.NewCond(&sem.mu)
	return sem, nil
}

func (s *WeightedSemaphore) Acquire(ctx context.Context, weight int64) error {
	if weight < 1 || weight > s.capacity {
		return fmt.Errorf("weight %d outside 1..%d", weight, s.capacity)
	}

	done := make(chan struct{})
	go func() {
		select {
		case <-ctx.Done():
			s.mu.Lock()
			s.cond.Broadcast()
			s.mu.Unlock()
		case <-done:
		}
	}()
	defer close(done)

	s.mu.Lock()
	defer s.mu.Unlock()
	for s.used+weight > s.capacity {
		if err := ctx.Err(); err != nil {
			return err
		}
		s.cond.Wait()
	}
	if err := ctx.Err(); err != nil {
		return err
	}
	s.used += weight
	return nil
}

func (s *WeightedSemaphore) Release(weight int64) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if weight < 1 || weight > s.used {
		panic(fmt.Sprintf("release %d with %d in use", weight, s.used))
	}
	s.used -= weight
	s.cond.Broadcast()
}

func main() {
	sem, err := NewWeightedSemaphore(3)
	if err != nil {
		fmt.Println(err)
		return
	}

	jobs := []struct {
		name   string
		weight int64
	}{
		{"small", 1},
		{"medium", 2},
		{"large", 3},
		{"small-2", 1},
	}

	var wg sync.WaitGroup
	for _, job := range jobs {
		job := job
		wg.Add(1)
		go func() {
			defer wg.Done()
			ctx, cancel := context.WithTimeout(context.Background(), time.Second)
			defer cancel()
			if err := sem.Acquire(ctx, job.weight); err != nil {
				fmt.Printf("%s: %v\n", job.name, err)
				return
			}
			time.Sleep(20 * time.Millisecond)
			sem.Release(job.weight)
			fmt.Printf("%s finished (weight %d)\n", job.name, job.weight)
		}()
	}
	wg.Wait()
	fmt.Println("all jobs done")
}

What the code does

Count the functions NewWeightedSemaphore, Acquire, Release and main as the entire surface area of Weighted Semaphore for Variable-Cost Work.

The cycling part of this snippet is for s.used+weight > s.capacity. Every fallible call checks err != nil (4 sites), so an error is handled where it happens rather than bubbling invisibly. Work runs on a goroutine and results come back through a channel, giving a safe handoff between the two execution contexts. Deferred at entry, executed at exit: defer keeps cleanup adjacent to acquisition.

What to watch for

  • Failure is a return value (error), which keeps control flow explicit at every call site.

Using it

Signature: func NewWeightedSemaphore(capacity int64) (*WeightedSemaphore, error)

Output lines are prefixed (all jobs done), so each line of output points at the operation responsible for it.

The sequence NewWeightedSemaphore, Acquire and Release lives in main; running the file triggers it.

Key statements

return fmt.Errorf("weight %d outside 1..%d", weight, s.capacity)
panic(fmt.Sprintf("release %d with %d in use", weight, s.used))
return nil, fmt.Errorf("capacity must be positive")

Interactive Tools for This Topic

FAQ

What is the error handling style?
By return value: each fallible call hands back an `error`, checked at 4 places here.
What does Weighted Semaphore for Variable-Cost Work print when run?
The script runs standalone; its 1 labeled outputs include `all jobs done`.