Skip to content

Commit a5853c2

Browse files
authored
Merge pull request #16 from CoderCookE/ec/stats
Add prometheus stats
2 parents e88229a + 8ae2db5 commit a5853c2

File tree

12 files changed

+255
-117
lines changed

12 files changed

+255
-117
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ sudo ./bin/goaround -p 443 -b http://127.0.0.1:2702 -cacert /Users/ecook/cacert.
3636
-n max number of connections per backend
3737
-cacert location of certficate authority cert
3838
-privkey location of private key
39+
-cache enabled cache for get requests
40+
-prometheus-port defaults to 8080
3941
```
4042

4143
## Updating backends via unix socket
@@ -52,8 +54,8 @@ if no flag is passed the service will default to port 3000.
5254

5355
If you pass both a `cacert` and `privkey`, the server will terminate ssl connections.
5456

55-
Backend services are passed via `-b` flags, each backend passed will created a [connection](internal/connection-pool/connection),
56-
which are managed by a [pool](internal/connection-pool/connection). The `connections` are pushed into a buffered channel
57+
Backend services are passed via `-b` flags, each backend passed will created a [connection](internal/connection/main),
58+
which are managed by a [pool](internal/pool/main). The `connections` are pushed into a buffered channel
5759
where they are retrieved when the `Fetch` method is called on the `pool`. The `Fetch` method will recursively pull connections
5860
from the channel until a request is completed successfully, or we run out of available connections.
5961

bin/shasum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
07146cbafc06b4f95eed8425800450ef2ba87c4f ./bin/goaround
1+
f4a5f0d8fcb88d86492e41255eb1054e75231b81 ./bin/goaround

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ go 1.13
44

55
require (
66
github.com/OneOfOne/xxhash v1.2.7 // indirect
7-
github.com/davecgh/go-spew v1.1.1 // indirect
87
github.com/dgraph-io/ristretto v0.0.2-0.20200130184421-ff325ada232b
98
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
109
github.com/kr/pretty v0.2.0 // indirect
10+
github.com/prometheus/client_golang v1.4.1
1111
github.com/spaolacci/murmur3 v1.1.0 // indirect
12-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1312
gopkg.in/yaml.v2 v2.2.8 // indirect
1413
)

go.sum

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
22
github.com/OneOfOne/xxhash v1.2.7 h1:fzrmmkskv067ZQbd9wERNGuxckWw67dyzoMG62p7LMo=
33
github.com/OneOfOne/xxhash v1.2.7/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
4+
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
6+
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
7+
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
8+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
9+
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
10+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
11+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
412
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
513
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
14+
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
15+
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
616
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
717
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
818
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -11,22 +21,91 @@ github.com/dgraph-io/ristretto v0.0.2-0.20200130184421-ff325ada232b/go.mod h1:KP
1121
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
1222
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
1323
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
24+
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
25+
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
26+
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
27+
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
28+
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
29+
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
30+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
31+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
32+
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
33+
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
34+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
35+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
36+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
37+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
38+
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
39+
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
40+
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
41+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
42+
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
43+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1444
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
1545
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1646
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1747
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1848
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
49+
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
50+
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
51+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
52+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
53+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
54+
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
55+
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
56+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
57+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1958
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2059
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
60+
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
61+
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
62+
github.com/prometheus/client_golang v1.4.1 h1:FFSuS004yOQEtDdTq+TAOLP5xUq63KqAFYyOi8zA+Y8=
63+
github.com/prometheus/client_golang v1.4.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
64+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
65+
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
66+
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
67+
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
68+
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
69+
github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U=
70+
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
71+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
72+
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
73+
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
74+
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
75+
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
76+
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
2177
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
2278
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
2379
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
2480
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
81+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
82+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
83+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2584
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2685
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
86+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
87+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
88+
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
89+
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
90+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
91+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
92+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
93+
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
94+
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
95+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
96+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
97+
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
98+
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
99+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
100+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
101+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
102+
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
27103
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
28104
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
29105
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
106+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30107
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
108+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
109+
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
31110
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
32111
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/connection/main.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,73 @@ package connection
22

33
import (
44
"errors"
5-
"net/http"
65
"net/http/httputil"
76
"sync"
87

9-
"github.com/dgraph-io/ristretto"
8+
"github.com/CoderCookE/goaround/internal/stats"
109
)
1110

1211
type Message struct {
13-
Health bool
14-
Backend string
15-
Proxy *httputil.ReverseProxy
16-
Ack *sync.WaitGroup
12+
Health bool
13+
Backend string
14+
Proxy *httputil.ReverseProxy
15+
Ack *sync.WaitGroup
16+
Shutdown bool
1717
}
1818

1919
type Connection struct {
2020
healthy bool
21+
Shut bool
2122
Messages chan Message
2223
Backend string
2324
sync.RWMutex
2425
proxy *httputil.ReverseProxy
25-
cache *ristretto.Cache
2626
}
2727

28-
func NewConnection(proxy *httputil.ReverseProxy, backend string, cache *ristretto.Cache, startup *sync.WaitGroup) *Connection {
28+
func NewConnection(proxy *httputil.ReverseProxy, backend string, startup *sync.WaitGroup) *Connection {
2929
conn := &Connection{
3030
Backend: backend,
3131
Messages: make(chan Message),
3232
proxy: proxy,
33-
cache: cache,
3433
}
3534

3635
go conn.healthCheck()
37-
3836
startup.Done()
3937

4038
return conn
4139
}
4240

43-
func (c *Connection) Get(w http.ResponseWriter, r *http.Request) error {
44-
c.RLock()
45-
defer c.RUnlock()
41+
func (c *Connection) Get() (*httputil.ReverseProxy, error) {
42+
c.Lock()
43+
defer c.Unlock()
4644

4745
health := c.healthy
48-
err := errors.New("Unhealthy Node")
49-
if c.cache != nil && r.Method == "GET" {
50-
value, found := c.cache.Get(r.URL.Path)
51-
if found {
52-
res := value.(string)
53-
w.Write([]byte(res))
54-
55-
return nil
56-
}
57-
}
58-
59-
if health {
60-
c.proxy.ServeHTTP(w, r)
61-
return nil
46+
if health && !c.Shut {
47+
return c.proxy, nil
6248
}
6349

64-
return err
50+
return nil, errors.New("Unhealthy Node")
6551
}
6652

6753
func (c *Connection) healthCheck() {
6854
for {
6955
select {
7056
case msg := <-c.Messages:
7157
c.Lock()
72-
backend := msg.Backend
73-
c.healthy = msg.Health
74-
proxy := msg.Proxy
58+
if msg.Shutdown {
59+
c.Shutdown()
60+
c.Unlock()
61+
return
62+
} else {
63+
backend := msg.Backend
64+
c.healthy = msg.Health
65+
proxy := msg.Proxy
66+
67+
if proxy != nil && c.Backend != backend {
68+
c.Backend = backend
69+
c.proxy = proxy
70+
}
7571

76-
if proxy != nil && c.Backend != backend {
77-
c.Backend = backend
78-
c.proxy = proxy
7972
}
8073

8174
msg.Ack.Done()
@@ -85,5 +78,8 @@ func (c *Connection) healthCheck() {
8578
}
8679

8780
func (c *Connection) Shutdown() {
81+
c.healthy = false
82+
c.Shut = true
8883
close(c.Messages)
84+
stats.AvailableConnectionsGauge.WithLabelValues("available").Sub(1)
8985
}

internal/connection/main_test.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package connection
22

33
import (
4-
"io/ioutil"
54
"net/http"
65
"net/http/httptest"
76
"net/http/httputil"
87
"net/url"
8+
"reflect"
99
"sync"
1010
"testing"
1111
"time"
1212

1313
"github.com/CoderCookE/goaround/internal/assert"
14-
"github.com/dgraph-io/ristretto"
1514
)
1615

1716
func TestConnection(t *testing.T) {
@@ -28,18 +27,10 @@ func TestConnection(t *testing.T) {
2827

2928
assertion := &assert.Asserter{T: t}
3029

31-
t.Run("When get is called", func(t *testing.T) {
32-
30+
t.Run("When get is called on a healthy connection", func(t *testing.T) {
3331
availableServer := httptest.NewServer(availableHandler)
3432
backend := availableServer.URL
3533

36-
cache, err := ristretto.NewCache(&ristretto.Config{
37-
NumCounters: 1e7, // number of keys to track frequency of (10M).
38-
MaxCost: 1 << 30, // maximum cost of cache (1GB).
39-
BufferItems: 64, // number of keys per Get buffer.
40-
})
41-
assertion.Equal(err, nil)
42-
4334
url, err := url.ParseRequestURI(backend)
4435
assertion.Equal(err, nil)
4536

@@ -48,35 +39,21 @@ func TestConnection(t *testing.T) {
4839

4940
startup := &sync.WaitGroup{}
5041
startup.Add(1)
51-
conn := NewConnection(proxy, backend, cache, startup)
42+
conn := NewConnection(proxy, backend, startup)
5243

5344
assertion.NotEqual(conn, nil)
5445
assertion.Equal(err, nil)
5546
startup.Wait()
5647
conn.healthy = true
5748

58-
req := httptest.NewRequest(http.MethodGet, backend, nil)
59-
res := httptest.NewRecorder()
60-
conn.Get(res, req)
61-
62-
result, err := ioutil.ReadAll(res.Result().Body)
49+
usableProxy, err := conn.Get()
6350
assertion.Equal(err, nil)
64-
defer res.Result().Body.Close()
65-
66-
assertion.Equal(string(result), "hello")
51+
assertion.Equal(reflect.TypeOf(usableProxy).String(), "*httputil.ReverseProxy")
6752
})
6853

6954
t.Run("starts in an unhealthy state", func(t *testing.T) {
7055
backend := "http://www.google.com/"
7156

72-
cache, err := ristretto.NewCache(&ristretto.Config{
73-
NumCounters: 1e7, // number of keys to track frequency of (10M).
74-
MaxCost: 1 << 30, // maximum cost of cache (1GB).
75-
BufferItems: 64, // number of keys per Get buffer.
76-
})
77-
assertion.Equal(err, nil)
78-
assertion.NotEqual(cache, nil)
79-
8057
url, err := url.ParseRequestURI(backend)
8158
assertion.Equal(err, nil)
8259

@@ -85,7 +62,7 @@ func TestConnection(t *testing.T) {
8562

8663
startup := &sync.WaitGroup{}
8764
startup.Add(1)
88-
conn := NewConnection(proxy, backend, cache, startup)
65+
conn := NewConnection(proxy, backend, startup)
8966
assertion.Equal(err, nil)
9067
startup.Wait()
9168

@@ -95,13 +72,6 @@ func TestConnection(t *testing.T) {
9572
t.Run("when passed a new health stat", func(t *testing.T) {
9673
backend := "http://www.google.com/"
9774

98-
cache, err := ristretto.NewCache(&ristretto.Config{
99-
NumCounters: 1e7, // number of keys to track frequency of (10M).
100-
MaxCost: 1 << 30, // maximum cost of cache (1GB).
101-
BufferItems: 64, // number of keys per Get buffer.
102-
})
103-
assertion.Equal(err, nil)
104-
10575
url, err := url.ParseRequestURI(backend)
10676
assertion.Equal(err, nil)
10777

@@ -110,7 +80,7 @@ func TestConnection(t *testing.T) {
11080

11181
startup := &sync.WaitGroup{}
11282
startup.Add(1)
113-
conn := NewConnection(proxy, backend, cache, startup)
83+
conn := NewConnection(proxy, backend, startup)
11484
startup.Wait()
11585

11686
wg := &sync.WaitGroup{}

0 commit comments

Comments
 (0)