1. What is a plug-in (also called a dynamic library)

When writing C++ programs, it is often necessary to write a class as a DLL(dynamic link library) that can be called by the client program. Such a DLL can export an entire class, as well as a method of that class.

Call through DLL and write code in the program to call the difference: see whether this function is available to other programs call;

Other procedures certainly can not call a function of this procedure, the total can not copy the code to him, not to say can copy, even if you can also trouble a, directly written DLL let him call;

Plug-ins are like DLLS;

Extend the functionality of a program at run time by using plug-ins without recompiling the program;

New functions can be added without stopping after starting.

Advantages:

  • Dynamic loading, also known as hot loading, does not recompile the entire project and redeploy the service at each upgrade, but dynamically updates when plug-ins are added. This is important for many heavy duty services.

Disadvantages:

  • If some illegal modules are injected, how to prevent them?
  • Bring certain unstable factors to the system, if the module has problems, not through good testing, easy to lead to service crash

2. Go’s plug-in system :Plugin

The Go plug-in is a package compiled with the -buildmode = plugin tag to generate a shared object (.so) library file.

The exported functions and variables in the Go package are exposed as ELF symbols, which can be found and bound at run time using the Plugin package. This allows you to query and call functions in the plug-in;

3. Principles of plug-in design

  • The plugin independent
  • Use interface types as boundaries
  • Unix modularity Principles
  • Version control

4. Go uses plug-ins

  • Open the plug-in file with plugin.open ()

  • Use plguin.Lookup(” export-variable-name “) to find the exported symbol “Car” or “Phone”.

    Notice that the symbol name matches the variable name defined in the plug-in module

  • Type assertion, and then you can make the call

Go build-buildmode =plugin file.go, build the plugin

Plugins in GO are not process-isolated, but single-process; = =

Golang’s plugin is so imperfect that it can only be loaded, not unloaded. How you need to uninstall requires you to implement a set of overloading mechanism.

5. Pay attention to using plugin

The Go plugin determines whether two plugins are the same by comparing pluginPath. If no pluginPath is specified, Is generated by the algorithm of Internal, the format of the generated for the plugin/unnamed – “+ root. Package. Internal. BuildID.

Before you start coding, there are a few problems to solve:1The same so file will only be opened once2Each SO has a pluginpath to indicate whether the two SO files are duplicated. If the two SO files are different, but the pluginPath is the same, an error will be reported. It will be recognized as the same file and an error will be reported when both plug-ins are loaded simultaneously.3Structs defined by different so files cannot be converted using type assertions. There is a solution to the above problem: add -- ldFlags = at compile time"-pluginpath=xxx"Parameter, specify the pluginpath parameter to avoid duplication.Copy the code

6. Plug-in memory

When the.so file is loaded, it allocates memory for global variables and init() initialization is performed. Stack: is a thread unique to the same process space, functions can call each other directly;Copy the code

Think: Will I call other parts of the project, such as Models, from within the plug-in, and compile the plug-in without an error? I haven’t even loaded my plugins yet?

First, plug-ins are also called dynamic libraries, which are no different from static libraries. A static library is a program that has been compiled, and then you modify the static library and the program doesn’t work, you have to recompile.

The dynamic library is different. After the program is compiled and run, the dynamic library can still be changed. After the change, the program is reloaded once again.

Models are not called when the plug-in is compiled, and the plug-in is only compiled. If there is a problem with the call, it will be after the plug-in is loaded.