Skip to content

Commit

Permalink
use httprouter and cactus
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Apr 17, 2021
1 parent 08a86f0 commit 0596fcd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## 0.10.0 (2021-04-05)
* Default HTTP port changed to 8825
* Configure HTTP timeouts through `--http-timeout-read`, `--http-timeout-write` and `--http-timeout-idle`
* Allow to configure router through `--http-router-name`

## 0.9 (2019-01-29)
* TLS Secure connection listening
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ run-trace:
go tool trace /tmp/statsd-http-proxt-trace

run-siege:
time siege -c 150 -r 5000 -H 'Connection: close' -H 'X-JWT-Token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdGF0c2QtcmVzdC1zZXJ2ZXIiLCJpYXQiOjE1MDY5NzI1ODAsImV4cCI6MTg4NTY2Mzc4MCwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3Nva2lsL3N0YXRzZC1yZXN0LXNlcnZlciIsInN1YiI6InNva2lsIn0.sOb0ccRBnN1u9IP2jhJrcNod14G5t-jMHNb_fsWov5c' "http://127.0.0.1:8080/count/a.b.c.d POST value=42"
time siege -c 150 -r 2000 -H 'Connection: close' -H 'X-JWT-Token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdGF0c2QtcmVzdC1zZXJ2ZXIiLCJpYXQiOjE1MDY5NzI1ODAsImV4cCI6MTg4NTY2Mzc4MCwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3Nva2lsL3N0YXRzZC1yZXN0LXNlcnZlciIsInN1YiI6InNva2lsIn0.sOb0ccRBnN1u9IP2jhJrcNod14G5t-jMHNb_fsWov5c' "http://127.0.0.1:8080/count/a.b.c.d POST value=42"

run-wrk:
docker run --rm --network="host" -v $(CURDIR):/proxy williamyeh/wrk -c 1 -t 1 -d 20 -s /proxy/bench/wrk.lua http://127.0.0.1:8080/count/a.b.c.d
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ func main() {
var tokenSecret = flag.String("jwt-secret", "", "Secret to encrypt JWT")
var verbose = flag.Bool("verbose", false, "Verbose")
var version = flag.Bool("version", false, "Show version")
var httpRouterName = flag.String("http-router-name", "GorillaMux", "Type of HTTP router. Allowed values are GorillaMux and HttpRouter. Do not use in production.")
var httpRouterName = flag.String("http-router-name", "HttpRouter", "Type of HTTP router. Allowed values are GorillaMux and HttpRouter. Do not use in production.")
var statsdClientName = flag.String("statsd-client-name", "Cactus", "Type of StatsD client. Allowed values are Cactus and GoMetric. Do not use in production.")
var profilerHTTPort = flag.Int("profiler-http-port", 0, "Start profiler localhost")

// get flags
flag.Parse()

// show version and exit
if *version == true {
if *version {
fmt.Printf(
"StatsD HTTP Proxy v.%s, build %s from %s\n",
Version,
Expand Down Expand Up @@ -97,6 +98,7 @@ func main() {
*tokenSecret,
*verbose,
*httpRouterName,
*statsdClientName,
)

proxyServer.Listen()
Expand Down
12 changes: 6 additions & 6 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewServer(
tokenSecret string,
verbose bool,
httpRouterName string,
statsdClientName string,
) *Server {
// configure logging
var logOutput io.Writer
Expand All @@ -55,14 +56,12 @@ func NewServer(
logger := log.New(logOutput, "", log.LstdFlags)

// create StatsD Client
statsdClientName := "Cactus"

var statsdClient statsdclient.StatsdClientInterface
switch statsdClientName {
case "GoMetric":
statsdClient = statsdclient.NewGoMetricClient(statsdHost, statsdPort)
case "Cactus":
statsdClient = statsdclient.NewCactusClient(statsdHost, statsdPort)
case "GoMetric":
statsdClient = statsdclient.NewGoMetricClient(statsdHost, statsdPort)
default:
panic("Passed statsd client not supported")
}
Expand All @@ -76,10 +75,11 @@ func NewServer(
// build router
var httpServerHandler http.Handler
switch httpRouterName {
case "GorillaMux":
httpServerHandler = router.NewGorillaMuxRouter(routeHandler, tokenSecret)
case "HttpRouter":
httpServerHandler = router.NewHTTPRouter(routeHandler, tokenSecret)
case "GorillaMux":
httpServerHandler = router.NewGorillaMuxRouter(routeHandler, tokenSecret)

default:
panic("Passed HTTP router not supported")
}
Expand Down
6 changes: 4 additions & 2 deletions proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ func TestNewServerWithGorillaMuxRouter(t *testing.T) {
"someTokenSecret",
true,
"GorillaMux",
"Cactus",
)

require.Equal(t, "*proxy.Server", reflect.TypeOf(proxyServer).String())

require.Equal(t, "127.0.0.1:8080", proxyServer.httpAddress)

require.Equal(t, "*statsd.Client", reflect.TypeOf(proxyServer.statsdClient).String())
require.Equal(t, "*statsdclient.CactusStatsdClientAdapter", reflect.TypeOf(proxyServer.statsdClient).String())

require.Equal(t, "tls.crt", proxyServer.tlsCert)

Expand All @@ -50,13 +51,14 @@ func TestNewServerWithHttpRouter(t *testing.T) {
"someTokenSecret",
true,
"HttpRouter",
"Cactus",
)

require.Equal(t, "*proxy.Server", reflect.TypeOf(proxyServer).String())

require.Equal(t, "127.0.0.1:8080", proxyServer.httpAddress)

require.Equal(t, "*statsd.Client", reflect.TypeOf(proxyServer.statsdClient).String())
require.Equal(t, "*statsdclient.CactusStatsdClientAdapter", reflect.TypeOf(proxyServer.statsdClient).String())

require.Equal(t, "tls.crt", proxyServer.tlsCert)

Expand Down

0 comments on commit 0596fcd

Please sign in to comment.