leakless

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 15 Imported by: 11

README

leakless

Run sub-process and make sure to kill it when the parent process exits. The way how it works is to output a standalone executable file to guard the subprocess and check parent TCP connection with a UUID. So that it works consistently on Linux, Mac, and Windows.

If you don't trust the executable, you can build it yourself from the source code by running go generate at the root of this repo, then use the replace to use your own module. Usually, it won't be a concern, all the executables are committed by this Github Action, the Action will print the hash of the commit, you can compare it with the repo.

Not using the PID is because after a process exits, a newly created process may have the same PID.

How to Use

See the examples.

Custom build for GOOS or GOARCH

Such as if you want to support FreeBSD, you can clone this project and modify the targets.go to something like:

var targets = []utils.Target{
    "freebsd/amd64",
}

Then run go generate and use replace in the project that will use leakless. You can keep this fork of leakless to serve your own interest.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLeaklessBin added in v0.6.2

func GetLeaklessBin() string

GetLeaklessBin returns the executable path of the guard, if it doesn't exists create one.

func LockPort added in v0.6.3

func LockPort(port int) func()

LockPort uses a tcp port to create a mutex lock for cross-process locking. It will poll the port to check if it's free.

func Support added in v0.5.5

func Support() bool

Support returns true if the OS is supported by leakless.

Types

type Launcher added in v0.2.0

type Launcher struct {
	// Lock for leakless.LockPort, default is 2978
	Lock int
	// contains filtered or unexported fields
}

Launcher struct

func New

func New() *Launcher

New leakless instance

Example
package main

import (
	"fmt"
	"time"

	"github.com/ysmood/leakless"
)

func main() {
	ll := leakless.New()

	// just like using the exec.Command
	_ = ll.Command("sleep", "3").Start()

	// get the pid of the guarded sub-process
	select {
	case <-time.Tick(3 * time.Second):
		panic("timeout")
	case pid := <-ll.Pid():
		fmt.Println(pid)
	}
}
Output:

func (*Launcher) Command added in v0.2.0

func (l *Launcher) Command(name string, arg ...string) *exec.Cmd

Command will try to download the leakless bin and prefix the exec.Cmd with the leakless options.

func (*Launcher) Err added in v0.5.1

func (l *Launcher) Err() string

Err message from the guard process

func (*Launcher) Pid added in v0.2.0

func (l *Launcher) Pid() chan int

Pid signals the pid of the guarded sub-process. The channel may never receive the pid.

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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