Skip to content

Commit 78b8ee8

Browse files
committed
Rewrite to use a structure instead of a channel
1 parent 975aed6 commit 78b8ee8

24 files changed

+858
-829
lines changed

.buildkite/pipeline.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ steps:
1111
- test-collector#v1.10.0:
1212
files: test.xml
1313
format: junit
14+
env:
15+
GOEXPERIMENT: rangefunc
1416

1517
- label: ':codecov: + :codeclimate: Coverage'
1618
commands:
1719
- go test -race -coverprofile=cover.out ./...
1820
- sh .buildkite/upload_coverage.sh cover.out
21+
env:
22+
GOEXPERIMENT: rangefunc

.codeclimate.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ exclude_patterns:
1212
- "go.mod"
1313
- "go.sum"
1414
- "LICENSE"
15+
- "nocopy.go"
16+
engines:
17+
golangci:
18+
enabled: true

.github/codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ coverage:
44
project: false
55
patch: false
66
ignore:
7-
- internal/mocks
7+
- nocopy.go

.github/workflows/test.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ name: Test
1010
jobs:
1111
test:
1212
runs-on: ubuntu-latest
13-
env:
14-
GOEXPERIMENT: rangefunc
13+
permissions:
14+
checks: write
15+
contents: read
16+
pull-requests: read
17+
statuses: write
1518
steps:
1619
- name: ✔ Check out
1720
uses: actions/checkout@v4
@@ -20,5 +23,11 @@ jobs:
2023
with:
2124
go-version: "1.22"
2225
check-latest: true
26+
- name: 🧸 golangci-lint
27+
uses: golangci/golangci-lint-action@v4
28+
with:
29+
version: v1.56.2
2330
- name: 🔨 Test
2431
run: go test -race ./...
32+
env:
33+
GOEXPERIMENT: rangefunc

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/bin/
1414
/cover.out
1515
/test.xml
16+
/trace.out

README.md

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,14 @@
77
[![Maintainability](https://api.codeclimate.com/v1/badges/72fe9626fb821fc70251/maintainability)](https://codeclimate.com/github/fillmore-labs/async-exp/maintainability)
88
[![Go Report Card](https://goreportcard.com/badge/fillmore-labs.com/exp/async)](https://goreportcard.com/report/fillmore-labs.com/exp/async)
99
[![License](https://img.shields.io/github/license/fillmore-labs/exp-async)](https://www.apache.org/licenses/LICENSE-2.0)
10+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ffillmore-labs%2Fasync-exp.svg?type=shield&issueType=license)](https://app.fossa.com/projects/git%2Bgithub.com%2Ffillmore-labs%2Fasync-exp)
1011

1112
The `async` package provides interfaces and utilities for writing asynchronous code in Go.
1213

1314
## Motivation
1415

15-
Futures and promises are constructs used for asynchronous and concurrent programming, allowing developers to work with
16-
values that may not be immediately available and can be evaluated in a different execution context.
17-
18-
Go is known for its built-in concurrency features like goroutines and channels.
19-
The select statement further allows for efficient multiplexing and synchronization of multiple channels, thereby
20-
enabling developers to coordinate and orchestrate asynchronous operations effectively.
21-
Additionally, the context package offers a standardized way to manage cancellation, deadlines, and timeouts within
22-
concurrent and asynchronous code.
23-
24-
On the other hand, Go's error handling mechanism, based on explicit error values returned from functions, provides a
25-
clear and concise way to handle errors.
26-
27-
The purpose of this package is to provide a thin layer over channels which simplifies the integration of concurrent
28-
code while providing a cohesive strategy for handling asynchronous errors.
29-
By adhering to Go's standard conventions for asynchronous and concurrent code, as well as error propagation, this
30-
package aims to enhance developer productivity and code reliability in scenarios requiring asynchronous operations.
16+
This is an experimental package which has a similar API as
17+
[fillmore-labs.com/promise](https://pkg.go.dev/fillmore-labs.com/promise), but is implemented with a structure instead.
3118

3219
## Usage
3320

@@ -37,18 +24,19 @@ address (see [GetMyIP](#getmyip) for an example).
3724
Now you can do
3825

3926
```go
40-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
27+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
4128
defer cancel()
4229

43-
future := async.NewFutureAsync(func() (string, error) {
44-
return getMyIP(ctx)
45-
})
30+
query := func() (string, error) {
31+
return getMyIP(ctx) // Capture context with timeout
32+
}
33+
future := async.NewAsync(query)
4634
```
4735

4836
and elsewhere in your program, even in a different goroutine
4937

5038
```go
51-
if ip, err := future.Wait(ctx); err == nil {
39+
if ip, err := future.Await(ctx); err == nil {
5240
slog.Info("Found IP", "ip", ip)
5341
} else {
5442
slog.Error("Failed to fetch IP", "error", err)
@@ -77,27 +65,15 @@ func getMyIP(ctx context.Context) (string, error) {
7765
}
7866
defer func() { _ = resp.Body.Close() }()
7967

80-
ipResponse := &struct {
68+
ipResponse := struct {
8169
Origin string `json:"origin"`
8270
}{}
71+
err = json.NewDecoder(resp.Body).Decode(&ipResponse)
8372

84-
if err := json.NewDecoder(resp.Body).Decode(ipResponse); err != nil {
85-
return "", err
86-
}
87-
88-
return ipResponse.Origin, nil
73+
return ipResponse.Origin, err
8974
}
9075
```
9176

92-
## Concurrency Correctness
93-
94-
When utilizing plain Go channels for concurrency, reasoning over the correctness of concurrent code becomes simpler
95-
compared to some other implementations of futures and promises.
96-
Channels provide a clear and explicit means of communication and synchronization between concurrent goroutines, making
97-
it easier to understand and reason about the flow of data and control in a concurrent program.
98-
99-
Therefore, this library provides a straightforward and idiomatic approach to achieving concurrency correctness.
100-
10177
## Links
10278

10379
- [Futures and Promises](https://en.wikipedia.org/wiki/Futures_and_promises) in the English Wikipedia

async_test.go

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)