kernel

package
v0.0.0-...-e9d698b Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2017 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

This is the basic config for oryx.

This is the context for logger.

This is the listeners for oryx.

This is the process pool for oryx.

This is the sync objects for multiple goroutine to work together.

This is the version for oryx.

Index

Examples

Constants

View Source
const (
	Major    = 0
	Minor    = 0
	Revision = 2
)

Variables

This section is empty.

Functions

func Version

func Version() string

The version format in Major.Minor.Revision

Types

type Config

type Config struct {
	Logger struct {
		Tank     string `json:"tank"`
		FilePath string `json:"file"`
	} `json:"logger"`
}

The basic config, for all modules which will provides these config.

func (*Config) Close

func (v *Config) Close() error

The interface io.Closer Cleanup the resource open by config, for example, the logger file.

func (*Config) OpenLogger

func (v *Config) OpenLogger() (err error)

Open the logger, when tank is file, switch logger to file.

func (*Config) String

func (v *Config) String() string

The interface fmt.Stringer

type Context

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

The context for logger.

Example
package main

import (
	ol "github.com/ossrs/go-oryx-lib/logger"
	"github.com/ossrs/go-oryx/kernel"
)

func main() {
	ctx := &kernel.Context{}
	ol.T(ctx, "create context ok")
}
Output:

func (*Context) Cid

func (v *Context) Cid() int

type ProcessPool

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

The pool for process, when user create pool, user can exec processes, wait for terminated process and close all.

Example
package main

import (
	"github.com/ossrs/go-oryx/kernel"
	"os/exec"
)

func main() {
	pp := kernel.NewProcessPool()
	defer pp.Close()

	// start a worker.
	var err error
	var cmd *exec.Cmd
	if cmd, err = pp.Start(nil, "ls", "-al"); err != nil {
		return
	}

	// start other workers.

	// wait for some processes to quit.
	var c *exec.Cmd
	if c, err = pp.Wait(); err != nil {
		return
	}

	// do something for the terminated process.
	if cmd == c {
		// cmd must be c.
	}
}
Output:

func NewProcessPool

func NewProcessPool() *ProcessPool

func (*ProcessPool) Close

func (v *ProcessPool) Close() (err error)

io.Closer User should reuse the closed process pool.

func (*ProcessPool) Start

func (v *ProcessPool) Start(ctx ol.Context, name string, arg ...string) (c *exec.Cmd, err error)

Start new process, user can start many processes.

func (*ProcessPool) Wait

func (v *ProcessPool) Wait() (p *exec.Cmd, err error)

Wait for a dead process. @return error io.EOF when pool closed. @remark the error may indicates the process not terminate success, user must handle it.

type TcpListeners

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

The tcp listeners which support reload.

Example
package main

import (
	"github.com/ossrs/go-oryx/kernel"
)

func main() {
	// create listener by addresses
	ls, err := kernel.NewTcpListeners([]string{"tcp://:1935", "tcp://:1985", "tcp://:8080"})
	if err != nil {
		return
	}
	defer ls.Close()

	// listen all addresses
	if err = ls.ListenTCP(); err != nil {
		return
	}

	// accept conn from any listeners
	for {
		conn, err := ls.AcceptTCP()
		if err != nil {
			return
		}

		// serve and close conn
		defer conn.Close()
	}
}
Output:

func NewTcpListeners

func NewTcpListeners(addrs []string) (v *TcpListeners, err error)

Listen at addrs format as netowrk://laddr, for example, tcp://:1935, tcp4://:1935, tcp6://1935, tcp://0.0.0.0:1935

func (*TcpListeners) AcceptTCP

func (v *TcpListeners) AcceptTCP() (c *net.TCPConn, err error)

@remark when user closed the listener, err is io.EOF.

func (*TcpListeners) Close

func (v *TcpListeners) Close() (err error)

io.Closer User should never reuse the closed instance.

func (*TcpListeners) ListenTCP

func (v *TcpListeners) ListenTCP() (err error)

type TerminatedProcess

type TerminatedProcess struct {
	// The process command object.
	Process *exec.Cmd
	// The error got by wait.
	WaitError error
}

The dead body of process.

type WorkerGroup

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

a group of worker, each is a goroutine.

Example
package main

import (
	oa "github.com/ossrs/go-oryx-lib/asprocess"
	"github.com/ossrs/go-oryx/kernel"
	"io"
	"net"
	"syscall"
	"time"
)

func main() {
	// other goroutine to notify worker group to quit.
	closing := make(chan bool, 1)
	// for example, user can use asprocess to watch without exit to write closing.
	oa.WatchNoExit(nil, oa.Interval, closing)

	// use group to sync workers.
	wg := kernel.NewWorkerGroup()
	defer wg.Close()

	// quit for external events.
	wg.QuitForChan(closing)
	// quit for signals.
	wg.QuitForSignals(nil, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

	// start goroutine to worker, quit when worker quit.
	// for example, the listener for http.
	var listener net.Listener
	wg.ForkGoroutine(func() {
		for {
			if conn, err := listener.Accept(); err != nil {
				if err != io.EOF {
					// log error.
				}
				return
			} else {
				// serve conn

				defer conn.Close()
			}
		}
	}, func() {
		listener.Close()
	})

	// start goroutine without cleanup.
	var closed bool
	wg.ForkGoroutine(func() {
		for !closed {
			// do something util quit.
			time.Sleep(time.Duration(1) * time.Second)
		}
	}, func() {
		closed = true
	})

	// wait for quit.
	wg.Wait()
}
Output:

func NewWorkerGroup

func NewWorkerGroup() *WorkerGroup

func (*WorkerGroup) Close

func (v *WorkerGroup) Close() error

interface io.Closer notify and wait for all workers to quit.

func (*WorkerGroup) ForkGoroutine

func (v *WorkerGroup) ForkGoroutine(pfn func(), cleanup func())

start new goroutine to run pfn. @remark the worker group will quit when each pfn done. @remark cleanup should never be nil.

func (*WorkerGroup) QuitForChan

func (v *WorkerGroup) QuitForChan(closing chan bool)

when got singal from this chan, quit.

func (*WorkerGroup) QuitForSignals

func (v *WorkerGroup) QuitForSignals(ctx ol.Context, signals ...os.Signal)

quit when got these signals.

func (*WorkerGroup) Wait

func (v *WorkerGroup) Wait()

wait util got quit signal.

Jump to

Keyboard shortcuts

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