Skip to content

Commit

Permalink
fix: latency req timeout calc (#40)
Browse files Browse the repository at this point in the history
* chore: adjust logging levels and msgs

* test: add run success test with multiple targets

* fix: calc timeout in secs and pull out of go routine

* chore: ignore tmp dir
  • Loading branch information
lvlcn-t authored Dec 12, 2023
1 parent 759b5ca commit 7f408eb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ gen

# test configs for debug launch configuration
.vscode/config

# Temporary directory
.tmp/*
18 changes: 12 additions & 6 deletions pkg/checks/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package checks

import (
"context"
"fmt"
"io"
"net/http"
"sync"
Expand Down Expand Up @@ -49,12 +50,15 @@ type LatencyResult struct {
}

func (l *Latency) Run(ctx context.Context) error {
log := logger.FromContext(ctx).WithGroup("Latency")
log.Info(l.cfg.Interval.String())
ctx, cancel := logger.NewContextWithLogger(ctx, "latency")
defer cancel()
log := logger.FromContext(ctx)
log.Info(fmt.Sprintf("Using latency check interval of %s", l.cfg.Interval.String()))

for {
select {
case <-ctx.Done():
log.Error("context canceled", "err", ctx.Err())
log.Error("Context canceled", "err", ctx.Err())
return ctx.Err()
case <-l.done:
return nil
Expand Down Expand Up @@ -137,6 +141,9 @@ func (l *Latency) check(ctx context.Context) map[string]LatencyResult {
var wg sync.WaitGroup
results := map[string]LatencyResult{}

l.mu.Lock()
l.client.Timeout = l.cfg.Timeout * time.Second
l.mu.Unlock()
for _, tar := range l.cfg.Targets {
target := tar
wg.Add(1)
Expand All @@ -146,10 +153,10 @@ func (l *Latency) check(ctx context.Context) map[string]LatencyResult {
lo.Debug("Starting retry routine to get latency", "target", target)

err := helper.Retry(func(ctx context.Context) error {
lo.Debug("Getting latency", "timing out in", l.client.Timeout.String())
res := getLatency(ctx, l.client, target)
mu.Lock()
defer mu.Unlock()
l.client.Timeout = l.cfg.Timeout
res := getLatency(ctx, l.client, target)
results[target] = res
return nil
}, l.cfg.Retry)(ctx)
Expand All @@ -160,7 +167,6 @@ func (l *Latency) check(ctx context.Context) map[string]LatencyResult {
}
wg.Wait()

log.Info("Successfully got latency to all targets")
return results
}

Expand Down
39 changes: 37 additions & 2 deletions pkg/checks/latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func TestLatency_Run(t *testing.T) {
want Result
}{
{
name: "runs successfully a latency check",
name: "success with one target",
registeredEndpoints: []struct {
name string
status int
success bool
}{
{
name: successURL,
status: 200,
status: http.StatusOK,
success: true,
},
},
Expand All @@ -62,6 +62,41 @@ func TestLatency_Run(t *testing.T) {
Err: "",
},
},
{
name: "success with multiple targets",
registeredEndpoints: []struct {
name string
status int
success bool
}{
{
name: successURL,
status: http.StatusOK,
success: true,
},
{
name: failURL,
status: http.StatusInternalServerError,
success: true,
},
{
name: timeoutURL,
status: 0,
success: false,
},
},
targets: []string{successURL, failURL, timeoutURL},
ctx: context.Background(),
want: Result{
Data: map[string]LatencyResult{
successURL: {Code: http.StatusOK, Error: nil, Total: 0},
failURL: {Code: http.StatusInternalServerError, Error: nil, Total: 0},
timeoutURL: {Code: 0, Error: stringPointer(fmt.Sprintf("Get %q: context deadline exceeded", timeoutURL)), Total: 0},
},
Timestamp: time.Time{},
Err: "",
},
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *Sparrow) api(ctx context.Context) error {
// run http server in goroutine
go func(cErr chan error) {
defer close(cErr)
log.Info("serving api", "addr", s.cfg.Api.ListeningAddress)
log.Info("Serving Api", "addr", s.cfg.Api.ListeningAddress)
if err := server.ListenAndServe(); err != nil {
log.Error("Failed to serve api", "error", err)
cErr <- err
Expand Down

0 comments on commit 7f408eb

Please sign in to comment.