From e79a58dba8057dc1290198caca9cdd89f464fd0e Mon Sep 17 00:00:00 2001 From: Tobias Guggenmos Date: Tue, 7 Jul 2020 20:53:04 +0200 Subject: [PATCH 1/2] Remove websocket package The websocket module was never finished and due to API changes in various places, doesn't work in it's current form. Also it's incompatible with #181. If someone wants to solve #55, it's better to start over. Signed-off-by: Tobias Guggenmos --- websocket/websocket.go | 106 ----------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 websocket/websocket.go diff --git a/websocket/websocket.go b/websocket/websocket.go deleted file mode 100644 index f98c5b37..00000000 --- a/websocket/websocket.go +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2019 Tobias Guggenmos -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Websocket provides support to run the language server over websocket (experimental). -package langserver - -import ( - "context" - "errors" - "net/http" - - "github.com/gorilla/websocket" - "github.com/prometheus-community/promql-langserver/config" - "github.com/prometheus-community/promql-langserver/langserver" -) - -// Implements the jsonrpc2.Stream interface. -type wsConn struct { - *websocket.Conn -} - -func (c wsConn) Read(ctx context.Context) ([]byte, int64, error) { - // Returning an error on an expired context is important here. - // If that isn't done, the server won't stop after the connection - // has been closed. - select { - case <-ctx.Done(): - return nil, 0, ctx.Err() - default: - } - - typ, ret, err := c.ReadMessage() - if err != nil { - return nil, 0, err - } - - if typ != websocket.TextMessage { - return nil, 0, errors.New("wrong message type") - } - - return ret, int64(len(ret)), nil -} - -func (c wsConn) Write(ctx context.Context, msg []byte) (int64, error) { - select { - case <-ctx.Done(): - return 0, ctx.Err() - default: - } - - err := c.WriteMessage(websocket.TextMessage, msg) - if err != nil { - return 0, err - } - - return int64(len(msg)), nil -} - -// WebSocketHandler creates a HTTP handler that tries to upgrade each request to -// a websocket connection and then runs a language server instance on that connection. -// -// WARNING: This code is mostly untested so it might not work. -func WebSocketHandler(addr string) (func(http.ResponseWriter, *http.Request), error) { - upgrader := websocket.Upgrader{ - ReadBufferSize: 2048, - WriteBufferSize: 2048, - } - - return func(w http.ResponseWriter, r *http.Request) { - ws, err := upgrader.Upgrade(w, r, nil) - if err != nil { - // No special handling required here, since the - // error is already added to the ResponseWriter - // by the upgraded call. - return - } - - ctx, cancel := context.WithCancel(context.TODO()) - - ch := func(_ int, _ string) error { - cancel() - return nil - } - - ws.SetCloseHandler(ch) - - var s langserver.Server - - _, s = langserver.ServerFromStream(ctx, wsConn{ws}, &config.Config{LogFormat: config.TextFormat}) - - if err := s.Run(); err != nil { - // If the client disconnects, the above will fail - return - } - }, nil -} From 75e402b1ab6d06e63c86e77092b616aa5e943192 Mon Sep 17 00:00:00 2001 From: Tobias Guggenmos Date: Tue, 7 Jul 2020 20:59:28 +0200 Subject: [PATCH 2/2] go mod tidy Signed-off-by: Tobias Guggenmos --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index a17c8942..f0115250 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.13 require ( github.com/blang/semver v3.5.1+incompatible github.com/go-kit/kit v0.10.0 - github.com/gorilla/websocket v1.4.2 github.com/kelseyhightower/envconfig v1.4.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.7.1 diff --git a/go.sum b/go.sum index 8ab03fe0..ded3dda0 100644 --- a/go.sum +++ b/go.sum @@ -347,8 +347,6 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51 github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=