GoPlug

package module
v0.0.0-...-7b1c5f6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2015 License: MIT Imports: 14 Imported by: 0

README

GoPlug

< GoPlug >
 --------
   \
    \   
       _____  -----------------  _____
      |     \/  ----     ----  \/     |
       \  ==/  /    |   |    \  \==  /
        \--/  | ()  |   |()   |  \--/
          /    \---/ ___ \---/    \
         |         _(===)_         |
        |         (__/--\__)        |
         |          |_||_|         |
          \                       /   

GoPlug is a pure Go Plugin libary project that provides flexibility, Loose Coupling and moduler approach of Building Software in/around Go. The goal of the project is to provide a simple, fast and a reliable plugin architecture that is independent of the platform.

[GpPlug GoDoc] (https://godoc.org/github.com/swarvanusg/GoPlug)

Version

0.1.0

Usage
Step 1 : Get It

To get the GoPlug install Go and execute the below command

go get github.com/swarvanusg/GoPlug
Step 2: Lifecycle

GoPlug plugin lifecycle is quite simple as it consist only three state.

  1. Stopped : Plugin is not yet started or stopped
  2. Discovered/Installed : Plugin is discoved and ready to be started
  3. Started/Loaded : Plugin is started or Loaded for serving request
Plugin Registry

Each of the application creates a Plugin Registry to manage Plugins. Plugin Registry is based on plugin discovery service that provide api to search, load and unload plugin to/from registry.

Auto discovery service at plugin registry could be disabled resulting plugin to be discovered at loading time.

Plugin

Each plugin makes itself available for the discovery service, and while discovered it is loaded by the application. On a successful loading start() is called and on a successful uploading stop() is called

Lazy start could be enabled to make plugin loaded by explicit call to Plugin Registry rather than at discovery.

Step 3: Use it
Plugin Conf

Plugin conf (.pconf) defines the plugin properties. It is created by the Plugins at Plugin startup and loaded by the Application.

Example.pconf
    {
        "Name" : "NameOfPlugin",
        "NameSpace" : "NamespaceOfPlugin",
        "Url" : "unix://PluginUrl",
        "sock" : "unixSockLocation.sock",
        "LazyLoad" : false,
    }
Application That Use Plugins

Plugin registry is initialized with the plugin location where it will search for plugin conf (.pconf), along with the Auto Discover setting. If auto discovery is enabled the discover service starts and search for new plugin, while in other case of discovery service not running, plugin gets discovered while loading (via Explicit call to LoadPlugin) if available.

    plugRegConf := GoPlug.PluginRegConf{PluginLocation: "./PluginLoc", AutoDiscover: true}
    /* Initialize a Plugin Registry that will search location "./PluginLoc" for '.pconf' file */  
    pluginReg, err := GoPlug.PluginRegInit(plugRegConf)

Lazyload is a feature that prevents auto loading of a plugin when it is discovered. If Plugin is Configured for lazy load plugin should be loaded explicitly when needed by the user.

    plugin, err := pluginReg.LoadPlugin("name", "namespace")

Each plugin is identified by the plugin name and namespace

    plugin := pluginReg.GetPlugin("name", "namespace")

Plugin can be searched for available methods (registered methods by Plugin implementation)

    methodList := plugin.GetMethods()

Method could be executed by method name

    returnBytes, err := plugin.Execute(methodName, inputBytes)

Callback could be registered in Apllication to receive notification from plugin

    plugin.RegisterCallback(Foo)
    ...
    func Foo(data []byte) {
        // Callback body called on notification from pugin
    }

Plugin could be forced to unload or stopped

    err := pluginReg.UnloadPlugin(plugin)
Plugin Implementation

Plugin is initialized with the Location, Name, Namespace (optional), Url (optional), LazyStart conf, Activator and Stopper. The Plugin location should be same on which Plugin Registry is configured

    config := GoPlug.PluginImplConf{"PluginLoc", "Name", "Namespace", "unix://URL", false, activate, stop}
    plugin, err := GoPlug.PluginInit(config)
    ...
    func activate(input []byte) []byte {
        // Called on Activation of the Plugin
    }
    func stop(input []byte) []byte {
        // Called on Deactivation of the Plugin
    }

Method should be registered before starting the plugin

plugin.RegisterMethod(Do)
...
func Do(input []byte) []byte {
    // Call on execution of "Do" from application
}

Plugin start makes the plugin available for the discovery service and to be loaded

plugin.Start()

Plugin could notify application using callback. A list of registered callbacks are available at plugins

    //get available callback list
    callbackList := plugin.GetCallbacks()
    ...
    err := plugin.Notify(callbackName, inputBytes)

Plugin stop makes the plugin to be stopped and unavailable from the Plugin Reg service. It should be done after plugin is unloaded from the Plugin registry.

plugin.stop()

More ...

Step 4: How It Works

Plugins runs a different process that sould be started explicitly. Unix domain socket is used for IPC where the communication is based on HTTP request response model.

Step by Step:
  1. At start of the Plugin it opens a Unix domain socket and listen for connection
  2. Once it initialized it puts the .pconf file in a specific location of Plugin Discovery
  3. Plugin Registry discover the .pconf and load the configuration to get the properties and UNIX sock
  4. Plugin Registry initialize the Connections using UNIX sock and it loads the Plugin information
  5. Http request is made as per the methods Executed over the connection
Current Status

GoPlug is unstable and in active development and testing

Future Scope

As GoPlug Plugin are independent process and the communication is based on Unix socket and Http. Plugin could be developed using any programming language. In future GoPlug Plugin Implementation library should be implementated in different languages.

More Information

This is an early release. I’ve been using it for a while and this is working fine. I like this one pretty well, but no guarantees that it won’t change a bit.

For Any more info Contact:

swarvanusg@gmail.com

Documentation

Overview

< GoPlug >

--------
       \   ^__^
        \  (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||

GoPlug is a pure Go Plugin libary project that provides flexibility, Loose Coupling and moduler approach of Building Software in/around Go. The goal of the project is to provide a simple, fast and a reliable plugin architecture that is independent of the platform.

Lifecycle

GoPlug plugin lifecycle is quite simple as it consist only three state. 1. Stopped : Plugin is not yet started or stopped 2. Discovered/Installed : Plugin is discoved and ready to be started 3. Started/Loaded : Plugin is started or Loaded for serving request

Plugin Registry

Each of the application creates a Plugin Registry to manage Plugins. Plugin Registry is based on plugin discovery service that provide api to search, load and unload plugin to/from registry. Auto discovery service at plugin registry could be disabled resulting plugin to be discovered at loading time.

Plugin

Each plugin makes itself available for the discovery service, and while discovered it is loaded by the application. On a successful loading start() is called and on a successful uploading stop() is called

Lazy start could be enabled to make plugin loaded by explicit call to Plugin Registry rather than at discovery.

How It Works

Plugins runs a different process that sould be started explicitly. Unix domain socket is used for IPC where the communication is based on HTTP request response model.

Step by Step: 1> At start of the Plugin it opens a Unix domain socket and listen for connection 2> Once it initialized it puts the .pconf file in a specific location of Plugin Discovery 3> Plugin Registry discover the .pconf and load the configuration to get the properties and UNIX sock 4> Plugin Registry initialize the Connections using UNIX sock and it loads the Plugin information 5> Http request is made as per the methods Executed over the connection

Index

Constants

This section is empty.

Variables

View Source
var (
	// An error to indicate the plugin not discovered
	ConfigLoadFailed = errors.New("Configuration load failed")

	// An error to indicate the plugin not discovered
	PluginNotDiscovered = errors.New("Plugin is not Discovered")

	// An error to indicate the connection to plugin could not be made
	PluginConnFailed = errors.New("Plugin to connection failed")

	// An error to indicate the plugin is already loaded
	PluginLoaded = errors.New("Plugin is already loaded")

	// Conf Extension
	DefaultConfExt = ".pconf"

	// Default Interval for Discovery search in MS
	DefaultInterval = 1000
)

Functions

This section is empty.

Types

type Plugin

type Plugin struct {
	// The name of the Plugin
	PluginName string
	// The nameSpace of the Plugin
	PluginNameSpace string
	// The URL to reach the Plugin
	PluginUrl string
	// contains filtered or unexported fields
}

func (*Plugin) Execute

func (plugin *Plugin) Execute(funcName string, body []byte) (error, []byte)

Executes a specific plugin method by the method name. Each method takes a byte array as input

and returns a byte array as output

func (*Plugin) GetMethods

func (plugin *Plugin) GetMethods() []string

Get the list of available (registered) methods for a specific plugin

func (*Plugin) Ping

func (plugin *Plugin) Ping() error

Ping a specific plugin to check the plugin status

func (*Plugin) RegisterCallback

func (plugin *Plugin) RegisterCallback(function func([]byte)) error

Register a callback that will be called on notification from the plugin

type PluginConf

type PluginConf struct {
	Name      string
	NameSpace string
	Url       string
	Sock      string
	LazyLoad  bool
}

type PluginImpl

type PluginImpl struct {
	// contains filtered or unexported fields
}

The Plugin Implentaion Struct to represent a Plugin, provides all the methods to be implemented

func PluginInit

func PluginInit(pluginImplConf PluginImplConf) (*PluginImpl, error)

Initialize a plugin as per the provided plugin implementation configuration.

It returns a pointer to a PluginImpl that is used to perfom different operation on the implementde plugin

func (*PluginImpl) Notify

func (plugin *PluginImpl) Notify(callBack string, data []byte) error

Method to notify a callback registered by the application by the name of the callback.

User could sent input bytes for the callback. Callback doesn't return anything

func (*PluginImpl) Register

func (plugin *PluginImpl) Register()

Internal Method: Used to register a handle method for the incoming request to plugin. Should not be called explicitly

func (*PluginImpl) RegisterMethod

func (plugin *PluginImpl) RegisterMethod(method func([]byte) []byte)

Method to register a function for the plugin that could be invoked by the application.

Function Prototype: func ([]byte) []byte

func (*PluginImpl) ServeHTTP

func (plugin *PluginImpl) ServeHTTP(res http.ResponseWriter, req *http.Request)

Internal Method: Default handler to serve all http request that comes to the plugin. Should not be called explicitly

func (*PluginImpl) Start

func (plugin *PluginImpl) Start() error

Used to start the Plugin Service. It makes a plugin operable and discoverable by application

func (*PluginImpl) Stop

func (plugin *PluginImpl) Stop() error

Used to stop the Plugin service. It makes the plugin hidden from the application and stops all functionalities

type PluginImplConf

type PluginImplConf struct {
	// Plugin location path
	PluginLoc string
	// The Name of the plugin
	Name string
	// The namespace of the plugin [optional - default: nil]
	Namespace string
	// The URL to reach the plugin over http (ie unix://ExamplePlug) [optional - default: unix://<Namespace><Name>]
	Url string
	// The LazyLoad configuration [optional - default: false]
	LazyLoad bool
	// The Function that would be called on Plugin Activation
	Activator func([]byte) []byte
	// The Function that would be called on Plugin DeActivation
	Stopper func([]byte) []byte
}

The plugin implementaion configuration. Provides all the information that are required for the GoPlug to provide an implementation of Plugin

type PluginReg

type PluginReg struct {
	// The discoveredPlugin list
	DiscoveredPlugin map[string]PluginConf
	// The Loaded Plugin list
	PluginReg map[string]*Plugin
	// The waitgroup to wait for till PluginRegistry doesn't stop
	Wg *sync.WaitGroup
	// The Plugin search location
	PluginLocation string
	// The mutex to sync the Plugin reg access
	RegAccess *sync.Mutex
	// The flag to stop PluginRegistry Service
	StopFlag bool
	// The Conf Extension
	ConfExt string
	// To enable and disable autoDisover. For auto Loading autoDiscover
	// Should be enabled. Default is false
	AutoDiscover bool
}

PluginReg should be created per types of Plugin * each PluginReg monitor a specific location

func PluginRegInit

func PluginRegInit(regConf PluginRegConf) (*PluginReg, error)

Function is called to inititate the PluginRegistry as per the Plugin registry Configuration.

It initiate and return a plugin registry pointer that could be used to manage plugins.
If Discovery is enabled the DiscoverService Starts

func (*PluginReg) GetPlugin

func (pluginReg *PluginReg) GetPlugin(pluginName string, pluginNamespace string) *Plugin

Get a plugin from the Plugin registry for a specified name and namespace

func (*PluginReg) IsDiscovered

func (pluginReg *PluginReg) IsDiscovered(pluginname string, namespace string) bool

Check if a plugin is discovered by the plugin registry discovery service automatically or is discover implicitly

func (*PluginReg) IsLoaded

func (pluginReg *PluginReg) IsLoaded(pluginname string, namespace string) bool

Check if a plugin is loaded and active by the plugin registry automatically or is loaded implicitly

func (*PluginReg) LoadPlugin

func (pluginReg *PluginReg) LoadPlugin(pluginName string, pluginNamespace string) (*Plugin, error)
Load the plugin to the plugin Registry explicitly when lazy load is active.

(if The discovery Process is not running, It search for the plugin and then load it to the registry)

func (*PluginReg) Stop

func (pluginReg *PluginReg) Stop()

Function to stop the Plugin Registry service. It stops the discovery service

func (*PluginReg) UnloadPlugin

func (pluginReg *PluginReg) UnloadPlugin(plugin *Plugin) error

Unload a Plugin from the plugin Registry. It invokes a stop request to the plugin.

(It doesn't remove the Plugin from Discovered Plugin List)

func (*PluginReg) WaitForStop

func (pluginReg *PluginReg) WaitForStop()

Function to wait for PluginReg Discovery service to be stopped. If its not started then it return immediately

type PluginRegConf

type PluginRegConf struct {
	// The location to search for Plugin. Default is .
	PluginLocation string
	// To enable and disable autoDisover. For auto Loading autoDiscover
	// Should be enabled. Default is false
	AutoDiscover bool
	// The conf Extension. Default is .conf
	ConfExt string
}

PluginRegConf provides the configuration to create a plugin registry

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL