subrpc

package module
v0.0.0-...-1a56684 Latest Latest
Warning

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

Go to latest
Published: May 6, 2021 License: GPL-3.0 Imports: 11 Imported by: 0

README

libsubrpc

libsubrpc provides subprocess management and RPC communication in a single package

Purpose

The purpose is to provide a framework for allowing a plugin/dll-style system for Go given its lack of plugin or dynamic symbol loading.

Basic Usage

Main App

manager := subrpc.NewManager() // make a new Manager instance

// add a few subprocesses to the manager
manager.NewProcess(subrpc.ProcessOptions{
    // Name should be unique
    Name: "foo",

    // Absolute path is best
    Exepath: "/path/to/process/binary/foo",
})

manager.NewProcess(subrpc.ProcessOptions{
    // Name should be unique
    Name: "bar",
    
    // Absolute path is best
    Exepath: "/path/to/process/binary/bar",

    // You can set a custom socket path if you want, otherwise
    // a random one is generated
    SockPath: "/path/to/custom/socket",

    // Custom environment variables to pass into the process
    Env: []string{
        "MYVAR=myvalue",
        "OTHERVAR=other_value",
    }
})

// Read Stdin * Stderr from our subprocesses for logging purposes
// the Manager struct exposes OutBuffer and ErrBuffer buffers that
// combine the stdout and stderr of all processes, respectively.
go func() {
    for {
		l, err := manager.OutBuffer.ReadString('\n')
		if err != nil && err != io.EOF {
			fmt.Println(err)
		}
		if l != "" {
			fmt.Println(l)
		}
		l, err = manager.ErrBuffer.ReadString('\n')
		if err != nil && err != io.EOF {
			fmt.Print(err)
		}
		if l != "" {
			fmt.Println(l)
		}
		time.Sleep(1 * time.Second)
	}
}()

// Start all the processes (returns error)
_ = manager.StartAllProcess()


// Create a variable to put the results into
var result json.RawMessage

// Call the `foo` process and request the `test_service` endpoint.
// Endpoints are underscore delimited (see subprocess implementation)
_ = manager.Call("foo:test_service", &result)

// Call the `bar` process with some args and save it to `result`
_ = manager.Call("bar:other_service", &result, "something", 1, 2, 5)

// Stops all running processes
manager.StopAll()

Subprocess App

// Define a service here
type Test struct {}

// Service handler; to call this you would call `test_service`
func (t *Test) Service() string {
    return "this is a test"
}

proc := subrpc.Process()

// add the Test object as an RPC handler under the `test_` prefix
proc.AddFunction("test", new(Test))

proc.Start() // start listening for connections; blocking


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	ServerSocket string
	SocketDir    string
	Procs        map[string]map[string]*ProcessInfo
	OutBuffer    *bytes.Buffer
	ErrBuffer    *bytes.Buffer
	Errors       chan error
	Metrics      chan Metrics
	RPC          *rpc.Server
	// contains filtered or unexported fields
}

Manager type instantiates a new Manager instance

func NewManager

func NewManager() (*Manager, error)

NewManager function returns a new instance of the Manager object

func (*Manager) Call

func (m *Manager) Call(urn string, dst interface{}, args ...interface{}) error

Call function calls an RPC service with the supplied "name:function" string

func (*Manager) NewProcess

func (m *Manager) NewProcess(options ...ProcessOptions) error

NewProcess instantiates new processes

func (*Manager) RestartProcess

func (m *Manager) RestartProcess(name string, typ string) error

RestartProcess restarts a process

func (*Manager) StartAllProcess

func (m *Manager) StartAllProcess() []error

StartAllProcess starts all procs in the manager

func (*Manager) StartProcess

func (m *Manager) StartProcess(name string, typ string) error

StartProcess starts all of the sub processes

func (*Manager) StopAll

func (m *Manager) StopAll() []error

StopAll stopps all procs

func (*Manager) StopProcess

func (m *Manager) StopProcess(name string, typ string) error

StopProcess stopps a process by name

type ManagerService

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

ManagerService type

func (*ManagerService) Ping

func (ms *ManagerService) Ping() string

Ping function

type Metrics

type Metrics struct {
	URN      string
	CallTime time.Duration
	Error    bool
}

Metrics type

type Process

type Process struct {
	Socket       string
	Config       []byte
	Env          []string
	RPC          *rpc.Server
	Token        string
	ServerSocket string
	Srv          *rpc.Client
}

Process type represents an RPC service

func NewProcess

func NewProcess() *Process

NewProcess function

func (*Process) AddFunction

func (p *Process) AddFunction(name string, f interface{}) error

AddFunction adds a function to the RPC handler

func (*Process) Call

func (p *Process) Call(method string, dst interface{}, args ...interface{}) error

Call function

func (*Process) Start

func (p *Process) Start() error

Start starts a new process instance

type ProcessInfo

type ProcessInfo struct {
	Name         string
	Type         string
	Config       map[string]interface{}
	Token        string
	Handler      interface{}
	CMD          *airboss.Subprocess
	Options      ProcessOptions
	Running      bool
	Terminate    chan bool
	PID          int
	Socket       string
	RPC          *rpc.Client
	StartupDelay time.Duration
}

ProcessInfo holds information about running processes

type ProcessInput

type ProcessInput struct {
	Socket       string
	ServerSocket string
	Token        string
	Config       []byte
}

ProcessInput type

type ProcessOptions

type ProcessOptions struct {
	Name         string
	Type         string
	Config       map[string]interface{}
	Handler      interface{}
	ExePath      string
	Socket       string
	Env          map[string]string
	Token        string
	StartupDelay time.Duration
}

ProcessOptions allows for passing process options to NewProcess

Jump to

Keyboard shortcuts

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