-
Notifications
You must be signed in to change notification settings - Fork 454
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
Add toxic slicer #66
Merged
Merged
Add toxic slicer #66
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7842215
Add toxic slicer
connor4312 416b3de
Adjust from PR comments
connor4312 08af0c1
Send the rest of the data when we get an interrupt
connor4312 fce5e1a
Actual fair offset
connor4312 44566b7
Simpler append
connor4312 f3c3d73
update changelog
connor4312 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
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// The SlicerToxic slices data into multiple smaller packets | ||
// to simulate real-world TCP behaviour. | ||
type SlicerToxic struct { | ||
Enabled bool `json:"enabled"` | ||
// Average number of bytes to slice at | ||
AverageSize int `json:"average_size"` | ||
// +/- bytes to vary sliced amounts. Must be less than | ||
// the average size | ||
SizeVariation int `json:"size_variation"` | ||
// Microseconds to delay each packet. May be useful since there's | ||
// usually some kind of buffering of network data | ||
Delay int `json:"delay"` | ||
} | ||
|
||
func (t *SlicerToxic) Name() string { | ||
return "slicer" | ||
} | ||
|
||
func (t *SlicerToxic) IsEnabled() bool { | ||
return t.Enabled | ||
} | ||
|
||
func (t *SlicerToxic) SetEnabled(enabled bool) { | ||
t.Enabled = enabled | ||
} | ||
|
||
// Returns a list of chunk offsets to slice up a packet of the | ||
// given total size. For example, for a size of 100, output might be: | ||
// | ||
// []int{0, 18, 18, 43, 43, 67, 67, 77, 77, 100} | ||
// ^---^ ^----^ ^----^ ^----^ ^-----^ | ||
// | ||
// This tries to get fairly evenly-varying chunks (no tendency | ||
// to have a small/large chunk at the start/end). | ||
func (t *SlicerToxic) chunk(start int, end int) []int { | ||
// Base case: | ||
// If the size is within the random varation, _or already | ||
// less than the average size_, just return it. | ||
// Otherwise split the chunk in about two, and recurse. | ||
if (end-start)-t.AverageSize <= t.SizeVariation { | ||
return []int{start, end} | ||
} | ||
|
||
// +1 in the size variation to offset favoring of smaller | ||
// numbers by integer division | ||
mid := start + (end-start)/2 + (rand.Intn(t.SizeVariation*2) - t.SizeVariation) + rand.Intn(1) | ||
left := t.chunk(start, mid) | ||
right := t.chunk(mid, end) | ||
|
||
return append(left, right...) | ||
} | ||
|
||
func (t *SlicerToxic) Pipe(stub *ToxicStub) { | ||
for { | ||
select { | ||
case <-stub.interrupt: | ||
return | ||
case c := <-stub.input: | ||
if c == nil { | ||
stub.Close() | ||
return | ||
} | ||
|
||
chunks := t.chunk(0, len(c.data)) | ||
for i := 1; i < len(chunks); i += 2 { | ||
stub.output <- &StreamChunk{ | ||
data: c.data[chunks[i-1]:chunks[i]], | ||
timestamp: c.timestamp, | ||
} | ||
|
||
select { | ||
case <-stub.interrupt: | ||
stub.output <- &StreamChunk{ | ||
data: c.data[chunks[i]:], | ||
timestamp: c.timestamp, | ||
} | ||
return | ||
case <-time.After(time.Duration(t.Delay) * time.Microsecond): | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
If this toxic is interrupted at this point in the loop, the remainder of
c
will be dropped. In this case we would want to just send off the remainder like in the latency toxic.Toxics can be interrupted without the stream closing, so we need to make sure it's in a usable state.
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.
Fixed