Skip to content

Commit

Permalink
issue octolab#8: extend Interface, add Err()
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Feb 28, 2020
1 parent 9bdffb4 commit 1aac9ae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@ go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x

jobs:
allow_failures:
- go: master

before_script:
- |
if [[ $TRAVIS_GO_VERSION == 1.13* ]]; then
if [[ $TRAVIS_GO_VERSION == 1.14* ]]; then
curl -L $CODECLIMATE > /home/travis/gopath/bin/cc-test-reporter
chmod +x /home/travis/gopath/bin/cc-test-reporter
cc-test-reporter before-build
fi
script:
- |
if [[ $TRAVIS_GO_VERSION == 1.13* ]]; then
if [[ $TRAVIS_GO_VERSION == 1.14* ]]; then
make test-with-coverage-profile
else
make test
fi
after_script:
- |
if [[ $TRAVIS_GO_VERSION == 1.13* ]]; then
if [[ $TRAVIS_GO_VERSION == 1.14* ]]; then
cc-test-reporter after-build -t gocov -p $(basename $(go list -m)) --exit-code $TRAVIS_TEST_RESULT
fi
Expand Down
19 changes: 14 additions & 5 deletions breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ type breaker struct {
released int32
}

// Done returns a channel that's closed when a cancellation signal occurred.
func (br *breaker) Done() <-chan struct{} {
return br.signal
}

// Close closes the Done channel and releases resources associated with it.
func (br *breaker) Close() {
br.closer.Do(func() {
Expand All @@ -63,6 +58,20 @@ func (br *breaker) Close() {
})
}

// Done returns a channel that's closed when a cancellation signal occurred.
func (br *breaker) Done() <-chan struct{} {
return br.signal
}

// Err returns a non-nil error if Done is closed and nil otherwise.
// After Err returns a non-nil error, successive calls to Err return the same error.
func (br *breaker) Err() error {
if atomic.LoadInt32(&br.released) == 1 {
return Interrupted
}
return nil
}

// Released returns true if resources associated with the Breaker were released.
func (br *breaker) Released() bool {
return atomic.LoadInt32(&br.released) == 1
Expand Down
22 changes: 14 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,42 @@ import (

// BreakByContext returns a new Breaker based on the Context.
func BreakByContext(ctx context.Context, cancel context.CancelFunc) Interface {
return (&contextBreaker{newBreaker(), cancel, ctx.Done()}).trigger()
return (&contextBreaker{newBreaker(), cancel, ctx}).trigger()
}

// WithContext returns a new Breaker and an associated Context derived from ctx.
// Deprecated: use BreakByContext instead.
// TODO:v2 will be removed
func WithContext(ctx context.Context) (Interface, context.Context) {
ctx, cancel := context.WithCancel(ctx)
return (&contextBreaker{newBreaker(), cancel, ctx.Done()}).trigger(), ctx
return (&contextBreaker{newBreaker(), cancel, ctx}).trigger(), ctx
}

type contextBreaker struct {
*breaker
cancel context.CancelFunc
signal <-chan struct{}
ctx context.Context
}

// Close closes the Done channel and releases resources associated with it.
func (br *contextBreaker) Close() {
br.cancel()
}

// Done returns a channel that's closed when a cancellation signal occurred.
func (br *contextBreaker) Done() <-chan struct{} {
return br.signal
return br.ctx.Done()
}

// Close closes the Done channel and releases resources associated with it.
func (br *contextBreaker) Close() {
br.cancel()
// Err returns a non-nil error if Done is closed and nil otherwise.
// After Err returns a non-nil error, successive calls to Err return the same error.
func (br *contextBreaker) Err() error {
return br.ctx.Err()
}

func (br *contextBreaker) trigger() Interface {
go func() {
<-br.signal
<-br.ctx.Done()
atomic.StoreInt32(&br.released, 1)
}()
return br
Expand Down
7 changes: 5 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ package breaker
// }
//
type Interface interface {
// Done returns a channel that's closed when a cancellation signal occurred.
Done() <-chan struct{}
// Close closes the Done channel and releases resources associated with it.
Close()
// Done returns a channel that's closed when a cancellation signal occurred.
Done() <-chan struct{}
// Err returns a non-nil error if Done is closed and nil otherwise.
// After Err returns a non-nil error, successive calls to Err return the same error.
Err() error
// trigger is a private method to guarantee that the Breakers come from
// this package and all of them return a valid Done channel.
trigger() Interface
Expand Down

0 comments on commit 1aac9ae

Please sign in to comment.