Skip to content

Commit

Permalink
Update toxiproxy.NewProxy signature to support common attributes
Browse files Browse the repository at this point in the history
NewProxy always uses 4 lines to initialize name, listen and upstream addresses.
Combine them in single initialization via arguments.
  • Loading branch information
miry committed Aug 27, 2022
1 parent cfd4ac5 commit 3c8c029
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 1 addition & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 4 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions proxy_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions toxics/toxic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions toxiproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3c8c029

Please sign in to comment.