-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
Added the feature of delayed packages #170
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package delay | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
type DelayRange struct { | ||
Min int `yaml:"min"` | ||
Max int `yaml:"max"` | ||
} | ||
|
||
type PortDelay struct { | ||
Port int `yaml:"port"` | ||
Min int `yaml:"min"` | ||
Max int `yaml:"max"` | ||
Protocols []string `yaml:"protocols"` | ||
} | ||
|
||
type DelayConfig struct { | ||
Global DelayRange `yaml:"global"` | ||
PerPort []PortDelay `yaml:"per_port"` | ||
} | ||
|
||
func NewDelayHandler(config DelayConfig) *DelayHandler { | ||
return &DelayHandler{config: config} | ||
} | ||
|
||
type DelayHandler struct { | ||
config DelayConfig | ||
} | ||
|
||
func (h *DelayHandler) GetDelay(port int, protocol string) time.Duration { | ||
// Check port specific delay | ||
for _, pd := range h.config.PerPort { | ||
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. can you solve this without a for loop? |
||
if pd.Port == port { | ||
for _, p := range pd.Protocols { | ||
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. can you solve this without a for loop? |
||
if p == protocol { | ||
return time.Duration(rand.Intn(pd.Max-pd.Min+1)+pd.Min) * time.Millisecond | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Fall back to global delay | ||
return time.Duration(rand.Intn(h.config.Global.Max-h.config.Global.Min+1)+h.config.Global.Min) * time.Millisecond | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package delay | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDelayHandler(t *testing.T) { | ||
config := DelayConfig{ | ||
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. use the test config |
||
Global: DelayRange{Min: 50, Max: 200}, | ||
PerPort: []PortDelay{ | ||
{ | ||
Port: 5000, | ||
Min: 100, | ||
Max: 300, | ||
Protocols: []string{"tcp"}, | ||
}, | ||
}, | ||
} | ||
|
||
handler := NewDelayHandler(config) | ||
|
||
tests := []struct { | ||
name string | ||
port int | ||
protocol string | ||
minDelay time.Duration | ||
maxDelay time.Duration | ||
}{ | ||
{ | ||
name: "TCP port 5000", | ||
port: 5000, | ||
protocol: "tcp", | ||
minDelay: 100 * time.Millisecond, | ||
maxDelay: 300 * time.Millisecond, | ||
}, | ||
{ | ||
name: "Global fallback", | ||
port: 5001, | ||
protocol: "udp", | ||
minDelay: 50 * time.Millisecond, | ||
maxDelay: 200 * time.Millisecond, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
delay := handler.GetDelay(tt.port, tt.protocol) | ||
if delay < tt.minDelay || delay > tt.maxDelay { | ||
t.Errorf("Delay %v outside expected range [%v, %v]", | ||
delay, tt.minDelay, tt.maxDelay) | ||
} | ||
Comment on lines
+49
to
+52
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. use the |
||
}) | ||
} | ||
} |
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.
Needs a doc string