From d10a921aee2af6a278f5ea5c9eae6deac39302a1 Mon Sep 17 00:00:00 2001 From: Michael Nikitochkin Date: Sat, 27 Aug 2022 15:54:46 +0200 Subject: [PATCH] Update toxiproxy.NewProxy signature to support common attributes NewProxy always uses 4 lines to initialize name, listen and upstream addresses. Combine them in single initialization via arguments. --- CHANGELOG.md | 3 ++- api.go | 5 +---- metrics_test.go | 5 +---- proxy.go | 5 ++++- proxy_collection.go | 5 +---- toxics/toxic_test.go | 6 +----- toxiproxy_test.go | 6 +----- 7 files changed, 11 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a54beb7..1e5b5cc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # [Unreleased] -* Support go 1.18, 1.19. +* Support go 1.18, 1.19. (@miry) +* `toxiproxy.NewProxy` now accepts `name`, `listen addr` and `upstream addr`. (#418, @miry) # [2.4.0] - 2022-03-07 diff --git a/api.go b/api.go index bddeb399..e5f85a4c 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 dfee5171..1f8cba20 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 8a1f8dc3..1621a965 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 f79be152..dd707770 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 78070cbd..e5539353 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 16a1f03f..75e113cb 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 }