application

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2020 License: MIT Imports: 9 Imported by: 0

README

go-application

Go application

Build Status

start an application

func run1(ctx context.Context) {
	for {
		select {
		case <-ctx.Done():
			// done, exit loop now
			return
		default:
		}
	}
}

func run2(ctx context.Context) {
	for {
		select {
		case <-ctx.Done():
			// done, exit loop now
			return
		default:
		}
	}
}


func main() {
app := application.NewApplication(nil)
app.Start(run1)  // start a goroutine for run1
app.Start(run2)  // start a goroutine for run2
app.Background()  // wait until ctrl-c is pressed
}
func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
	defer cancel()
	app := application.NewApplication(ctx)
	app.Start(run1)  // start a goroutine for run1
	app.Start(run2)  // start a goroutine for run2
	app.Wait() // wait until run1 & run2 completed, or after 1 hr

Executor


func afunc(ctx context.Context, data interface{}) {
	// the function to be called later
}
// start 10 goroutines to execute the given func
executor := application.NewExecutor(context.Background(), 10)

// submit afunc to be call later after a second
executor.ExecuteLater(ctx, afunc, nil, time.Second)

// submit afunc to be called by the 10 goroutine
executor.Submit(ctx, afunc, nil)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitGroupFromContext

func WaitGroupFromContext(ctx context.Context) *sync.WaitGroup

WaitGroupFromContext get a sync.WaitGroup from context, if available

Types

type IApplication

type IApplication interface {

	// Background run the application in background, and wait for OS signal to terminate
	Background()

	// Run a runnable in a goroutine
	Start(runnable Runnable)

	// Stop the application
	Stop()

	// Run and wait all runnable stopped
	Wait()
}

IApplication interface

func NewApplication

func NewApplication(ctx context.Context) IApplication

NewApplication create a new application that can run in background

type ICallable

type ICallable func(ctx context.Context, data interface{})

ICallable callable

type IContextKey

type IContextKey interface {
	ToString(ctx context.Context) string
}

IContextKey interface

func NewContextKey

func NewContextKey(name string) IContextKey

NewContextKey create a new context key

type IExecutor

type IExecutor interface {
	// Submit a callable to be called later
	Submit(ctx context.Context, callable ICallable, data interface{})

	// Execute a function later using the goroutine pool defined
	ExecuteLater(ctx context.Context, callable ICallable, data interface{}, delay time.Duration)

	// Shutdown the executors
	Shutdown()

	// Count number of task submitted + executeLater
	Count() int64
}

func NewExecutor

func NewExecutor(ctx context.Context, executorCount int) IExecutor

type IThread

type IThread interface {
	Start()
}

type Runnable

type Runnable func(ctx context.Context)

Runnable function signature

Jump to

Keyboard shortcuts

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