1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
package toxiproxy
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"github.com/Shopify/toxiproxy/stream"
"github.com/Shopify/toxiproxy/toxics"
)
type ToxicCollection struct {
sync.Mutex
noop *toxics.ToxicWrapper
proxy *Proxy
chain [][]*toxics.ToxicWrapper
toxics [][]*toxics.ToxicWrapper
links map[string]*ToxicLink
}
func NewToxicCollection(proxy *Proxy) *ToxicCollection {
collection := &ToxicCollection{
noop: &toxics.ToxicWrapper{
Toxic: new(toxics.NoopToxic),
Type: "noop",
},
proxy: proxy,
chain: make([][]*toxics.ToxicWrapper, stream.NumDirections),
toxics: make([][]*toxics.ToxicWrapper, stream.NumDirections),
links: make(map[string]*ToxicLink),
}
for dir := range collection.chain {
collection.chain[dir] = make([]*toxics.ToxicWrapper, 1, toxics.Count()+1)
collection.chain[dir][0] = collection.noop
collection.toxics[dir] = make([]*toxics.ToxicWrapper, 0, toxics.Count())
}
return collection
}
func (c *ToxicCollection) ResetToxics() {
c.Lock()
defer c.Unlock()
for dir := range c.toxics {
for _, toxic := range c.toxics[dir] {
// TODO do this in bulk
c.chainRemoveToxic(toxic)
}
c.toxics[dir] = c.toxics[dir][:0]
}
}
func (c *ToxicCollection) GetToxic(name string) *toxics.ToxicWrapper {
c.Lock()
defer c.Unlock()
for dir := range c.toxics {
for _, toxic := range c.toxics[dir] {
if toxic.Name == name {
return toxic
}
}
}
return nil
}
func (c *ToxicCollection) GetToxicArray() []toxics.Toxic {
c.Lock()
defer c.Unlock()
result := make([]toxics.Toxic, 0)
for dir := range c.toxics {
for _, toxic := range c.toxics[dir] {
result = append(result, toxic)
}
}
return result
}
func (c *ToxicCollection) AddToxicJson(data io.Reader) (*toxics.ToxicWrapper, error) {
c.Lock()
defer c.Unlock()
var buffer bytes.Buffer
// Default to a downstream toxic with a toxicity of 1.
wrapper := &toxics.ToxicWrapper{
Stream: "downstream",
Toxicity: 1.0,
Toxic: new(toxics.NoopToxic),
}
err := json.NewDecoder(io.TeeReader(data, &buffer)).Decode(wrapper)
if err != nil {
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 wrapper.Name == "" {
wrapper.Name = fmt.Sprintf("%s_%s", wrapper.Type, wrapper.Stream)
}
if toxics.New(wrapper) == nil {
return nil, ErrInvalidToxicType
}
for dir := range c.toxics {
for _, toxic := range c.toxics[dir] {
if toxic.Name == wrapper.Name {
return nil, ErrToxicAlreadyExists
}
}
}
// Parse attributes because we now know the toxics type.
attrs := &struct {
Attributes interface{} `json:"attributes"`
}{
wrapper.Toxic,
}
err = json.NewDecoder(&buffer).Decode(attrs)
if err != nil {
return nil, joinError(err, ErrBadRequestBody)
}
c.toxics[wrapper.Direction] = append(c.toxics[wrapper.Direction], wrapper)
c.chainAddToxic(wrapper)
return wrapper, nil
}
func (c *ToxicCollection) UpdateToxicJson(name string, data io.Reader) (*toxics.ToxicWrapper, error) {
c.Lock()
defer c.Unlock()
for dir := range c.toxics {
for _, toxic := range c.toxics[dir] {
if toxic.Name == name {
attrs := &struct {
Attributes interface{} `json:"attributes"`
Toxicity float32 `json:"toxicity"`
}{
toxic.Toxic,
toxic.Toxicity,
}
err := json.NewDecoder(data).Decode(attrs)
if err != nil {
return nil, joinError(err, ErrBadRequestBody)
}
toxic.Toxicity = attrs.Toxicity
c.chainUpdateToxic(toxic)
return toxic, nil
}
}
}
return nil, ErrToxicNotFound
}
func (c *ToxicCollection) RemoveToxic(name string) error {
c.Lock()
defer c.Unlock()
for dir := range c.toxics {
for index, toxic := range c.toxics[dir] {
if toxic.Name == name {
c.toxics[dir] = append(c.toxics[dir][:index], c.toxics[dir][index+1:]...)
c.chainRemoveToxic(toxic)
return nil
}
}
}
return ErrToxicNotFound
}
func (c *ToxicCollection) StartLink(name string, input io.Reader, output io.WriteCloser, direction stream.Direction) {
c.Lock()
defer c.Unlock()
link := NewToxicLink(c.proxy, c, direction)
link.Start(name, input, output)
c.links[name] = link
}
func (c *ToxicCollection) RemoveLink(name string) {
c.Lock()
defer c.Unlock()
delete(c.links, name)
}
// All following functions assume the lock is already grabbed
func (c *ToxicCollection) chainAddToxic(toxic *toxics.ToxicWrapper) {
dir := toxic.Direction
toxic.Index = len(c.chain[dir])
c.chain[dir] = append(c.chain[dir], toxic)
// Asynchronously add the toxic to each link
group := sync.WaitGroup{}
for _, link := range c.links {
if link.direction == dir {
group.Add(1)
go func(link *ToxicLink) {
defer group.Done()
link.AddToxic(toxic)
}(link)
}
}
group.Wait()
}
func (c *ToxicCollection) chainUpdateToxic(toxic *toxics.ToxicWrapper) {
c.chain[toxic.Direction][toxic.Index] = toxic
// Asynchronously update the toxic in each link
group := sync.WaitGroup{}
for _, link := range c.links {
if link.direction == toxic.Direction {
group.Add(1)
go func(link *ToxicLink) {
defer group.Done()
link.UpdateToxic(toxic)
}(link)
}
}
group.Wait()
}
func (c *ToxicCollection) chainRemoveToxic(toxic *toxics.ToxicWrapper) {
dir := toxic.Direction
c.chain[dir] = append(c.chain[dir][:toxic.Index], c.chain[dir][toxic.Index+1:]...)
for i := toxic.Index; i < len(c.chain[dir]); i++ {
c.chain[dir][i].Index = i
}
// Asynchronously remove the toxic from each link
group := sync.WaitGroup{}
for _, link := range c.links {
if link.direction == dir {
group.Add(1)
go func(link *ToxicLink) {
defer group.Done()
link.RemoveToxic(toxic)
}(link)
}
}
group.Wait()
toxic.Index = -1
}
|