This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
driver-client.go
92 lines (77 loc) · 2.53 KB
/
driver-client.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bblfsh
import (
"context"
protocol2 "github.com/bblfsh/sdk/v3/protocol"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
/*
multipleDriverClient could be useful during language-specific parsings on the large scale
Examples of endpoint formats:
- localhost:9432 - casual example there's only one driver or bblfshd server
- python=localhost:9432,go=localhost:9432 - coma-separated mapping in format language=address
- %s-driver.bblfsh.svc.example.com - DNS template based on the language
*/
// multipleDriverClient is a DriverClient implementation, contains connection getter and a map[language]connection
type multipleDriverClient struct {
getConn ConnFunc
// key is a language
drivers map[string]*connDriver
}
type connDriver struct {
conn *grpc.ClientConn
driver protocol2.DriverClient
}
// multipleDriverHostClient is a DriverHostClient implementation, currently does almost nothing
type multipleDriverHostClient struct{}
// newMultipleDriverClient is a multipleDriverClient constructor
func newMultipleDriverClient(getConn ConnFunc) *multipleDriverClient {
return &multipleDriverClient{
getConn: getConn,
drivers: make(map[string]*connDriver),
}
}
// Parse gets connection from a given map, or creates a new connection, then inits driver client and performs Parse
func (c *multipleDriverClient) Parse(
ctx context.Context,
in *protocol2.ParseRequest,
opts ...grpc.CallOption) (*protocol2.ParseResponse, error) {
lang := in.Language
connD, ok := c.drivers[lang]
if !ok {
gConn, err := c.getConn(ctx, lang)
if err != nil {
return nil, err
}
connD = &connDriver{
conn: gConn,
driver: protocol2.NewDriverClient(gConn),
}
c.drivers[lang] = connD
}
return connD.driver.Parse(ctx, in, opts...)
}
func (c *multipleDriverClient) Close() error {
var lastErr error
for k, v := range c.drivers {
if err := v.conn.Close(); err != nil {
lastErr = err
}
delete(c.drivers, k)
}
c.drivers = make(map[string]*connDriver)
return lastErr
}
func (hc *multipleDriverHostClient) ServerVersion(
ctx context.Context,
in *protocol2.VersionRequest,
opts ...grpc.CallOption) (*protocol2.VersionResponse, error) {
return nil, status.Error(codes.Unimplemented, "ServerVersion is not implemented")
}
func (hc *multipleDriverHostClient) SupportedLanguages(
ctx context.Context,
in *protocol2.SupportedLanguagesRequest,
opts ...grpc.CallOption) (*protocol2.SupportedLanguagesResponse, error) {
return nil, status.Error(codes.Unimplemented, "SupportedLanguages is not implemented")
}