-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batching the probes to spread the workload and more exact time readings #15
Open
fmotrifork
wants to merge
2
commits into
Lesterpig:master
Choose a base branch
from
fmotrifork:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/board | ||
/board.yaml | ||
bin/* | ||
tmp/* |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:latest AS build-env | ||
FROM golang:1.16-alpine AS build-env | ||
|
||
# Dependencies | ||
WORKDIR /build | ||
|
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,39 @@ | ||
BINDIR ?= $(CURDIR)/bin | ||
TMPDIR ?= $(CURDIR)/tmp | ||
ARCH ?= amd64 | ||
|
||
help: ## display this help | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) | ||
|
||
.PHONY: help build image all clean dev | ||
|
||
test: ## test board | ||
go test ./... | ||
|
||
dev: ## live reload development | ||
gin --path . --appPort 8080 --all --immediate --bin tmp/board run | ||
|
||
build: ## build board | ||
mkdir -p $(BINDIR) | ||
CGO_ENABLED=0 go build -o ./bin/board | ||
|
||
verify: test build ## tests and builds board | ||
|
||
image: ## build docker image | ||
docker build -t lesterpig/board:latest . | ||
|
||
clean: ## clean up created files | ||
rm -rf \ | ||
$(BINDIR) \ | ||
$(TMPDIR) | ||
|
||
all: clean test build image ## runs test, build and image | ||
|
||
test-coverage: ## Generate test coverage report | ||
mkdir -p $(TMPDIR) | ||
go test ./... --coverprofile $(TMPDIR)/outfile | ||
go tool cover -html=$(TMPDIR)/outfile | ||
|
||
lint: ## Generate static analysis report | ||
goreportcard-cli -v | ||
golint ./... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ package probe | |
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptrace" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
@@ -38,9 +40,11 @@ func (h *HTTP) Init(c Config) error { | |
} | ||
|
||
/* #nosec G402 */ | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: !opts.VerifyCertificate}, | ||
} | ||
tr := http.DefaultTransport.(*http.Transport).Clone() | ||
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the |
||
// Disable keep alive to get more consistent measurements | ||
tr.DisableKeepAlives = true | ||
tr.ResponseHeaderTimeout = c.Fatal | ||
|
||
h.client = &http.Client{ | ||
Timeout: c.Fatal, | ||
|
@@ -55,17 +59,39 @@ func (h *HTTP) Init(c Config) error { | |
// If the operation succeeds, the message will be the duration of the HTTP request in ms. | ||
// Otherwise, an error message is returned. | ||
func (h *HTTP) Probe() (status Status, message string) { | ||
start := time.Now() | ||
res, err := h.client.Get(h.Target) | ||
duration := time.Since(start) | ||
|
||
req, _ := http.NewRequest("GET", h.Target, nil) | ||
|
||
var server, start time.Time | ||
var initDuration, serverDuration, totalDuration time.Duration | ||
|
||
trace := &httptrace.ClientTrace{ | ||
|
||
TLSHandshakeDone: func(cs tls.ConnectionState, err error) { | ||
server = time.Now() | ||
initDuration = time.Since(start) | ||
}, | ||
} | ||
|
||
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) | ||
// Starting timer | ||
start = time.Now() | ||
// Set server time before request. This may be reset in TLSHandshakeDone if a TLSHandshake is preformed | ||
server = start | ||
|
||
res, err := h.client.Transport.RoundTrip(req) | ||
totalDuration = time.Since(start) | ||
serverDuration = time.Since(server) | ||
|
||
if res != nil { | ||
defer res.Body.Close() // MUST CLOSED THIS | ||
} | ||
|
||
if err != nil { | ||
return StatusError, defaultConnectErrorMsg | ||
} | ||
|
||
defer func() { _ = res.Body.Close() }() | ||
|
||
if res.StatusCode != 200 { | ||
if !(res.StatusCode >= 200 && res.StatusCode <= 399) { | ||
return StatusError, strconv.Itoa(res.StatusCode) | ||
} | ||
|
||
|
@@ -74,5 +100,20 @@ func (h *HTTP) Probe() (status Status, message string) { | |
return StatusError, "Unexpected result" | ||
} | ||
|
||
return EvaluateDuration(duration, h.Warning) | ||
return AdvancedEvaluateDuration(initDuration, serverDuration, totalDuration, h.Warning) | ||
} | ||
|
||
// AdvancedEvaluateDuration is a shortcut for warning duration checks. | ||
// It returns a message containing the duration, and a OK or a WARNING status | ||
// depending on the provided warning duration. | ||
func AdvancedEvaluateDuration(initDuration, serverDuration, duration time.Duration, warning time.Duration) (status Status, message string) { | ||
if duration >= warning { | ||
status = StatusWarning | ||
} else { | ||
status = StatusOK | ||
} | ||
|
||
message = fmt.Sprintf("%d ms / %d ms", initDuration.Nanoseconds()/1000000, serverDuration.Nanoseconds()/1000000) | ||
|
||
return | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: is there a reason to use a float for
i
?We could use integers and check with
if i % 8 == 0
.Moreover, the
i = 0
line might not be necessary (or the condition can just bei >= 8
).