9 "github.com/hashicorp/go-plugin/internal/plugin"
10 "golang.org/x/net/context"
11 "google.golang.org/grpc"
12 "google.golang.org/grpc/credentials"
13 "google.golang.org/grpc/health/grpc_health_v1"
16 func dialGRPCConn(tls *tls.Config, dialer func(string, time.Duration) (net.Conn, error)) (*grpc.ClientConn, error) {
17 // Build dialing options.
18 opts := make([]grpc.DialOption, 0, 5)
20 // We use a custom dialer so that we can connect over unix domain sockets.
21 opts = append(opts, grpc.WithDialer(dialer))
24 opts = append(opts, grpc.FailOnNonTempDialError(true))
26 // If we have no TLS configuration set, we need to explicitly tell grpc
27 // that we're connecting with an insecure connection.
29 opts = append(opts, grpc.WithInsecure())
31 opts = append(opts, grpc.WithTransportCredentials(
32 credentials.NewTLS(tls)))
35 // Connect. Note the first parameter is unused because we use a custom
36 // dialer that has the state to see the address.
37 conn, err := grpc.Dial("unused", opts...)
45 // newGRPCClient creates a new GRPCClient. The Client argument is expected
46 // to be successfully started already with a lock held.
47 func newGRPCClient(doneCtx context.Context, c *Client) (*GRPCClient, error) {
48 conn, err := dialGRPCConn(c.config.TLSConfig, c.dialer)
54 brokerGRPCClient := newGRPCBrokerClient(conn)
55 broker := newGRPCBroker(brokerGRPCClient, c.config.TLSConfig)
57 go brokerGRPCClient.StartStream()
61 Plugins: c.config.Plugins,
64 controller: plugin.NewGRPCControllerClient(conn),
70 // GRPCClient connects to a GRPCServer over gRPC to dispense plugin types.
71 type GRPCClient struct {
73 Plugins map[string]Plugin
75 doneCtx context.Context
78 controller plugin.GRPCControllerClient
81 // ClientProtocol impl.
82 func (c *GRPCClient) Close() error {
84 c.controller.Shutdown(c.doneCtx, &plugin.Empty{})
88 // ClientProtocol impl.
89 func (c *GRPCClient) Dispense(name string) (interface{}, error) {
90 raw, ok := c.Plugins[name]
92 return nil, fmt.Errorf("unknown plugin type: %s", name)
95 p, ok := raw.(GRPCPlugin)
97 return nil, fmt.Errorf("plugin %q doesn't support gRPC", name)
100 return p.GRPCClient(c.doneCtx, c.broker, c.Conn)
103 // ClientProtocol impl.
104 func (c *GRPCClient) Ping() error {
105 client := grpc_health_v1.NewHealthClient(c.Conn)
106 _, err := client.Check(context.Background(), &grpc_health_v1.HealthCheckRequest{
107 Service: GRPCServiceName,