@@ -67,14 +67,7 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
67
67
link .input .Close ()
68
68
}()
69
69
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 )
78
71
79
72
go link .stubs [i ].Run (toxic )
80
73
}
@@ -93,6 +86,22 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
93
86
}()
94
87
}
95
88
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
+
96
105
// Add a toxic to the end of the chain.
97
106
func (link * ToxicLink ) AddToxic (toxic * toxics.ToxicWrapper ) {
98
107
i := len (link .stubs )
@@ -104,14 +113,7 @@ func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) {
104
113
if link .stubs [i - 1 ].InterruptToxic () {
105
114
link .stubs [i - 1 ].Output = newin
106
115
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 )
115
117
116
118
go link .stubs [i ].Run (toxic )
117
119
go link .stubs [i - 1 ].Run (link .toxics .chain [link .direction ][i - 1 ])
0 commit comments