Skip to content

Commit 86d3c5b

Browse files
authored
Merge pull request #2 from reugn/develop
v0.3.0
2 parents 15e5b39 + 1a73892 commit 86d3c5b

16 files changed

+242
-87
lines changed

.codecov.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 80%
6+
patch:
7+
default:
8+
target: 70%

.github/workflows/build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
go-version: [1.17.x]
11+
steps:
12+
- name: Setup Go
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: ${{ matrix.go-version }}
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
- name: Run coverage
19+
run: go test . -coverprofile=coverage.out -covermode=atomic
20+
- name: Upload coverage to Codecov
21+
run: bash <(curl -s https://codecov.io/bash)

.github/workflows/golangci-lint.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
8+
jobs:
9+
golangci:
10+
name: lint
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: golangci-lint
15+
uses: golangci/golangci-lint-action@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
.vscode/
33
/vendor
4+
coverage.out

.golangci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
skip-dirs:
3+
- generic
4+
5+
linters:
6+
disable-all: true
7+
enable:
8+
- deadcode
9+
- dupl
10+
- errcheck
11+
- exportloopref
12+
- funlen
13+
- goconst
14+
- gocritic
15+
- gocyclo
16+
- gofmt
17+
- goimports
18+
- gosimple
19+
- govet
20+
- ineffassign
21+
- lll
22+
- misspell
23+
- prealloc
24+
- revive
25+
- staticcheck
26+
- structcheck
27+
- stylecheck
28+
- typecheck
29+
- unconvert
30+
- unparam
31+
- unused
32+
- varcheck
33+
34+
issues:
35+
exclude-rules:
36+
# Exclude some linters from running on tests files.
37+
- path: _test\.go
38+
linters:
39+
- errcheck
40+
- unparam

.travis.yml

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

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<p align="center"><img src="./images/async.png" /></p>
22
<p align="center">Alternative sync library for Go.</p>
33
<p align="center">
4-
<a href="https://travis-ci.org/reugn/async"><img src="https://travis-ci.org/reugn/async.svg?branch=master"></a>
5-
<a href="https://godoc.org/github.com/reugn/async"><img src="https://godoc.org/github.com/reugn/async?status.svg"></a>
4+
<a href="https://github.com/reugn/async/actions/workflows/build.yml"><img src="https://github.com/reugn/async/actions/workflows/build.yml/badge.svg"></a>
5+
<a href="https://pkg.go.dev/github.com/reugn/async"><img src="https://pkg.go.dev/badge/github.com/reugn/async"></a>
66
<a href="https://goreportcard.com/report/github.com/reugn/async"><img src="https://goreportcard.com/badge/github.com/reugn/async"></a>
77
<a href="https://codecov.io/gh/reugn/async"><img src="https://codecov.io/gh/reugn/async/branch/master/graph/badge.svg"></a>
88
</p>
@@ -12,6 +12,7 @@
1212
* **Promise** - While futures are defined as a type of read-only placeholder object created for a result which doesn’t yet exist, a promise can be thought of as a writable, single-assignment container, which completes a future.
1313
* **Reentrant Lock** - Mutex that allows goroutines to enter into the lock on a resource more than once.
1414
* **Optimistic Lock** - Mutex that allows optimistic reading. Could be retried or switched to RLock in case of failure. Significantly improves performance in case of frequent reads and short writes. See [benchmarks](./benchmarks/README.md).
15+
* [**Go 1.18 Generics prototypes**](./generic)
1516

1617
## Examples
1718
Can be found in the examples directory/tests.

benchmarks/lock_bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/reugn/async"
1010
)
1111

12-
//go test -bench=. -benchmem -v
12+
// go test -bench=. -benchmem -v
1313

1414
type syncList struct {
1515
list []string

future.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@ import "sync"
66
// but will be available at some point, or an error if that value could not be made available.
77
type Future interface {
88

9-
// Creates a new future by applying a function to the successful result of this future.
9+
// Map creates a new Future by applying a function to the successful result of this Future.
1010
Map(func(interface{}) (interface{}, error)) Future
1111

12-
// Creates a new future by applying a function to the successful result of
13-
// this future, and returns the result of the function as the new future.
12+
// FlatMap creates a new Future by applying a function to the successful result of
13+
// this Future.
1414
FlatMap(func(interface{}) (Future, error)) Future
1515

16-
// Blocks until Future completed and return either result or error.
16+
// Get blocks until the Future is completed and returns either a result or an error.
1717
Get() (interface{}, error)
1818

19-
// Creates a new future that will handle any error that this
20-
// future might contain. If this future contains
21-
// a valid result then the new future will contain the same.
19+
// Recover handles any error that this Future might contain using a resolver function.
2220
Recover(func() (interface{}, error)) Future
2321

24-
// Creates a new future that will handle any error that this
25-
// future might contain by assigning it a value of another future.
26-
// If this future contains a valid result then the new future will contain the same result.
22+
// RecoverWith handles any error that this Future might contain using another Future.
2723
RecoverWith(Future) Future
2824

29-
// Complete future with either result or error
30-
// For Promise use internally
25+
// complete completes the Future with either a value or an error.
26+
// Is used by Promise internally.
3127
complete(interface{}, error)
3228
}
3329

34-
// FutureImpl Future implementation
30+
// FutureImpl implements the Future interface.
3531
type FutureImpl struct {
3632
acc sync.Once
3733
compl sync.Once
@@ -40,14 +36,14 @@ type FutureImpl struct {
4036
err error
4137
}
4238

43-
// NewFuture returns new Future
39+
// NewFuture returns a new Future.
4440
func NewFuture() Future {
4541
return &FutureImpl{
4642
done: make(chan interface{}),
4743
}
4844
}
4945

50-
// accept blocks once until result is available
46+
// accept blocks once, until the Future result is available.
5147
func (fut *FutureImpl) accept() {
5248
fut.acc.Do(func() {
5349
sig := <-fut.done
@@ -60,7 +56,8 @@ func (fut *FutureImpl) accept() {
6056
})
6157
}
6258

63-
// Map default implementation
59+
// Map creates a new Future by applying a function to the successful result of this Future
60+
// and returns the result of the function as a new Future.
6461
func (fut *FutureImpl) Map(f func(interface{}) (interface{}, error)) Future {
6562
next := NewFuture()
6663
go func() {
@@ -74,7 +71,8 @@ func (fut *FutureImpl) Map(f func(interface{}) (interface{}, error)) Future {
7471
return next
7572
}
7673

77-
// FlatMap default implementation
74+
// FlatMap creates a new Future by applying a function to the successful result of
75+
// this Future and returns the result of the function as a new Future.
7876
func (fut *FutureImpl) FlatMap(f func(interface{}) (Future, error)) Future {
7977
next := NewFuture()
8078
go func() {
@@ -93,35 +91,43 @@ func (fut *FutureImpl) FlatMap(f func(interface{}) (Future, error)) Future {
9391
return next
9492
}
9593

96-
// Get default implementation
94+
// Get blocks until the Future is completed and returns either a result or an error.
9795
func (fut *FutureImpl) Get() (interface{}, error) {
9896
fut.accept()
9997
return fut.value, fut.err
10098
}
10199

102-
// Recover default implementation
100+
// Recover handles any error that this Future might contain using a given resolver function.
101+
// Returns the result as a new Future.
103102
func (fut *FutureImpl) Recover(f func() (interface{}, error)) Future {
104-
fut.accept()
105-
if fut.err != nil {
106-
next := NewFuture()
107-
next.complete(f())
108-
return next
109-
}
110-
return fut
103+
next := NewFuture()
104+
go func() {
105+
fut.accept()
106+
if fut.err != nil {
107+
next.complete(f())
108+
} else {
109+
next.complete(fut.value, nil)
110+
}
111+
}()
112+
return next
111113
}
112114

113-
// RecoverWith default implementation
115+
// RecoverWith handles any error that this Future might contain using another Future.
116+
// Returns the result as a new Future.
114117
func (fut *FutureImpl) RecoverWith(rf Future) Future {
115-
fut.accept()
116-
if fut.err != nil {
117-
next := NewFuture()
118-
next.complete(rf.Get())
119-
return next
120-
}
121-
return fut
118+
next := NewFuture()
119+
go func() {
120+
fut.accept()
121+
if fut.err != nil {
122+
next.complete(rf.Get())
123+
} else {
124+
next.complete(fut.value, nil)
125+
}
126+
}()
127+
return next
122128
}
123129

124-
// complete future with either value or error
130+
// complete completes the Future with either a value or an error.
125131
func (fut *FutureImpl) complete(v interface{}, e error) {
126132
fut.compl.Do(func() {
127133
go func() {

generic/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Go 1.18 Generics package
2+
This package contains preliminary [`Go Generics`](https://github.com/golang/go/issues/43651) prototypes.
3+
4+
## Implemented data types
5+
* asyncTask <sup>[1](#unexported)</sup>
6+
7+
<sup name="unexported">1</sup> Unexported since it is not possible to export generic code yet.
8+
9+
## Getting started
10+
11+
### Go 1.17
12+
```sh
13+
go test ./... -gcflags=-G=3 -vet=off
14+
```
15+
16+
### Go 1.18+
17+
Install [gotip](https://pkg.go.dev/golang.org/dl/gotip):
18+
```sh
19+
go install golang.org/dl/gotip@latest
20+
gotip download
21+
```
22+
Use `gotip` to build and test the code.

0 commit comments

Comments
 (0)