gitmyhub

go-plugin

Go ★ 6.0k updated 13d ago

Golang plugin system over RPC.

go-plugin is a library from HashiCorp that lets you build plugin systems in the Go programming language. A plugin system means your application can load and run additional pieces of code (plugins) that were written separately, possibly by other developers. This library handles the communication between the main application and each plugin.

The way it works is that each plugin runs as a separate process on the same machine, and the main application talks to it over a local network connection using a protocol called RPC. Because the plugin is a separate process, a crash inside a plugin cannot take down the main application. From the perspective of the developer writing the plugin or using it, the experience looks like calling a normal function on a Go interface, and the library handles all the behind-the-scenes communication. Plugins can also be written in other languages, since the library supports a communication format called gRPC which is language-independent.

HashiCorp uses this library across several of their well-known open-source tools including Terraform, Vault, Nomad, and Packer. The README states it has run on millions of machines and is considered production-ready.

Features described in the README include: automatic forwarding of log output from plugins back to the host application, TLS encryption for communication with plugins, support for checking that a plugin binary matches an expected checksum, the ability to upgrade the host application while a plugin keeps running, and compatibility with terminals that require direct input (TTY). Protocol versioning is also supported so that incompatible plugin versions produce a clear error message rather than silent failures.

Using the library requires implementing a Go interface for the behavior you want to expose to plugins, then wrapping it with the client and server code that go-plugin needs to route calls over RPC.