Skip to content

Commit 3b4fc1a

Browse files
committed
initial commit, open source simultaneous
1 parent 4a2ab43 commit 3b4fc1a

File tree

13 files changed

+565
-1
lines changed

13 files changed

+565
-1
lines changed

.github/dependabot.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
version: 2
3+
updates:
4+
- package-ecosystem: "gomod" # See documentation for possible values
5+
directory: "/" # Location of package manifests
6+
schedule:
7+
interval: "daily"
8+
reviewers:
9+
- muir
10+
- dncohen
11+
12+
- package-ecosystem: github-actions
13+
directory: "/"
14+
schedule:
15+
interval: "daily"
16+
reviewers:
17+
- muir
18+
- dncohen

.github/workflows/codecov.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Test and coverage
2+
3+
on: [push, pull_request]
4+
5+
permissions: # added using https://github.com/step-security/secure-workflows
6+
contents: read
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Harden Runner
13+
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481
14+
with:
15+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
16+
17+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
18+
with:
19+
fetch-depth: 2
20+
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b
21+
with:
22+
go-version: '1.20'
23+
- name: Run coverage
24+
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic
25+
- name: Upload coverage to Codecov
26+
uses: codecov/[email protected]
27+
with:
28+
verbose: true
29+
env:
30+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ main ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main ]
20+
schedule:
21+
- cron: '20 3 * * 4'
22+
23+
permissions: # added using https://github.com/step-security/secure-workflows
24+
contents: read
25+
26+
jobs:
27+
analyze:
28+
name: Analyze
29+
runs-on: ubuntu-latest
30+
permissions:
31+
actions: read
32+
contents: read
33+
security-events: write
34+
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
language: [ 'go' ]
39+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
40+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
41+
42+
steps:
43+
- name: Harden Runner
44+
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481
45+
with:
46+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
47+
48+
- name: Checkout repository
49+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
50+
51+
# Initializes the CodeQL tools for scanning.
52+
- name: Initialize CodeQL
53+
uses: github/codeql-action/init@1b549b9259bda1cb5ddde3b41741a82a2d15a841
54+
with:
55+
languages: ${{ matrix.language }}
56+
# If you wish to specify custom queries, you can do so here or in a config file.
57+
# By default, queries listed here will override any specified in a config file.
58+
# Prefix the list here with "+" to use these queries and those in the config file.
59+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
60+
61+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
62+
# If this step fails, then you should remove it and run the build manually (see below)
63+
- name: Autobuild
64+
uses: github/codeql-action/autobuild@1b549b9259bda1cb5ddde3b41741a82a2d15a841
65+
66+
# ℹ️ Command-line programs to run using the OS shell.
67+
# 📚 https://git.io/JvXDl
68+
69+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
70+
# and modify them (or add more) to build your code if your project
71+
# uses a compiled language
72+
73+
#- run: |
74+
# make bootstrap
75+
# make release
76+
77+
- name: Perform CodeQL Analysis
78+
uses: github/codeql-action/analyze@1b549b9259bda1cb5ddde3b41741a82a2d15a841

.github/workflows/go.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
on: [push, pull_request]
2+
name: Unit Tests
3+
permissions: # added using https://github.com/step-security/secure-workflows
4+
contents: read
5+
6+
jobs:
7+
test:
8+
strategy:
9+
matrix:
10+
go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.x]
11+
os: [ubuntu-latest, macos-latest, windows-latest]
12+
runs-on: ${{ matrix.os }}
13+
steps:
14+
- name: Harden Runner
15+
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481
16+
with:
17+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
18+
19+
- name: Install Go
20+
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b
21+
with:
22+
go-version: ${{ matrix.go-version }}
23+
- name: Checkout code
24+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
25+
- name: Test
26+
run: go test ./...
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: golangci-lint
2+
on: [push]
3+
permissions: # added using https://github.com/step-security/secure-workflows
4+
contents: read
5+
6+
jobs:
7+
golangci:
8+
permissions:
9+
contents: read # for actions/checkout to fetch code
10+
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
11+
name: lint
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Harden Runner
15+
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481
16+
with:
17+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
18+
19+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd
22+
with:
23+
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
24+
version: latest
25+
26+
# Optional: working directory, useful for monorepos
27+
# working-directory: somedir
28+
output: checkstyle
29+
30+
# Optional: golangci-lint command line arguments.
31+
# args: --issues-exit-code=0
32+
# args: --out-format checkstyle
33+
34+
# Optional: show only new issues if it's a pull request. The default value is `false`.
35+
# only-new-issues: true
36+
37+
# Optional: if set to true then the action will use pre-installed Go.
38+
# skip-go-installation: true
39+
40+
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
41+
# skip-pkg-cache: true
42+
43+
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
44+
# skip-build-cache: true

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 SingleStore, Inc
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.
22+

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# make formats, tests, and validates
2+
3+
4+
DOLLAR=$
5+
6+
all:
7+
git status | awk '/modified:/{print ${DOLLAR}NF}' | perl -n -e 'print if /\.go${DOLLAR}/' | xargs -r gofmt -w -s
8+
go mod tidy
9+
go test ./...
10+
golangci-lint run
11+
@ echo any output from the following command indicates an out-of-date direct dependency
12+
go list -u -m -f '{{if (and (not .Indirect) .Update)}}{{.}}{{end}}' all
13+
14+
misspell:;
15+
go install github.com/client9/misspell/cmd/misspell@latest
16+
misspell -w `find . -name \*.md`
17+

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
# simultaneous
1+
# simultaneous - limit the number of concurrent operations, Go
2+
3+
[![GoDoc](https://godoc.org/github.com/singlestore-labs/simultaneous?status.svg)](https://pkg.go.dev/github.com/singlestore-labs/simultaneous)
4+
![unit tests](https://github.com/singlestore-labs/simultaneous/actions/workflows/go.yml/badge.svg)
5+
[![report card](https://goreportcard.com/badge/github.com/singlestore-labs/simultaneous)](https://goreportcard.com/report/github.com/singlestore-labs/simultaneous)
6+
[![codecov](https://codecov.io/gh/singlestore-labs/simultaneous/branch/main/graph/badge.svg)](https://codecov.io/gh/singlestore-labs/simultaneous)
7+
8+
Install:
9+
10+
go get github.com/singlestore-labs/simultaneous
11+
12+
---
13+
14+
Simultaneous limits the number of concurrent operations. It supports passing proof of
15+
obtaining a limit.
16+
17+
## Simple usage
18+
19+
```go
20+
var limit = simultaneous.New[any](10)
21+
22+
func unlimitedWait() {
23+
defer limit.Forever()()
24+
// do stuff
25+
}
26+
27+
func limitedWait() error {
28+
done, err := limit.Timeout(time.Minute)
29+
if err != nil {
30+
return fmt.Errorf("timeout: %w", err)
31+
}
32+
defer done()
33+
34+
// do stuff
35+
}
36+
```
37+
38+
## Proving that operating within a limit
39+
40+
You can prove that you've got permission
41+
42+
43+
```go
44+
type myLimitType struct{}
45+
46+
var limit = simultaneous.New[myLimitType](10)
47+
48+
func wantsProof(_ Enforced[myLimitType]) {
49+
// do something
50+
}
51+
52+
func providesProof() {
53+
done := limit.Forever()
54+
defer done()
55+
wantsProof(done)
56+
}
57+
```

claude_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package simultaneous_test
2+
3+
// This generated with Claude 3.7 Sonnet
4+
5+
import (
6+
"testing"
7+
"time"
8+
9+
"github.com/singlestore-labs/simultaneous"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// TestTimeoutRejection verifies that Timeout correctly rejects when the limit is full
14+
func TestTimeoutRejection(t *testing.T) {
15+
t.Parallel()
16+
limit := simultaneous.New[any](1)
17+
18+
// Take the only available slot
19+
done := limit.Forever()
20+
defer done.Done()
21+
22+
// This should time out immediately
23+
_, err := limit.Timeout(0)
24+
assert.Error(t, err)
25+
t.Log("Timeout(0) correctly rejected when limit is full")
26+
27+
// This should time out after a short wait
28+
start := time.Now()
29+
_, err = limit.Timeout(50 * time.Millisecond)
30+
duration := time.Since(start)
31+
32+
assert.Error(t, err)
33+
assert.GreaterOrEqual(t, duration, 50*time.Millisecond)
34+
t.Log("Timeout(50ms) correctly waited then rejected when limit remained full")
35+
}
36+
37+
// TestUnlimited verifies that Unlimited provides a way to bypass enforcement
38+
func TestUnlimited(t *testing.T) {
39+
t.Parallel()
40+
41+
// Set up a function that requires proof of limit
42+
called := false
43+
requireLimit := func(proof simultaneous.Enforced[string]) {
44+
called = true
45+
}
46+
47+
// Call it with an unlimited proof
48+
unlimited := simultaneous.Unlimited[string]()
49+
requireLimit(unlimited)
50+
51+
assert.True(t, called)
52+
t.Log("Unlimited token successfully passed type enforcement")
53+
}

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/singlestore-labs/simultaneous
2+
3+
go 1.20
4+
5+
require (
6+
github.com/memsql/errors v0.1.0
7+
github.com/stretchr/testify v1.10.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/kr/text v0.2.0 // indirect
13+
github.com/pkg/errors v0.9.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
gopkg.in/yaml.v3 v3.0.1 // indirect
16+
)

0 commit comments

Comments
 (0)