hotplugin

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: MIT Imports: 12 Imported by: 0

README

Build Status

hotplugin

golang plugin framework for hot update, go version >= 1.8

usage

  1. get hotplugin
go get github.com/letiantech/hotplugin
  1. write a plugin with Load, Unload and other functions like this
//testplugin.go
package main

import (
	"fmt"
	"log"
)

const (
	pluginName    = "testplugin"
	pluginVersion = 0x00010000
)

func Load(register func(name string, version uint64) error) error {
	err := register(pluginName, pluginVersion)
	if err != nil {
		log.Println(err.Error())
		return err
	}
	log.Println("loading test plugin")
	return nil
}

func Unload() error {
	fmt.Printf("unload %s, version: 0x%x\n", pluginName, pluginVersion)
	return nil
}

func Test(data string) string {
	return "hello " + data
}
  1. build your plugin
go build -buildmode=plugin ./testplugin.go
  1. save your testplugin.so to /path/of/plugin/dir

  2. write main.go like this

//main.go
package main

import (
	"fmt"
	"github.com/letiantech/hotplugin"
)

func main() {
	options := hotplugin.ManagerOptions{
		Dir:    "./",
		Suffix: ".so",
	}
	hotplugin.StartManager(options)
	result := hotplugin.Call("testplugin", "Test", "my world")
	fmt.Println(result...)
}

Documentation

Index

Constants

View Source
const PluginTimeout = 100 * time.Millisecond

Variables

This section is empty.

Functions

func Call

func Call(module, function string, args ...interface{}) []interface{}

func GetFunc

func GetFunc(module, function string) (f func(...interface{}) []interface{}, err error)

func StartManager

func StartManager(options ManagerOptions) error

Types

type Manager

type Manager interface {
	Run() error
	IsRunning() bool
	GetPlugin(name string) (*Plugin, error)
	GetPluginWithVersion(name string, version uint64) (*Plugin, error)
	GetFunc(module, function string) (f func(...interface{}) []interface{}, err error)
	Call(module, function string, args ...interface{}) []interface{}
	OnLoaded(p *Plugin)
	OnReloaded(p *Plugin)
	OnUnloaded(p *Plugin)
	OnError(p *Plugin, err *PluginError)
}

func NewManager

func NewManager(options ManagerOptions) (Manager, error)

type ManagerOptions

type ManagerOptions struct {
	Dir    string
	Suffix string
}

type Plugin

type Plugin struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func GetPlugin

func GetPlugin(name string) (*Plugin, error)

func GetPluginWithVersion

func GetPluginWithVersion(name string, version uint64) (*Plugin, error)

func NewPlugin

func NewPlugin(path string, m Manager) *Plugin

func (*Plugin) Call

func (p *Plugin) Call(fun string, params ...interface{}) []interface{}

func (*Plugin) GetFunc

func (p *Plugin) GetFunc(fun string) (f func(...interface{}) []interface{}, err error)

func (*Plugin) Load

func (p *Plugin) Load() error

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) Path

func (p *Plugin) Path() string

func (*Plugin) Reload

func (p *Plugin) Reload() error

func (*Plugin) Status

func (p *Plugin) Status() PluginStatus

func (*Plugin) Unload

func (p *Plugin) Unload() error

func (*Plugin) Version

func (p *Plugin) Version() uint64

type PluginError

type PluginError struct {
	Type int
	Err  error
}

type PluginFunc

type PluginFunc func(...interface{}) []interface{}

type PluginStatus

type PluginStatus int32
const (
	PluginStatusNone PluginStatus = iota
	PluginStatusLoading
	PluginStatusLoaded
	PluginStatusReloading
	PluginStatusUnloading
	PluginStatusUnloaded
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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