Skip to content

Commit b82721b

Browse files
committed
Initial commit
0 parents  commit b82721b

14 files changed

Lines changed: 802 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_requests:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: stable
19+
20+
- name: Test
21+
run: go test ./...

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tests
2+
*.test
3+
*.out
4+
5+
# IDEs
6+
.idea
7+
.vscode
8+
9+
# Emacs
10+
*~
11+
\#*\#
12+
13+
# OS
14+
.DS_Store

.pre-commit-config.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.6.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
9+
- repo: local
10+
hooks:
11+
- id: go-fmt
12+
name: go fmt
13+
entry: gofmt -w
14+
language: system
15+
files: \.go$
16+
17+
- id: go-vet
18+
name: go vet
19+
entry: go vet ./...
20+
language: system
21+
pass_filenames: false
22+
23+
- id: go-test
24+
name: go test
25+
entry: go test ./...
26+
language: system
27+
pass_filenames: false

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2026-01-14 Arian Behvandnejad <behvandnejad@gmail.com>
2+
3+
* reaper Deterministic bounded rewrite loop over a priority
4+
structure.
5+
* heapadapter Adapts container/heap.Interface with explicit
6+
locking.
7+
* sliceheap Minimal generic implementation of
8+
container/heap.Interface backed by a slice.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Arian Behvandnejad
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# reaper
2+
3+
`reaper` implements a deterministic, bounded rewrite loop over a
4+
priority structure.
5+
6+
It repeatedly removes a single elment from a heap, allows controlled
7+
reinsertion, and enforces a strict upper bound on growth per step.
8+
9+
## Motivation
10+
11+
Many systems require repeatedly rewriting or rescheduling items based
12+
on partial information. `reaper` addresses this need while enforcing
13+
three invariants:
14+
15+
1. Only one element is processed at a time
16+
2. Reinsertion is explicitly bounded
17+
3. Callbacks run without holding heap locks
18+
19+
## Core Concepts
20+
21+
### Heap
22+
23+
A `Heap` is a priority structure protected by explicit locking. Reaper
24+
does not assume ownership of synchronization, allowing it to be
25+
integrated into exisiting concurrent systems.
26+
27+
### Callback
28+
29+
A `Callback` inspects a popped element and may emit a replacements.
30+
31+
```go
32+
Visit(front T, emit func(...T) error) (stop bool)
33+
```
34+
35+
- `emit` buffers elements for reinsertion. It returns an error if the
36+
buffer is full.
37+
- emission is bounded by the configured degree
38+
- returning `true` stops the reaping process and restores `front` to
39+
the heap without reinsertion
40+
41+
### Degree
42+
43+
The degree defines the maximum number of elements a callback may
44+
emit for reinsertion per visit.
45+
46+
- `degree > 0`: bounded rewrite system (DAG)
47+
- `degree == 0`: pure consumer
48+
49+
## Usage
50+
51+
```go
52+
r := reaper.New[int](2) // degree 2
53+
54+
cb := reaper.CallbackFunc[int](func(front int, emit func(...int) error) bool {
55+
if front == 1 {
56+
emit(4, 5)
57+
}
58+
59+
retur false
60+
})
61+
62+
res := r.Reap(heap, cb)
63+
```
64+
65+
### Results
66+
`Reap` returns one of:
67+
68+
- `Exhausted`: the heap became empty
69+
- `Stopped`: the callback requested termination
70+
71+
Both of which are perfectly normal outcomes.
72+
73+
## Guarantees
74+
75+
- No heap mutation occurs while callbacks exectue, except caused by
76+
another goroutine
77+
- No callback can cause unbounded growth
78+
- The heap is always in a valid state
79+
80+
## Heap Adapter
81+
82+
The `heapadapter` subpackage provides adapters for using Go’s `container/heap`
83+
with `reaper`.
84+
85+
```go
86+
h := &MyHeap{} // implements container/heap.Interface of Item
87+
mu := &sync.Mutex{}
88+
89+
heap := heapadapter.New[Item](h, mu)
90+
r := reaper.New(0)
91+
_ = r.Reap(heap, cb)
92+
```

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/codegrapple/reaper
2+
3+
go 1.25.5

heapadapter/adapter.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Package heapadapter provides adapters from container/heap to reaper.Heap.
2+
//
3+
// The adapter does not introduce additional semantics or synchronization
4+
// policies. Locking is explicit and delegated to the caller.
5+
package heapadapter
6+
7+
import (
8+
"container/heap"
9+
"sync"
10+
11+
"github.com/codegrapple/reaper"
12+
)
13+
14+
// Adapter wraps a container/heap.Interface and exposes it as a reaper.Heap[T].
15+
//
16+
// The underlying heap.Interface must store values of type T, violation of
17+
// which is a programmer error and will cause a panic.
18+
type Adapter[T any] struct {
19+
h heap.Interface
20+
l sync.Locker
21+
}
22+
23+
// New constructs a new Adapter.
24+
//
25+
// The provided heap.Interface is heap-initialized during construction.
26+
// Locking behavior is fully controlled by the supplied sync.Locker.
27+
func New[T any](h heap.Interface, l sync.Locker) *Adapter[T] {
28+
heap.Init(h)
29+
return &Adapter[T]{h: h, l: l}
30+
}
31+
32+
// Push inserts an element into the heap.
33+
func (a *Adapter[T]) Push(elem T) {
34+
heap.Push(a.h, elem)
35+
}
36+
37+
// Pop removes and returns the minimum element from the heap.
38+
//
39+
// If the underlying heap contains a value that is not of type T, Pop panics.
40+
func (a *Adapter[T]) Pop() T {
41+
v := heap.Pop(a.h)
42+
elem, ok := v.(T)
43+
if !ok {
44+
panic("heapadapter: unexpected element type")
45+
}
46+
return elem
47+
}
48+
49+
// Empty reports whether the heap contains no elements.
50+
func (a *Adapter[T]) Empty() bool {
51+
return a.h.Len() == 0
52+
}
53+
54+
// Lock acquires the heap lock.
55+
func (a *Adapter[T]) Lock() {
56+
a.l.Lock()
57+
}
58+
59+
// Unlock releases the heap lock.
60+
func (a *Adapter[T]) Unlock() {
61+
a.l.Unlock()
62+
}
63+
64+
// Compile-time assertion that Adapter implements reaper.Heap.
65+
var _ reaper.Heap[any] = (*Adapter[any])(nil)

0 commit comments

Comments
 (0)