-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit f86b0de
Showing
5 changed files
with
126 additions
and
0 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,13 @@ | ||
#vim temp files | ||
*~ | ||
*.swp | ||
*.rpm | ||
|
||
#log files | ||
*.log | ||
*.err | ||
|
||
#ctags | ||
tags | ||
|
||
#ignore the go executables |
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,27 @@ | ||
Copyright (c) 2014, Syncbak Inc. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
* Neither the name of Syncbak Inc nor the names of its contributors | ||
may be used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
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,10 @@ | ||
Package heartbeat | ||
=============== | ||
|
||
import "github.com/Syncbak-Git/heartbeat" | ||
|
||
Package heartbeat provides functionality to invoke panic() if the program does not call a heartbeat function within a configurable time period. | ||
|
||
Documentation is available at | ||
[](https://godoc.org/github.com/Syncbak-Git/heartbeat) | ||
|
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,35 @@ | ||
// Package heartbeat provides functionality to invoke panic() if the program does not call a heartbeat function within a configurable time period. | ||
package heartbeat | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Heartbeat launches a go routine that will panic if the returned function is not called at least once per time interval t. | ||
// message is an optional parameter. If supplied, it will override the default panic message "Heartbeat timer expired." | ||
// cleanup is an optional parameter. If supplied, it will be called during the panic() unwinding. It can be used to recover() from the panic, if the program does not want to exit. | ||
func Heartbeat(t time.Duration, message string, cleanup func()) func(cancel bool) { | ||
if len(message) == 0 { | ||
message = "Heartbeat timer expired." | ||
} | ||
tt := time.NewTimer(t) | ||
f := func(cancel bool) { | ||
if cancel { | ||
tt.Stop() | ||
} else { | ||
tt.Reset(t) | ||
} | ||
} | ||
go func() { | ||
if cleanup != nil { | ||
defer cleanup() | ||
} | ||
for { | ||
select { | ||
case <-tt.C: | ||
panic(message) | ||
} | ||
} | ||
}() | ||
return f | ||
} |
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,41 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestHeartbeat(t *testing.T) { | ||
d := time.Millisecond * 50 | ||
msg := "Test message" | ||
fired := false | ||
cleanup := func() { | ||
if m := recover(); m != nil { | ||
fired = true | ||
if msg != m { | ||
t.Errorf("Override panic message not set. Expected %s, got %s\n", msg, m) | ||
} | ||
} | ||
} | ||
h := Heartbeat(d, msg, cleanup) | ||
time.Sleep(2 * d) | ||
if !fired { | ||
t.Errorf("Heartbeat timer didn't fire") | ||
} | ||
h = Heartbeat(d, msg, nil) | ||
tt := time.NewTicker(d / 2) | ||
i := 0 | ||
for { | ||
select { | ||
case <-tt.C: | ||
i++ | ||
if i < 4 { | ||
h(false) | ||
} else if i == 4 { | ||
h(true) | ||
} else if i == 10 { | ||
return | ||
} | ||
} | ||
} | ||
} |