leakless

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: MIT Imports: 14 Imported by: 0

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.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLeaklessBin

func GetLeaklessBin() string

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

func LockPort

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

func Support() bool

Support returns true if the OS is supported by leakless.

Types

type Launcher

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

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

func (l *Launcher) Err() string

Err message from the guard process

func (*Launcher) Pid

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

Jump to

Keyboard shortcuts

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