diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a54beb78..cd8713569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # [Unreleased] * Support go 1.18, 1.19. +* `toxiproxy.NewProxy` now accepts `name`, `listen addr` and `upstream addr`. # [2.4.0] - 2022-03-07 diff --git a/api.go b/api.go index bddeb3992..e5f85a4ce 100644 --- a/api.go +++ b/api.go @@ -151,10 +151,7 @@ func (server *ApiServer) ProxyCreate(response http.ResponseWriter, request *http return } - proxy := NewProxy(server) - proxy.Name = input.Name - proxy.Listen = input.Listen - proxy.Upstream = input.Upstream + proxy := NewProxy(server, input.Name, input.Listen, input.Upstream) err = server.Collection.Add(proxy, input.Enabled) if apiError(response, err) { diff --git a/metrics_test.go b/metrics_test.go index dfee51718..1f8cba20b 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -19,10 +19,7 @@ func TestProxyMetricsReceivedSentBytes(t *testing.T) { srv := NewServer(NewMetricsContainer(prometheus.NewRegistry())) srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors() - proxy := NewProxy(srv) - proxy.Name = "test_proxy_metrics_received_sent_bytes" - proxy.Listen = "localhost:0" - proxy.Upstream = "upstream" + proxy := NewProxy(srv, "test_proxy_metrics_received_sent_bytes", "localhost:0", "upstream") r := bufio.NewReader(bytes.NewBufferString("hello")) w := &testWriteCloser{ diff --git a/proxy.go b/proxy.go index 8a1f8dc33..1621a9651 100644 --- a/proxy.go +++ b/proxy.go @@ -48,8 +48,11 @@ func (c *ConnectionList) Unlock() { var ErrProxyAlreadyStarted = errors.New("Proxy already started") -func NewProxy(server *ApiServer) *Proxy { +func NewProxy(server *ApiServer, name, listen, upstream string) *Proxy { proxy := &Proxy{ + Name: name, + Listen: listen, + Upstream: upstream, started: make(chan error), connections: ConnectionList{list: make(map[string]net.Conn)}, apiServer: server, diff --git a/proxy_collection.go b/proxy_collection.go index f79be1527..dd7077700 100644 --- a/proxy_collection.go +++ b/proxy_collection.go @@ -97,10 +97,7 @@ func (collection *ProxyCollection) PopulateJson( proxies := make([]*Proxy, 0, len(input)) for i := range input { - proxy := NewProxy(server) - proxy.Name = input[i].Name - proxy.Listen = input[i].Listen - proxy.Upstream = input[i].Upstream + proxy := NewProxy(server, input[i].Name, input[i].Listen, input[i].Upstream) err = collection.AddOrReplace(proxy, *input[i].Enabled) if err != nil { diff --git a/toxics/toxic_test.go b/toxics/toxic_test.go index 78070cbde..e55393537 100644 --- a/toxics/toxic_test.go +++ b/toxics/toxic_test.go @@ -25,11 +25,7 @@ func init() { func NewTestProxy(name, upstream string) *toxiproxy.Proxy { srv := toxiproxy.NewServer(toxiproxy.NewMetricsContainer(prometheus.NewRegistry())) srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors() - proxy := toxiproxy.NewProxy(srv) - - proxy.Name = name - proxy.Listen = "localhost:0" - proxy.Upstream = upstream + proxy := toxiproxy.NewProxy(srv, name, "localhost:0", upstream) return proxy } diff --git a/toxiproxy_test.go b/toxiproxy_test.go index 16a1f03f7..75e113cbd 100644 --- a/toxiproxy_test.go +++ b/toxiproxy_test.go @@ -13,11 +13,7 @@ import ( func NewTestProxy(name, upstream string) *toxiproxy.Proxy { srv := toxiproxy.NewServer(toxiproxy.NewMetricsContainer(prometheus.NewRegistry())) srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors() - proxy := toxiproxy.NewProxy(srv) - - proxy.Name = name - proxy.Listen = "localhost:0" - proxy.Upstream = upstream + proxy := toxiproxy.NewProxy(srv, name, "localhost:0", upstream) return proxy }