Skip to content

Commit cec8c8a

Browse files
committed
test on Go 1.10
2 parents e3ff475 + e2cc807 commit cec8c8a

File tree

10 files changed

+83
-31
lines changed

10 files changed

+83
-31
lines changed

.gitignore

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
# glide from docker
2-
.glide/
3-
4-
# dep, glide
1+
# dep
52
vendor/
63

74
# goreleaser
85
/dist/
96

10-
# go test, code quality report
7+
# go test and code quality report
118
*.out

.goreleaser.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ brew:
2424
github:
2525
name: homebrew-tap
2626
owner: kamilsk
27-
homepage: https://github.com/kamilsk/semaphore
27+
homepage: https://github.com/kamilsk/semaphore
2828

2929
build:
3030
binary: semaphore
@@ -38,7 +38,7 @@ build:
3838
- linux
3939
- windows
4040
ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
41-
main: ./cmd/semaphore
41+
main: ./cmd/semaphore
4242

4343
checksum: { name_template: checksums.txt }
4444

.scrutinizer.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
build:
2+
dependencies:
3+
override:
4+
- export DEP_VERSION=0.4.1; wget -q -O dep https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 && chmod +x dep && mv dep "${GOPATH}"/bin; make cmd-deps-local
5+
6+
environment:
7+
go: go1.9
8+
9+
nodes:
10+
analysis:
11+
tests:
12+
override:
13+
- go-scrutinizer-run
14+
- golint-run
15+
16+
tests:
17+
override:
18+
- make test
19+
20+
checks:
21+
go: true

.travis.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
language: go
22

33
go:
4+
- master
5+
- 1.x
46
- 1.5.x
57
- 1.6.x
68
- 1.7.x
79
- 1.8.x
810
- 1.9.x
9-
- 1.x
10-
- master
11+
- 1.10.x
1112

1213
matrix:
1314
allow_failures:
@@ -17,12 +18,14 @@ matrix:
1718
sudo: false
1819

1920
before_install:
20-
- wget -q -O dep https://github.com/golang/dep/releases/download/v0.3.2/dep-linux-amd64
21-
- chmod +x dep && mv dep "${GOPATH}"/bin
22-
- go get github.com/mattn/goveralls
21+
- export DEP_VERSION=0.4.1
2322
- export GO_TEST_COVERAGE_FILENAME='coverage.out'
2423
- export GO15VENDOREXPERIMENT=1
2524

25+
- wget -q -O dep https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64
26+
- chmod +x dep && mv dep "${GOPATH}"/bin
27+
- go get github.com/mattn/goveralls
28+
2629
install:
2730
- make cmd-deps-local
2831

@@ -32,6 +35,7 @@ script:
3235
after_success:
3336
- goveralls -service=travis-ci -coverprofile="${GO_TEST_COVERAGE_FILENAME}"
3437
- echo $TRAVIS_GO_VERSION ":" $TRAVIS_TAG
38+
# TODO wait https://github.com/travis-ci/gimme/pull/130
3539
- if [[ $TRAVIS_GO_VERSION == 1.9* ]] && [ -n "$TRAVIS_TAG" ]; then curl -sL https://git.io/goreleaser | bash; fi
3640

3741
notifications:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 OctoLab, https://www.octolab.org/ <feedback@octolab.org>
3+
Copyright (c) 2018 OctoLab, https://www.octolab.org/ <feedback@octolab.org>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
> # semaphore [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Semaphore%20pattern%20implementation%20with%20a%20timeout%20of%20lock%2Funlock%20operations%20based%20on%20channels&url=https://github.com/kamilsk/semaphore&via=ikamilsk&hashtags=go,semaphore,throughput,limiter)
2-
> [![Analytics](https://ga-beacon.appspot.com/UA-109817251-2/semaphore/master?pixel)](https://github.com/igrigorik/ga-beacon)
2+
> [![Analytics](https://ga-beacon.appspot.com/UA-109817251-2/semaphore/master?pixel)](https://github.com/kamilsk/semaphore)
33
> Semaphore pattern implementation with timeout of lock/unlock operations based on channels.
44
55
[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#goroutines)
@@ -12,6 +12,23 @@
1212

1313
## Usage
1414

15+
### Quick start
16+
17+
```go
18+
limiter := semaphore.New(1000)
19+
20+
http.HandleFunc("/", func(rw http.ResponseWriter, _ *http.Request) {
21+
if _, err := limiter.Acquire(semaphore.WithTimeout(time.Minute)); err != nil {
22+
http.Error(rw, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
23+
return
24+
}
25+
defer limiter.Release()
26+
// handle request
27+
})
28+
29+
log.Fatal(http.ListenAndServe(":80", nil))
30+
```
31+
1532
### Console tool for command execution in parallel
1633

1734
This example shows how to execute many console commands in parallel.
@@ -256,5 +273,5 @@ use [dep](https://github.com/golang/dep) or something similar for this purpose.
256273
## Notes
257274

258275
- [research](../../tree/research)
259-
- tested on Go 1.5, 1.6, 1.7, 1.8 and 1.9
276+
- tested on Go 1.5, 1.6, 1.7, 1.8, 1.9 and 1.10
260277
- made with ❤️ by [OctoLab](https://www.octolab.org/)

channel.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
// Multiplex combines multiple empty struct channels into one.
11+
// TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
1112
func Multiplex(channels ...<-chan struct{}) <-chan struct{} {
1213
ch := make(chan struct{})
1314
if len(channels) == 0 {
@@ -25,13 +26,29 @@ func Multiplex(channels ...<-chan struct{}) <-chan struct{} {
2526
return ch
2627
}
2728

28-
// WithDeadline returns empty struct channel based on Time channel.
29+
// WithDeadline returns empty struct channel above on `time.Timer` channel.
30+
// TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
2931
func WithDeadline(deadline time.Time) <-chan struct{} {
30-
// go 1.5 doesn't support time.Until(deadline)
31-
return WithTimeout(deadline.Sub(time.Now())) //nolint: gosimple
32+
ch := make(chan struct{})
33+
if time.Now().After(deadline) {
34+
close(ch)
35+
return ch
36+
}
37+
go func() {
38+
// go 1.5 doesn't support time.Until(deadline)
39+
after := deadline.Sub(time.Now()) //nolint: gosimple
40+
if after <= 0 {
41+
close(ch)
42+
return
43+
}
44+
<-time.After(after)
45+
close(ch)
46+
}()
47+
return ch
3248
}
3349

34-
// WithSignal returns empty struct channel based on Signal channel.
50+
// WithSignal returns empty struct channel above on `os.Signal` channel.
51+
// TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
3552
func WithSignal(s os.Signal) <-chan struct{} {
3653
ch := make(chan struct{})
3754
if s == nil {
@@ -48,16 +65,13 @@ func WithSignal(s os.Signal) <-chan struct{} {
4865
return ch
4966
}
5067

51-
// WithTimeout returns empty struct channel based on Time channel.
68+
// WithTimeout returns empty struct channel above on `time.Timer` channel.
69+
// TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
5270
func WithTimeout(timeout time.Duration) <-chan struct{} {
5371
ch := make(chan struct{})
5472
if timeout <= 0 {
5573
close(ch)
5674
return ch
5775
}
58-
go func() {
59-
<-time.After(timeout)
60-
close(ch)
61-
}()
62-
return ch
76+
return WithDeadline(time.Now().Add(timeout))
6377
}

cmd/semaphore/cli.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,7 @@ func (c *HelpCommand) Desc() string {
402402
// Do handles inner error and shows a specific message.
403403
func (c *HelpCommand) Do() error {
404404
switch c.Error {
405-
case nil:
406-
fallthrough
407-
case errNotProvided:
408-
fallthrough
409-
case flag.ErrHelp:
405+
case nil, errNotProvided, flag.ErrHelp:
410406
c.Usage()
411407
return nil
412408
case errNotFound:

cmd/semaphore/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (l Results) Len() int {
141141

142142
// Less compares two results from container with indexes i and j.
143143
func (l Results) Less(i, j int) bool {
144-
return strings.Compare(l[i].Job.ID, l[j].Job.ID) == -1
144+
return l[i].Job.ID < l[j].Job.ID
145145
}
146146

147147
// Swap swaps two results from container with indexes i and j.

context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import "context"
88
func WithContext(parent context.Context, deadline <-chan struct{}) context.Context {
99
ctx, cancel := context.WithCancel(parent)
1010
go func() {
11+
if deadline == nil {
12+
return
13+
}
1114
<-deadline
1215
cancel()
1316
}()

0 commit comments

Comments
 (0)