Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-franken committed Jul 30, 2014
0 parents commit f86b0de
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
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
27 changes: 27 additions & 0 deletions LICENSE
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.


10 changes: 10 additions & 0 deletions README.md
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
[![GoDoc](https://godoc.org/github.com/Syncbak-Git/heartbeat?status.png)](https://godoc.org/github.com/Syncbak-Git/heartbeat)

35 changes: 35 additions & 0 deletions heartbeat.go
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
}
41 changes: 41 additions & 0 deletions heartbeat_test.go
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
}
}
}
}

0 comments on commit f86b0de

Please sign in to comment.