-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
73 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
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,9 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type Heartbeat interface { | ||
Beat(context.Context) error | ||
} |
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,46 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const discardLimit int64 = 128 * 1024 | ||
|
||
type URLHeartbeat struct { | ||
url string | ||
} | ||
|
||
func NewURLHeartbeat(url string) *URLHeartbeat { | ||
return &URLHeartbeat{ | ||
url: url, | ||
} | ||
} | ||
|
||
func (b *URLHeartbeat) Beat(ctx context.Context) error { | ||
req, err := http.NewRequestWithContext(ctx, "GET", b.url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer cleanupBody(resp.Body) | ||
|
||
return nil | ||
} | ||
|
||
// Does cleanup of HTTP response in order to make it reusable by keep-alive | ||
// logic of HTTP client | ||
func cleanupBody(body io.ReadCloser) { | ||
io.Copy(ioutil.Discard, &io.LimitedReader{ | ||
R: body, | ||
N: discardLimit, | ||
}) | ||
body.Close() | ||
} |