-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a969df
commit 4a07d7e
Showing
15 changed files
with
217 additions
and
35 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
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,42 @@ | ||
package protect | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/apache/servicecomb-service-center/pkg/log" | ||
) | ||
|
||
// ProtectionChecker checks whether to do the protection on null instance | ||
type ProtectionChecker interface { | ||
CheckProtection() bool | ||
} | ||
|
||
// DelayedStopProtectChecker means start the protection and close it after some time | ||
type DelayedStopProtectChecker struct { | ||
delay time.Duration | ||
start time.Time | ||
logWhenStop sync.Once | ||
} | ||
|
||
func NewDelayedSuccessChecker(delay time.Duration) *DelayedStopProtectChecker { | ||
return &DelayedStopProtectChecker{ | ||
delay: delay, | ||
start: time.Now(), | ||
logWhenStop: sync.Once{}, | ||
} | ||
} | ||
|
||
func (d *DelayedStopProtectChecker) CheckProtection() bool { | ||
if time.Since(d.start) > d.delay { | ||
d.logWhenStop.Do(func() { | ||
log.Info("null instance protection stopped") | ||
}) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func GetGlobalProtectionChecker() ProtectionChecker { | ||
return globalProtectionChecker | ||
} |
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,15 @@ | ||
package protect | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDelayedStopProtectChecker_CheckProtection(t *testing.T) { | ||
p := NewDelayedSuccessChecker(1 * time.Second) | ||
assert.True(t, p.CheckProtection()) | ||
time.Sleep(p.delay) | ||
assert.False(t, p.CheckProtection()) | ||
} |
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
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
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
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
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.