Skip to content

Commit

Permalink
IG-12673: Use golang proxy for websocket instead of koding/websocketp…
Browse files Browse the repository at this point in the history
…roxy (#20)
  • Loading branch information
urihoenig authored and Hedingber committed Dec 16, 2019
1 parent 60d4aac commit f077fc1
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 254 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ facilitate scale-to-zero flows.

Supported metrics:
1. General:
* `num_of_requests` - prometheus `CounterVec` that simply counts requests using a reverse proxy. Supports both HTTP
(Go's built in ReverseProxy) and Websocket (https://github.com/koding/websocketproxy) requests<br>
* `num_of_requests` - prometheus `CounterVec` that simply counts requests using a reverse proxy (Go's built in ReverseProxy)<br>
2. Service specific:
* Jupyter:
* `jupyter_kernel_busyness` - prometheus `GaugeVec` that is set to 1 if Jupyter has one or more busy kernels,
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.13
require (
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf // indirect
github.com/gorilla/websocket v1.4.1
github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c
github.com/nuclio/errors v0.0.1
github.com/nuclio/logger v0.0.1
github.com/nuclio/loggerus v0.0.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c h1:N7A4JCA2G+j5fuFxCsJqjFU/sZe0mj8H0sSoSwbaikw=
github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c/go.mod h1:Nn5wlyECw3iJrzi0AhIWg+AJUb4PlRQVW4/3XHH1LZA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
Expand Down
44 changes: 7 additions & 37 deletions pkg/sidecarproxy/metricshandler/numofrequests/metricshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,16 @@ import (

"github.com/v3io/sidecar-proxy/pkg/sidecarproxy/metricshandler"
"github.com/v3io/sidecar-proxy/pkg/sidecarproxy/metricshandler/abstract"
"github.com/v3io/sidecar-proxy/pkg/util"

"github.com/gorilla/websocket"
"github.com/koding/websocketproxy"
"github.com/nuclio/errors"
"github.com/nuclio/logger"
"github.com/prometheus/client_golang/prometheus"
)

var (
WebsocketUpgrader = util.ExtendedWebSocket{
WebsocketUpgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}}
)

type metricsHandler struct {
*abstract.MetricsHandler
metric *prometheus.CounterVec
httpProxy *httputil.ReverseProxy
webSocketProxy *websocketproxy.WebsocketProxy
metric *prometheus.CounterVec
proxy *httputil.ReverseProxy
}

func NewMetricsHandler(logger logger.Logger,
Expand Down Expand Up @@ -74,27 +62,21 @@ func (n *metricsHandler) RegisterMetrics() error {

func (n *metricsHandler) Start() error {
http.HandleFunc("/", n.onRequest)
if err := n.createProxies(); err != nil {
return errors.Wrap(err, "Failed to initiate proxies")
if err := n.createProxy(); err != nil {
return errors.Wrap(err, "Failed to initiate proxy")
}

// adds one data point on service initialization so metric will be initialized and queryable
n.incrementMetric()
return nil
}

func (n *metricsHandler) createProxies() error {
webSocketTargetURL, err := url.Parse("ws://" + n.ForwardAddress)
if err != nil {
return errors.Wrap(err, "Failed to parse web socket forward address")
}
n.webSocketProxy = websocketproxy.NewProxy(webSocketTargetURL)

func (n *metricsHandler) createProxy() error {
httpTargetURL, err := url.Parse("http://" + n.ForwardAddress)
if err != nil {
return errors.Wrap(err, "Failed to parse http forward address")
}
n.httpProxy = httputil.NewSingleHostReverseProxy(httpTargetURL)
n.proxy = httputil.NewSingleHostReverseProxy(httpTargetURL)

return nil
}
Expand Down Expand Up @@ -125,18 +107,6 @@ func (n *metricsHandler) onRequest(res http.ResponseWriter, req *http.Request) {
}

func (n *metricsHandler) forwardRequest(res http.ResponseWriter, req *http.Request) error {
proxyHandler := n.getProxyHandler(res, req)
proxyHandler.ServeHTTP(res, req)
n.proxy.ServeHTTP(res, req)
return nil
}

func (n *metricsHandler) getProxyHandler(res http.ResponseWriter, req *http.Request) http.Handler {
if n.isWebSocket(res, req) {
return n.webSocketProxy
}
return n.httpProxy
}

func (n *metricsHandler) isWebSocket(res http.ResponseWriter, req *http.Request) bool {
return WebsocketUpgrader.VerifyWebSocket(res, req, nil) == nil
}
166 changes: 0 additions & 166 deletions pkg/util/util.go

This file was deleted.

43 changes: 0 additions & 43 deletions pkg/util/websocket.go

This file was deleted.

0 comments on commit f077fc1

Please sign in to comment.