hashicorp / go-plugin

Golang plugin system over RPC.
Mozilla Public License 2.0
5.2k stars 447 forks source link

Dynamic Registration of PluginMap #187

Open jonwhitty opened 2 years ago

jonwhitty commented 2 years ago

I'd like to build an app that fetches rows of data from a database and then invokes some generic callback of the form HandleRows(ctx, rows...) error. It'd be nice to have a single binary for the primary app, but the behavior of the app is dependent upon the plugin that is loaded for the interface that defines the HandleRows method.

Do you guys have any examples of how to dynamically register a PluginMap based on module registration from within the module and within the host client?

Essentially the workflow I'm looking for is this:

  1. Developer authors a plugin and compiles it into a .so file.
  2. When the host client app starts up it looks for a configurable plugin directory (e.g. ~/.myapp/plugins).
  3. The plugin module registers its dispensable plugins by adding itself to the PluginMap.
  4. The host client app invokes the shared interface from the plugin.

There will only ever really be one plugin loaded at a time in my use case, but the plugins behavior will define what functionality gets invoked when HandleRows(ctx, rows...) is called.

I'm using this approach to try and build a generic outbox processing mechanism as part of an outbox pattern implementation. The way outbox events are handled across apps will differ, but the processing of the events is the same regardless of what you do with them. I thought about using AWS lambdas or something similar to achieve this, but the go-plugin approach seems like it could be a more native approach and wouldn't have the downsides of lambda invocation and the latency/cost associated with that.

mrcook commented 1 year ago

Perhaps this helps? https://github.com/dkiser/go-plugin-example