Skip to content

Commit 975aed6

Browse files
committed
Expand and rework to API
1 parent 9417ff0 commit 975aed6

20 files changed

+1195
-351
lines changed

.github/workflows/go.yml

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

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Test
3+
"on":
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
env:
14+
GOEXPERIMENT: rangefunc
15+
steps:
16+
- name: ✔ Check out
17+
uses: actions/checkout@v4
18+
- name: 🐹 Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: "1.22"
22+
check-latest: true
23+
- name: 🔨 Test
24+
run: go test -race ./...

.golangci.yaml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ linters:
2626
- wsl
2727
linters-settings:
2828
testifylint:
29-
enable:
30-
- bool-compare
31-
- compares
32-
- empty
33-
- error-is-as
34-
- error-nil
35-
- expected-actual
36-
- float-compare
37-
# - go-require
38-
- len
39-
# - nil-compare
40-
# - require-error
41-
- suite-dont-use-pkg
42-
- suite-extra-assert-call
43-
- suite-thelper
29+
enable-all: true
30+
disable:
31+
- require-error

README.md

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# Async (Experimental)
1+
# Fillmore Labs Async (Experimental)
22

33
[![Go Reference](https://pkg.go.dev/badge/fillmore-labs.com/exp/async.svg)](https://pkg.go.dev/fillmore-labs.com/exp/async)
44
[![Build Status](https://badge.buildkite.com/06fc8f7bdcfc5c380ea0c7c8bb92a7cee8b1676b841f3c65c8.svg)](https://buildkite.com/fillmore-labs/async-exp)
5+
[![GitHub Workflow](https://github.com/fillmore-labs/exp-async/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/fillmore-labs/async-exp/actions/workflows/test.yml)
56
[![Test Coverage](https://codecov.io/gh/fillmore-labs/async-exp/graph/badge.svg?token=GQUJA8PKJI)](https://codecov.io/gh/fillmore-labs/async-exp)
67
[![Maintainability](https://api.codeclimate.com/v1/badges/72fe9626fb821fc70251/maintainability)](https://codeclimate.com/github/fillmore-labs/async-exp/maintainability)
78
[![Go Report Card](https://goreportcard.com/badge/fillmore-labs.com/exp/async)](https://goreportcard.com/report/fillmore-labs.com/exp/async)
8-
[![License](https://img.shields.io/github/license/fillmore-labs/exp-async)](https://github.com/fillmore-labs/exp-async/blob/main/LICENSE)
9+
[![License](https://img.shields.io/github/license/fillmore-labs/exp-async)](https://www.apache.org/licenses/LICENSE-2.0)
910

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

@@ -36,10 +37,10 @@ address (see [GetMyIP](#getmyip) for an example).
3637
Now you can do
3738

3839
```go
39-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
40+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
4041
defer cancel()
4142

42-
future := async.NewAsyncFuture(func() (string, error) {
43+
future := async.NewFutureAsync(func() (string, error) {
4344
return getMyIP(ctx)
4445
})
4546
```
@@ -48,9 +49,9 @@ and elsewhere in your program, even in a different goroutine
4849

4950
```go
5051
if ip, err := future.Wait(ctx); err == nil {
51-
log.Printf("My IP is %s", ip)
52+
slog.Info("Found IP", "ip", ip)
5253
} else {
53-
log.Printf("Error fetching IP: %v", err)
54+
slog.Error("Failed to fetch IP", "error", err)
5455
}
5556
```
5657

@@ -61,53 +62,30 @@ decoupling query construction from result processing.
6162
Sample code to retrieve your IP address:
6263

6364
```go
64-
const (
65-
serverURL = "https://httpbin.org/ip"
66-
timeout = 2 * time.Second
67-
)
68-
69-
type IPResponse struct {
70-
Origin string `json:"origin"`
71-
}
65+
const serverURL = "https://httpbin.org/ip"
7266

7367
func getMyIP(ctx context.Context) (string, error) {
74-
resp, err := sendRequest(ctx)
68+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, serverURL, nil)
7569
if err != nil {
7670
return "", err
7771
}
72+
req.Header.Set("Accept", "application/json")
7873

79-
ipResponse, err := decodeResponse(resp)
74+
resp, err := http.DefaultClient.Do(req)
8075
if err != nil {
8176
return "", err
8277
}
78+
defer func() { _ = resp.Body.Close() }()
8379

84-
return ipResponse.Origin, nil
85-
}
80+
ipResponse := &struct {
81+
Origin string `json:"origin"`
82+
}{}
8683

87-
func sendRequest(ctx context.Context) (*http.Response, error) {
88-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, serverURL, nil)
89-
if err != nil {
90-
return nil, err
91-
}
92-
req.Header.Set("Accept", "application/json")
93-
94-
return http.DefaultClient.Do(req)
95-
}
96-
97-
func decodeResponse(response *http.Response) (*IPResponse, error) {
98-
body, err := io.ReadAll(response.Body)
99-
_ = response.Body.Close()
100-
if err != nil {
101-
return nil, err
102-
}
103-
104-
ipResponse := &IPResponse{}
105-
err = json.Unmarshal(body, ipResponse)
106-
if err != nil {
107-
return nil, err
84+
if err := json.NewDecoder(resp.Body).Decode(ipResponse); err != nil {
85+
return "", err
10886
}
10987

110-
return ipResponse, nil
88+
return ipResponse.Origin, nil
11189
}
11290
```
11391

async.go

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

0 commit comments

Comments
 (0)