Skip to content

Commit a59242a

Browse files
author
Jacob Wirth
committed
Clean up code and rename PipeRequest to PipeUpstream
1 parent be573fe commit a59242a

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

CREATING_TOXICS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Bidirectional toxics allow state to be shared for the `upstream` and `downstream
152152
toxic implementation. They also ensure direction-specific code is always run on the correct pipe
153153
(a toxic that only works on the `upstream` can't be added to the `downstream`).
154154

155-
Creating a bidirectional toxic is done by implementing a second `Pipe()` function called `PipeRequest()`.
155+
Creating a bidirectional toxic is done by implementing a second `Pipe()` function called `PipeUpstream()`.
156156
The implementation is same as a regular toxic, and can be paired with other types such as a stateful toxic.
157157

158158
One use case of a bidirectional toxic is to mock out the backend server entirely, which is shown below:
@@ -164,8 +164,8 @@ type EchoToxicState struct {
164164
Request chan *stream.StreamChunk
165165
}
166166

167-
// PipeRequest handles the upstream direction
168-
func (t *EchoToxic) PipeRequest(stub *toxics.ToxicStub) {
167+
// PipeUpstream handles the upstream direction
168+
func (t *EchoToxic) PipeUpstream(stub *toxics.ToxicStub) {
169169
state := stub.State.(*EchoToxicState)
170170

171171
for {

link.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,7 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
6767
link.input.Close()
6868
}()
6969
for i, toxic := range link.toxics.chain[link.direction] {
70-
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {
71-
if toxic.PairedToxic == nil || link.pairedLink.stubs[toxic.PairedToxic.Index].State == nil {
72-
link.stubs[i].State = stateful.NewState()
73-
} else {
74-
link.stubs[i].State = link.pairedLink.stubs[toxic.PairedToxic.Index].State
75-
link.stubs[i].Toxicity = link.pairedLink.stubs[toxic.PairedToxic.Index].Toxicity
76-
}
77-
}
70+
link.InitPairState(toxic)
7871

7972
go link.stubs[i].Run(toxic)
8073
}
@@ -93,6 +86,22 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
9386
}()
9487
}
9588

89+
func (link *ToxicLink) InitPairState(toxic *toxics.ToxicWrapper) {
90+
// If the toxic is stateful, create a state object or copy it from the paired link.
91+
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {
92+
if toxic.PairedToxic == nil || link.pairedLink.stubs[toxic.PairedToxic.Index].State == nil {
93+
link.stubs[toxic.Index].State = stateful.NewState()
94+
} else {
95+
link.stubs[toxic.Index].State = link.pairedLink.stubs[toxic.PairedToxic.Index].State
96+
}
97+
}
98+
99+
// If the toxic is paired, synchronize the toxicity so they are always in the same state.
100+
if toxic.PairedToxic != nil {
101+
link.stubs[toxic.Index].Toxicity = link.pairedLink.stubs[toxic.PairedToxic.Index].Toxicity
102+
}
103+
}
104+
96105
// Add a toxic to the end of the chain.
97106
func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) {
98107
i := len(link.stubs)
@@ -104,14 +113,7 @@ func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) {
104113
if link.stubs[i-1].InterruptToxic() {
105114
link.stubs[i-1].Output = newin
106115

107-
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {
108-
if toxic.PairedToxic == nil || link.pairedLink.stubs[toxic.PairedToxic.Index].State == nil {
109-
link.stubs[i].State = stateful.NewState()
110-
} else {
111-
link.stubs[i].State = link.pairedLink.stubs[toxic.PairedToxic.Index].State
112-
link.stubs[i].Toxicity = link.pairedLink.stubs[toxic.PairedToxic.Index].Toxicity
113-
}
114-
}
116+
link.InitPairState(toxic)
115117

116118
go link.stubs[i].Run(toxic)
117119
go link.stubs[i-1].Run(link.toxics.chain[link.direction][i-1])

toxics/bidirectional_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type EchoToxicState struct {
2323
UpstreamToxicity float32
2424
}
2525

26-
func (t *EchoToxic) PipeRequest(stub *toxics.ToxicStub) {
26+
func (t *EchoToxic) PipeUpstream(stub *toxics.ToxicStub) {
2727
state := stub.State.(*EchoToxicState)
2828
state.UpstreamToxicity = stub.Toxicity
2929

toxics/toxic.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type StatefulToxic interface {
4141
}
4242

4343
// Bidirectional toxics operate on both TCP streams and allow state to be shared.
44-
// PipeRequest() will oparate on the upstream, while Pipe() will operate on the downstream.
44+
// PipeUpstream() will oparate on the upstream, while Pipe() will operate on the downstream.
4545
type BidirectionalToxic interface {
4646
// Defines the packet flow through an upstream ToxicStub. Operates the same as Pipe().
47-
PipeRequest(*ToxicStub)
47+
PipeUpstream(*ToxicStub)
4848
}
4949

5050
type ToxicWrapper struct {
@@ -90,7 +90,7 @@ func (s *ToxicStub) Run(toxic *ToxicWrapper) {
9090
toxic.Pipe(s)
9191
} else {
9292
bidirectional := toxic.Toxic.(BidirectionalToxic)
93-
bidirectional.PipeRequest(s)
93+
bidirectional.PipeUpstream(s)
9494
}
9595
} else {
9696
new(NoopToxic).Pipe(s)

0 commit comments

Comments
 (0)