-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: improve breaker algorithm on recovery time
- Loading branch information
Showing
9 changed files
with
273 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package breaker | ||
|
||
const ( | ||
success = iota | ||
fail | ||
drop | ||
) | ||
|
||
// bucket defines the bucket that holds sum and num of additions. | ||
type bucket struct { | ||
Sum int64 | ||
Success int64 | ||
Failure int64 | ||
Drop int64 | ||
} | ||
|
||
func (b *bucket) Add(v int64) { | ||
switch v { | ||
case fail: | ||
b.fail() | ||
case drop: | ||
b.drop() | ||
default: | ||
b.succeed() | ||
} | ||
} | ||
|
||
func (b *bucket) Reset() { | ||
b.Sum = 0 | ||
b.Success = 0 | ||
b.Failure = 0 | ||
b.Drop = 0 | ||
} | ||
|
||
func (b *bucket) drop() { | ||
b.Sum++ | ||
b.Drop++ | ||
} | ||
|
||
func (b *bucket) fail() { | ||
b.Sum++ | ||
b.Failure++ | ||
} | ||
|
||
func (b *bucket) succeed() { | ||
b.Sum++ | ||
b.Success++ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package breaker | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBucketAdd(t *testing.T) { | ||
b := &bucket{} | ||
|
||
// Test succeed | ||
b.Add(0) // Using 0 for success | ||
assert.Equal(t, int64(1), b.Sum, "Sum should be incremented") | ||
assert.Equal(t, int64(1), b.Success, "Success should be incremented") | ||
assert.Equal(t, int64(0), b.Failure, "Failure should not be incremented") | ||
assert.Equal(t, int64(0), b.Drop, "Drop should not be incremented") | ||
|
||
// Test failure | ||
b.Add(fail) | ||
assert.Equal(t, int64(2), b.Sum, "Sum should be incremented") | ||
assert.Equal(t, int64(1), b.Failure, "Failure should be incremented") | ||
assert.Equal(t, int64(0), b.Drop, "Drop should not be incremented") | ||
|
||
// Test drop | ||
b.Add(drop) | ||
assert.Equal(t, int64(3), b.Sum, "Sum should be incremented") | ||
assert.Equal(t, int64(1), b.Drop, "Drop should be incremented") | ||
} | ||
|
||
func TestBucketReset(t *testing.T) { | ||
b := &bucket{ | ||
Sum: 3, | ||
Success: 1, | ||
Failure: 1, | ||
Drop: 1, | ||
} | ||
b.Reset() | ||
assert.Equal(t, int64(0), b.Sum, "Sum should be reset to 0") | ||
assert.Equal(t, int64(0), b.Success, "Success should be reset to 0") | ||
assert.Equal(t, int64(0), b.Failure, "Failure should be reset to 0") | ||
assert.Equal(t, int64(0), b.Drop, "Drop should be reset to 0") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.