Skip to content

Commit

Permalink
issue #156: update examples in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed May 29, 2020
1 parent af955d5 commit 7a5261c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.travis.yml export-ignore

/.github/ export-ignore
/githooks/ export-ignore

Makefile export-ignore
*.md export-ignore
Expand Down
45 changes: 30 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# sourced by https://github.com/octomation/makefiles

.DEFAULT_GOAL = test-with-coverage
GIT_HOOKS = post-merge pre-commit
GO_VERSIONS = 1.11 1.12 1.13 1.14

SHELL := /bin/bash -euo pipefail # `explain set -euo pipefail`
Expand Down Expand Up @@ -55,19 +56,19 @@ deps-check:
deps-clean:
@go clean -modcache

.PHONY: deps-shake
deps-shake:
@go mod tidy
.PHONY: deps-fetch
deps-fetch:
@go mod download
@if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi

.PHONY: module-deps
module-deps:
@go mod download
.PHONY: deps-tidy
deps-tidy:
@go mod tidy
@if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi

.PHONY: update
update: selector = '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}'
update:
.PHONY: deps-update
deps-update: selector = '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}'
deps-update:
@if command -v egg > /dev/null; then \
packages="`egg deps list`"; \
else \
Expand All @@ -80,17 +81,17 @@ update:
fi; \
if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi

.PHONY: update-all
update-all:
.PHONY: deps-update-all
deps-update-all:
@if [[ "`go version`" == *1.1[1-3]* ]]; then \
go get -d -mod= -u ./...; \
else \
go get -d -u ./...; \
fi; \
if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi

.PHONY: format
format:
.PHONY: go-fmt
go-fmt:
@if command -v goimports > /dev/null; then \
goimports -local $(LOCAL) -ungroup -w $(PATHS); \
else \
Expand Down Expand Up @@ -125,6 +126,11 @@ test-with-coverage:
test-with-coverage-profile:
@go test -cover -covermode count -coverprofile c.out -timeout $(TIMEOUT) $(PACKAGES)

.PHONY: hooks
hooks:
@ls .git/hooks | grep -v .sample | sed 's|.*|.git/hooks/&|' | xargs rm -f || true
@for hook in $(GIT_HOOKS); do cp githooks/$$hook .git/hooks/; done

ifdef GO_VERSIONS

define go_tpl
Expand All @@ -143,17 +149,26 @@ $(foreach version,$(GO_VERSIONS),$(render_go_tpl))
endif


.PHONY: init
init: deps test lint hooks

.PHONY: clean
clean: deps-clean test-clean

.PHONY: deps
deps: module-deps
deps: deps-fetch

.PHONY: env
env: go-env

.PHONY: format
format: go-fmt

.PHONY: generate
generate: go-generate format

.PHONY: refresh
refresh: deps-shake update deps generate format test
refresh: deps-tidy update deps generate format test

.PHONY: update
update: deps-update
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
The package based on [Rican7/retry][] but fully reworked and focused on integration
with the 🚧 [breaker][] and the built-in [context][] packages.

Full description of the idea is available [here][design.page].
A full description of the idea is available [here][design.page].

## 🏆 Motivation

Expand All @@ -24,14 +24,21 @@ these communications more reliable.

## 🤼‍♂️ How to

**Important**: retry/v5 compatible with [breaker][] version v1.2+ and above.

### retry.Do

```go
var response *http.Response

action := func() error {
action := func(ctx context.Context) error {
var err error
response, err = http.Get("https://github.com/kamilsk/retry")
req := http.NewRequestWithContext(
ctx,
http.MethodGet, "https://github.com/kamilsk/retry",
nil,
)
response, err = http.DefaultClient.Do(req)
return err
}

Expand Down Expand Up @@ -110,7 +117,7 @@ func main() {
}
}

func SendRequest() error {
func SendRequest(ctx context.Context) error {
// communicate with some service
}

Expand Down Expand Up @@ -157,8 +164,8 @@ made with ❤️ for everyone

[awesome.page]: https://github.com/avelino/awesome-go#utilities
[awesome.icon]: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
[build.page]: https://travis-ci.org/kamilsk/retry
[build.icon]: https://travis-ci.org/kamilsk/retry.svg?branch=v5
[build.page]: https://travis-ci.com/kamilsk/retry
[build.icon]: https://travis-ci.com/kamilsk/retry.svg?branch=v5
[coverage.page]: https://codeclimate.com/github/kamilsk/retry/test_coverage
[coverage.icon]: https://api.codeclimate.com/v1/badges/ed88afbc0754e49e9d2d/test_coverage
[design.page]: https://www.notion.so/octolab/retry-cab5722faae445d197e44fbe0225cc98?r=0b753cbf767346f5a6fd51194829a2f3
Expand All @@ -172,12 +179,9 @@ made with ❤️ for everyone

[Avito]: https://tech.avito.ru
[breaker]: https://github.com/kamilsk/breaker
[cli]: https://github.com/kamilsk/try
[cli]: https://github.com/octolab/try
[cli.demo]: https://asciinema.org/a/150367
[cli.preview]: https://asciinema.org/a/150367.png
[context]: https://pkg.go.dev/context
[Lazada]: https://github.com/lazada
[Rican7/retry]: https://github.com/Rican7/retry

[tmp.docs]: https://nicedoc.io/kamilsk/retry?theme=dark
[tmp.history]: https://github.githistory.xyz/kamilsk/retry/blob/v5/README.md
12 changes: 6 additions & 6 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleDo_badCases() {
now := time.Now()
breaker, cancel := context.WithTimeout(context.Background(), needTime)

Silent(retry.Do(breaker, badAction))
silent(retry.Do(breaker, badAction))
if time.Since(now) < realTime {
fmt.Println("unexpected waiting time")
}
Expand All @@ -37,7 +37,7 @@ func ExampleDo_badCases() {
now := time.Now()
breaker, cancel := context.WithTimeout(context.Background(), needTime)

Silent(retry.Do(breaker, func(context.Context) error { return nil }, badStrategy))
silent(retry.Do(breaker, func(context.Context) error { return nil }, badStrategy))
if time.Since(now) < realTime {
fmt.Println("unexpected waiting time")
}
Expand All @@ -49,7 +49,7 @@ func ExampleDo_badCases() {
}

// The example shows the difference between Do and Go.
func ExampleDoAsync_guarantees() {
func ExampleGo_guarantees() {
var (
sleepTime = 100 * time.Millisecond
needTime = 5 * time.Millisecond
Expand All @@ -63,7 +63,7 @@ func ExampleDoAsync_guarantees() {
now := time.Now()
breaker, cancel := context.WithTimeout(context.Background(), needTime)

Silent(retry.Go(breaker, badAction))
silent(retry.Go(breaker, badAction))
if time.Since(now)-needTime > time.Millisecond+inaccuracy {
fmt.Println("unexpected waiting time")
}
Expand All @@ -77,7 +77,7 @@ func ExampleDoAsync_guarantees() {
now := time.Now()
breaker, cancel := context.WithTimeout(context.Background(), needTime)

Silent(retry.Go(breaker, func(context.Context) error { return nil }, badStrategy))
silent(retry.Go(breaker, func(context.Context) error { return nil }, badStrategy))
if time.Since(now)-needTime > time.Millisecond+inaccuracy {
fmt.Println("unexpected waiting time")
}
Expand All @@ -88,4 +88,4 @@ func ExampleDoAsync_guarantees() {
// Output: done
}

func Silent(_ error) {}
func silent(_ error) {}
5 changes: 5 additions & 0 deletions githooks/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -euo pipefail

make deps hooks
6 changes: 6 additions & 0 deletions githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

make deps-tidy deps-fetch lint format
git diff --quiet --exit-code
6 changes: 6 additions & 0 deletions githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

make generate
git diff --quiet --exit-code

0 comments on commit 7a5261c

Please sign in to comment.