-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
50 lines (41 loc) · 1.1 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package lsp
import (
"context"
"github.com/joyme123/thrift-ls/lsp/cache"
"github.com/joyme123/thrift-ls/lsp/memoize"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/pkg/event"
"go.lsp.dev/protocol"
"go.uber.org/zap"
)
type StreamServer struct {
logger *zap.Logger
cache *cache.Cache
}
func NewStreamServer() *StreamServer {
logger, _ := zap.NewProduction()
store := &memoize.Store{}
return &StreamServer{
cache: cache.New(store),
logger: logger,
}
}
func (s *StreamServer) ServeStream(ctx context.Context, conn jsonrpc2.Conn) error {
client := protocol.ClientDispatcher(conn, s.logger)
server := NewServer(s.cache, client)
// Clients may or may not send a shutdown message. Make sure the server is
// shut down.
// TODO(rFindley): this shutdown should perhaps be on a disconnected context.
defer func() {
if err := server.Shutdown(ctx); err != nil {
event.Error(ctx, "error shutting down", err)
}
}()
ctx = protocol.WithClient(ctx, client)
conn.Go(ctx,
DebugHandler(
protocol.Handlers(
protocol.ServerHandler(server, jsonrpc2.MethodNotFoundHandler))))
<-conn.Done()
return conn.Err()
}