-
Notifications
You must be signed in to change notification settings - Fork 456
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 Bidirectional Toxics #132
base: dev
Are you sure you want to change the base?
Changes from all commits
2475d8c
2989b17
e82d648
2dc6830
ec964a0
c75bec4
0e85ee6
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 |
---|---|---|
|
@@ -18,12 +18,13 @@ import ( | |
// Input > ToxicStub > ToxicStub > Output | ||
// | ||
type ToxicLink struct { | ||
stubs []*toxics.ToxicStub | ||
proxy *Proxy | ||
toxics *ToxicCollection | ||
input *stream.ChanWriter | ||
output *stream.ChanReader | ||
direction stream.Direction | ||
stubs []*toxics.ToxicStub | ||
proxy *Proxy | ||
toxics *ToxicCollection | ||
input *stream.ChanWriter | ||
output *stream.ChanReader | ||
direction stream.Direction | ||
pairedLink *ToxicLink | ||
} | ||
|
||
func NewToxicLink(proxy *Proxy, collection *ToxicCollection, direction stream.Direction) *ToxicLink { | ||
|
@@ -66,9 +67,7 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser) | |
link.input.Close() | ||
}() | ||
for i, toxic := range link.toxics.chain[link.direction] { | ||
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok { | ||
link.stubs[i].State = stateful.NewState() | ||
} | ||
link.InitPairState(toxic) | ||
|
||
go link.stubs[i].Run(toxic) | ||
} | ||
|
@@ -87,6 +86,22 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser) | |
}() | ||
} | ||
|
||
func (link *ToxicLink) InitPairState(toxic *toxics.ToxicWrapper) { | ||
// If the toxic is stateful, create a state object or copy it from the paired link. | ||
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok { | ||
if toxic.PairedToxic == nil || link.pairedLink.stubs[toxic.PairedToxic.Index].State == nil { | ||
link.stubs[toxic.Index].State = stateful.NewState() | ||
} else { | ||
link.stubs[toxic.Index].State = link.pairedLink.stubs[toxic.PairedToxic.Index].State | ||
} | ||
} | ||
|
||
// If the toxic is paired, synchronize the toxicity so they are always in the same state. | ||
if toxic.PairedToxic != nil { | ||
link.stubs[toxic.Index].Toxicity = link.pairedLink.stubs[toxic.PairedToxic.Index].Toxicity | ||
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. If toxicity is always initialized to the paired toxic's toxicity, where is the value actually configured? |
||
} | ||
} | ||
|
||
// Add a toxic to the end of the chain. | ||
func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) { | ||
i := len(link.stubs) | ||
|
@@ -98,9 +113,7 @@ func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) { | |
if link.stubs[i-1].InterruptToxic() { | ||
link.stubs[i-1].Output = newin | ||
|
||
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok { | ||
link.stubs[i].State = stateful.NewState() | ||
} | ||
link.InitPairState(toxic) | ||
|
||
go link.stubs[i].Run(toxic) | ||
go link.stubs[i-1].Run(link.toxics.chain[link.direction][i-1]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
"strings" | ||
"sync" | ||
|
||
|
@@ -61,18 +62,17 @@ func (c *ToxicCollection) GetToxic(name string) *toxics.ToxicWrapper { | |
return c.findToxicByName(name) | ||
} | ||
|
||
func (c *ToxicCollection) GetToxicArray() []toxics.Toxic { | ||
func (c *ToxicCollection) GetToxicArray() []*toxics.ToxicWrapper { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
result := make([]toxics.Toxic, 0) | ||
result := make([]*toxics.ToxicWrapper, 0) | ||
for dir := range c.chain { | ||
for i, toxic := range c.chain[dir] { | ||
if i == 0 { | ||
// Skip the first noop toxic, it should not be visible | ||
continue | ||
for _, toxic := range c.chain[dir] { | ||
if len(toxic.Name) > 0 { | ||
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. Maybe we should have a |
||
// Skip toxics with no name, they should be hidden | ||
result = append(result, toxic) | ||
} | ||
result = append(result, toxic) | ||
} | ||
} | ||
return result | ||
|
@@ -96,20 +96,31 @@ func (c *ToxicCollection) AddToxicJson(data io.Reader) (*toxics.ToxicWrapper, er | |
return nil, joinError(err, ErrBadRequestBody) | ||
} | ||
|
||
switch strings.ToLower(wrapper.Stream) { | ||
case "downstream": | ||
wrapper.Direction = stream.Downstream | ||
case "upstream": | ||
wrapper.Direction = stream.Upstream | ||
default: | ||
return nil, ErrInvalidStream | ||
if toxics.New(wrapper) == nil { | ||
return nil, ErrInvalidToxicType | ||
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. Is |
||
} | ||
if wrapper.Name == "" { | ||
wrapper.Name = fmt.Sprintf("%s_%s", wrapper.Type, wrapper.Stream) | ||
|
||
if wrapper.PairedToxic == nil { | ||
switch strings.ToLower(wrapper.Stream) { | ||
case "downstream": | ||
wrapper.Direction = stream.Downstream | ||
case "upstream": | ||
wrapper.Direction = stream.Upstream | ||
default: | ||
return nil, ErrInvalidStream | ||
} | ||
} else { | ||
wrapper.Stream = "both" | ||
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. I'm not sure what the behaviour should be here since there's only one option for bidirectional toxics. |
||
wrapper.Direction = stream.Downstream | ||
wrapper.PairedToxic.Direction = stream.Upstream | ||
} | ||
|
||
if toxics.New(wrapper) == nil { | ||
return nil, ErrInvalidToxicType | ||
if wrapper.Name == "" { | ||
if wrapper.PairedToxic != nil { | ||
wrapper.Name = wrapper.Type | ||
} else { | ||
wrapper.Name = fmt.Sprintf("%s_%s", wrapper.Type, wrapper.Stream) | ||
} | ||
} | ||
|
||
found := c.findToxicByName(wrapper.Name) | ||
|
@@ -129,6 +140,9 @@ func (c *ToxicCollection) AddToxicJson(data io.Reader) (*toxics.ToxicWrapper, er | |
} | ||
|
||
c.chainAddToxic(wrapper) | ||
if wrapper.PairedToxic != nil { | ||
c.chainAddToxic(wrapper.PairedToxic) | ||
} | ||
return wrapper, nil | ||
} | ||
|
||
|
@@ -151,6 +165,10 @@ func (c *ToxicCollection) UpdateToxicJson(name string, data io.Reader) (*toxics. | |
} | ||
toxic.Toxicity = attrs.Toxicity | ||
|
||
if toxic.PairedToxic != nil { | ||
toxic.PairedToxic.Toxicity = attrs.Toxicity | ||
c.chainUpdateToxic(toxic.PairedToxic) | ||
} | ||
c.chainUpdateToxic(toxic) | ||
return toxic, nil | ||
} | ||
|
@@ -163,19 +181,30 @@ func (c *ToxicCollection) RemoveToxic(name string) error { | |
|
||
toxic := c.findToxicByName(name) | ||
if toxic != nil { | ||
if toxic.PairedToxic != nil { | ||
c.chainRemoveToxic(toxic.PairedToxic) | ||
} | ||
c.chainRemoveToxic(toxic) | ||
return nil | ||
} | ||
return ErrToxicNotFound | ||
} | ||
|
||
func (c *ToxicCollection) StartLink(name string, input io.Reader, output io.WriteCloser, direction stream.Direction) { | ||
func (c *ToxicCollection) StartLinks(name string, client, upstream net.Conn) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
link := NewToxicLink(c.proxy, c, direction) | ||
link.Start(name, input, output) | ||
c.links[name] = link | ||
linkUp := NewToxicLink(c.proxy, c, stream.Upstream) | ||
linkDown := NewToxicLink(c.proxy, c, stream.Downstream) | ||
|
||
linkUp.pairedLink = linkDown | ||
linkDown.pairedLink = linkUp | ||
|
||
linkUp.Start(name+"upstream", client, upstream) | ||
linkDown.Start(name+"downstream", upstream, client) | ||
|
||
c.links[name+"upstream"] = linkUp | ||
c.links[name+"downstream"] = linkDown | ||
} | ||
|
||
func (c *ToxicCollection) RemoveLink(name string) { | ||
|
@@ -187,12 +216,8 @@ func (c *ToxicCollection) RemoveLink(name string) { | |
// All following functions assume the lock is already grabbed | ||
func (c *ToxicCollection) findToxicByName(name string) *toxics.ToxicWrapper { | ||
for dir := range c.chain { | ||
for i, toxic := range c.chain[dir] { | ||
if i == 0 { | ||
// Skip the first noop toxic, it has no name | ||
continue | ||
} | ||
if toxic.Name == name { | ||
for _, toxic := range c.chain[dir] { | ||
if len(toxic.Name) > 0 && toxic.Name == name { | ||
return toxic | ||
} | ||
} | ||
|
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.
Why did we change from Toxic to ToxicWrapper?