Skip to content
Snippets Groups Projects
Verified Commit 0f23c128 authored by Malte Bauch's avatar Malte Bauch
Browse files

Use same connection for plugins

parent 8ad9894b
No related branches found
No related tags found
1 merge request!1074Resolve "Too many open files"
...@@ -16,6 +16,13 @@ import ( ...@@ -16,6 +16,13 @@ import (
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
type pluginConnection struct {
client *hcplugin.Client
model shared.DeviceModel
}
var pluginClients = make(map[uuid.UUID]pluginConnection, 0)
// Plugin is the controllers internal representation of a plugin. // Plugin is the controllers internal representation of a plugin.
type Plugin struct { type Plugin struct {
UUID uuid.UUID UUID uuid.UUID
...@@ -66,6 +73,11 @@ func NewPlugin(id uuid.UUID, execPath string) (*Plugin, error) { ...@@ -66,6 +73,11 @@ func NewPlugin(id uuid.UUID, execPath string) (*Plugin, error) {
} }
} }
pluginClients[id] = pluginConnection{
client: client,
model: model,
}
return &Plugin{ return &Plugin{
UUID: id, UUID: id,
client: client, client: client,
...@@ -78,43 +90,52 @@ func NewPlugin(id uuid.UUID, execPath string) (*Plugin, error) { ...@@ -78,43 +90,52 @@ func NewPlugin(id uuid.UUID, execPath string) (*Plugin, error) {
// NewPluginThroughReattachConfig creates a new Plugin through a reattach config. // NewPluginThroughReattachConfig creates a new Plugin through a reattach config.
func NewPluginThroughReattachConfig(loadedPlugin plugin.LoadedPlugin) (plugin.Plugin, error) { func NewPluginThroughReattachConfig(loadedPlugin plugin.LoadedPlugin) (plugin.Plugin, error) {
client := hcplugin.NewClient(&hcplugin.ClientConfig{ //client := hcplugin.NewClient(&hcplugin.ClientConfig{
HandshakeConfig: shared.Handshake, // HandshakeConfig: shared.Handshake,
Plugins: shared.PluginMap, // Plugins: shared.PluginMap,
Reattach: &loadedPlugin.ReattachConfig, // Reattach: &loadedPlugin.ReattachConfig,
AllowedProtocols: []hcplugin.Protocol{hcplugin.ProtocolGRPC}, // AllowedProtocols: []hcplugin.Protocol{hcplugin.ProtocolGRPC},
}) //})
// create a client that is within the AllowedProtocols. In this case this //// create a client that is within the AllowedProtocols. In this case this
// returns a gRPCClient. Allows to connect through gRPC. //// returns a gRPCClient. Allows to connect through gRPC.
gRPCClient, err := client.Client() //gRPCClient, err := client.Client()
//if err != nil {
// return nil, err
//}
//// Request the plugin. This returns the gRPC client from the
//// DeviceModelPlugin. This can then be casted to the interface that we are
//// exposing through the plugin (in this case "DeviceModel").
//raw, err := gRPCClient.Dispense("deviceModel")
//if err != nil {
// return nil, err
//}
//// cast the raw plugin to the DeviceModel interface. This allows to call
//// methods on the plugin as if it were a normal DeviceModel instance but
//// actually they are executed on the plugin sent through gRPC.
//model, ok := raw.(shared.DeviceModel)
//if !ok {
// return nil, customerrs.InvalidTypeAssertionError{
// Value: model,
// Type: (*shared.DeviceModel)(nil),
// }
//}
pluginId, err := uuid.Parse(loadedPlugin.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Request the plugin. This returns the gRPC client from the pc, ok := pluginClients[pluginId]
// DeviceModelPlugin. This can then be casted to the interface that we are
// exposing through the plugin (in this case "DeviceModel").
raw, err := gRPCClient.Dispense("deviceModel")
if err != nil {
return nil, err
}
// cast the raw plugin to the DeviceModel interface. This allows to call
// methods on the plugin as if it were a normal DeviceModel instance but
// actually they are executed on the plugin sent through gRPC.
model, ok := raw.(shared.DeviceModel)
if !ok { if !ok {
return nil, customerrs.InvalidTypeAssertionError{ return nil, fmt.Errorf("plugin not found")
Value: model,
Type: (*shared.DeviceModel)(nil),
}
} }
return &Plugin{ return &Plugin{
UUID: uuid.MustParse(loadedPlugin.ID), UUID: uuid.MustParse(loadedPlugin.ID),
client: client, client: pc.client,
DeviceModel: model, DeviceModel: pc.model,
manifest: &loadedPlugin.Manifest, manifest: &loadedPlugin.Manifest,
state: plugin.INITIALIZED, state: plugin.INITIALIZED,
}, nil }, nil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment