-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Reachability package * Allow healtcheck route to notify reachability test * Switch serve to be non-blocking * Perform reachability test in weep serve * Fix: revert accidental package removal
- Loading branch information
Showing
4 changed files
with
113 additions
and
13 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,41 @@ | ||
package reachability | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const maxWaitTimeSeconds = 3 | ||
|
||
var c chan struct{} | ||
|
||
func init() { | ||
c = make(chan struct{}) | ||
} | ||
|
||
// Notify will signal the reachability package that some reachability test | ||
// were received by this Weep instance | ||
func Notify() { | ||
// Only sends when there is already some receiver waiting. Never blocks. | ||
select { | ||
case c <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
func wait() bool { | ||
timeout := make(chan struct{}) | ||
go func() { | ||
time.Sleep(maxWaitTimeSeconds * time.Second) | ||
timeout <- struct{}{} | ||
}() | ||
|
||
select { | ||
case <-c: | ||
// Received a rechability test | ||
return true | ||
case <-timeout: | ||
// Timed out, move on | ||
} | ||
|
||
return false | ||
} |
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,37 @@ | ||
package reachability | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/netflix/weep/pkg/logging" | ||
) | ||
|
||
// TestReachability sends a GET request to the address IMDS is expected to run, while checks | ||
// whether this test was received by this same Weep instance, otherwise logs a warning | ||
func TestReachability() { | ||
go func() { | ||
logging.Log.Debug("Doing a healthcheck request on 169.254.169.254") | ||
resp, err := http.Get("http://169.254.169.254/healthcheck?reachability=1") | ||
|
||
// A response can be successful but have being served by another process on the | ||
// IMDS port/an actual IMDS. So we prefer relying on the reachability signal (which | ||
// means this same process received a reachability test). | ||
|
||
if err != nil { | ||
logging.Log.WithField("err", err).Debug("Received an error from healthcheck route") | ||
} else { | ||
logging.Log.WithField("status", resp.StatusCode).Debug("Received a response from healthcheck route") | ||
} | ||
}() | ||
|
||
received := wait() | ||
if received { | ||
logging.Log.Info("Reachability test was successful") | ||
} else { | ||
logging.Log.Warningf( | ||
"Reachability test was unsuccessful. Looks like we aren't being served in 169.254.169.254. Did you `%s setup`?", | ||
os.Args[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