Skip to content

Commit

Permalink
Added new feature ‘pathDelays’
Browse files Browse the repository at this point in the history
Configure a delay for individual path globs! Global delay is the fallback now, indivdual delays are now configurable.

Signed-off-by: Quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Aug 24, 2023
1 parent 43f1ed1 commit 0e21778
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/booted_message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/handle_http_traffic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/load_specification.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/print_banner.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
19 changes: 18 additions & 1 deletion cmd/root_command.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down Expand Up @@ -250,6 +250,12 @@ var (
printLoadedPathConfigurations(config.PathConfigurations)
}

// path delays
if len(config.PathDelays) > 0 {
config.CompilePathDelays()
printLoadedPathDelayConfigurations(config.PathDelays)
}

// static headers
if config.Headers != nil && len(config.Headers.DropHeaders) > 0 {
pterm.Info.Printf("Dropping the following %d %s globally:\n", len(config.Headers.DropHeaders),
Expand Down Expand Up @@ -354,6 +360,17 @@ func printLoadedPathConfigurations(configs map[string]*shared.WiretapPathConfig)
}
}

func printLoadedPathDelayConfigurations(pathDelays map[string]int) {
pterm.Info.Printf("Loaded %d path %s:\n", len(pathDelays),
shared.Pluralize(len(pathDelays), "delay", "delays"))

for k, v := range pathDelays {
pterm.Printf("⏱️ %sms --> %s\n", pterm.LightCyan(v), pterm.LightMagenta(k))
}
pterm.Println()

}

func printLoadedVariables(variables map[string]string) {
pterm.Info.Printf("Loaded %d %s:\n", len(variables),
shared.Pluralize(len(variables), "variable", "variables"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/run_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/serve_monitor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package cmd

Expand Down
2 changes: 1 addition & 1 deletion config/config_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package config

Expand Down
12 changes: 11 additions & 1 deletion config/paths.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package config

Expand All @@ -19,6 +19,16 @@ func FindPaths(path string, configuration *shared.WiretapConfiguration) []*share
return foundConfigurations
}

func FindPathDelay(path string, configuration *shared.WiretapConfiguration) int {
var foundMatch int
for key := range configuration.CompiledPathDelays {
if configuration.CompiledPathDelays[key].CompiledPathDelay.Match(path) {
foundMatch = configuration.CompiledPathDelays[key].PathDelayValue
}
}
return foundMatch
}

func RewritePath(path string, configuration *shared.WiretapConfiguration) string {
paths := FindPaths(path, configuration)
var replaced string
Expand Down
28 changes: 27 additions & 1 deletion config/paths_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package config

Expand Down Expand Up @@ -188,3 +188,29 @@ paths:
assert.Equal(t, "http://localhost:80/noKetchupPlease/-/yummy/yum?onions=true", path)

}

func TestLocatePathDelay(t *testing.T) {

config := `pathDelays:
/pb33f/test/**: 1000
/pb33f/cakes/123: 2000
/*/test/123: 3000`

var c shared.WiretapConfiguration
_ = yaml.Unmarshal([]byte(config), &c)

c.CompilePathDelays()

delay := FindPathDelay("/pb33f/test/burgers/fries?1234=no", &c)
assert.Equal(t, 1000, delay)

delay = FindPathDelay("/pb33f/cakes/123", &c)
assert.Equal(t, 2000, delay)

delay = FindPathDelay("/roastbeef/test/123", &c)
assert.Equal(t, 3000, delay)

delay = FindPathDelay("/not-registered", &c)
assert.Equal(t, 0, delay)

}
2 changes: 1 addition & 1 deletion controls/controls_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package controls

Expand Down
2 changes: 1 addition & 1 deletion daemon/api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/cors_middleware.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
15 changes: 10 additions & 5 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down Expand Up @@ -188,11 +188,16 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
}
}

// send response back to client.

if config.GlobalAPIDelay > 0 {
time.Sleep(time.Duration(config.GlobalAPIDelay) * time.Millisecond) // simulate a slow response.
// check if this path has a delay set.
delay := configModel.FindPathDelay(request.HttpRequest.URL.Path, config)
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Millisecond) // simulate a slow response, configured for path.
} else {
if config.GlobalAPIDelay > 0 {
time.Sleep(time.Duration(config.GlobalAPIDelay) * time.Millisecond) // simulate a slow response.
}
}

body, _ := io.ReadAll(returnedResponse.Body)
headers := extractHeaders(returnedResponse)

Expand Down
2 changes: 1 addition & 1 deletion daemon/monitor_static.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/validate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/wiretap_broadcast.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/wiretap_init.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/wiretap_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess Beef Heavy Industries LLC
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/wiretap_utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion daemon/wiretap_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package daemon

Expand Down
2 changes: 1 addition & 1 deletion report/report_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package report

Expand Down
20 changes: 19 additions & 1 deletion shared/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package shared

Expand Down Expand Up @@ -33,6 +33,8 @@ type WiretapConfiguration struct {
HardErrors bool `json:"hardValidation,omitempty" yaml:"hardValidation,omitempty"`
HardErrorCode int `json:"hardValidationCode,omitempty" yaml:"hardValidationCode,omitempty"`
HardErrorReturnCode int `json:"hardValidationReturnCode,omitempty" yaml:"hardValidationReturnCode,omitempty"`
PathDelays map[string]int `json:"pathDelays,omitempty" yaml:"pathDelays,omitempty"`
CompiledPathDelays map[string]*CompiledPathDelay `json:"-" yaml:"-"`
CompiledVariables map[string]*CompiledVariable `json:"-" yaml:"-"`
Version string `json:"-" yaml:"-"`
StaticPathsCompiled []glob.Glob `json:"-" yaml:"-"`
Expand All @@ -54,6 +56,17 @@ func (wtc *WiretapConfiguration) CompilePaths() {
}
}

func (wtc *WiretapConfiguration) CompilePathDelays() {
wtc.CompiledPathDelays = make(map[string]*CompiledPathDelay)
for k, v := range wtc.PathDelays {
compiled := &CompiledPathDelay{
CompiledPathDelay: glob.MustCompile(wtc.ReplaceWithVariables(k)),
PathDelayValue: v,
}
wtc.CompiledPathDelays[k] = compiled
}
}

func (wtc *WiretapConfiguration) CompileVariables() {
wtc.CompiledVariables = make(map[string]*CompiledVariable)
for x := range wtc.Variables {
Expand Down Expand Up @@ -92,6 +105,11 @@ type CompiledPath struct {
CompiledPathRewrite map[string]*regexp.Regexp
}

type CompiledPathDelay struct {
CompiledPathDelay glob.Glob
PathDelayValue int
}

type CompiledVariable struct {
CompiledVariable *regexp.Regexp
VariableValue string
Expand Down
2 changes: 1 addition & 1 deletion shared/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package shared

Expand Down
2 changes: 1 addition & 1 deletion shared/language.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package shared

Expand Down
2 changes: 1 addition & 1 deletion specs/spec_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package specs

Expand Down
2 changes: 1 addition & 1 deletion wiretap.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: AGPL

package main

Expand Down

0 comments on commit 0e21778

Please sign in to comment.