base

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: Apache-2.0 Imports: 15 Imported by: 127

Documentation

Overview

Package base provides common utilities for other stenographer libraries.

Index

Constants

This section is empty.

Variables

View Source
var (
	AllPositions = Positions{-1}
	NoPositions  = Positions{}
)
View Source
var VerboseLogging = flag.Int("v", -1, "log many verbose logs")

Functions

func ContextDone

func ContextDone(ctx context.Context) bool

ContextDone returns true if a context is complete.

func PacketsToFile

func PacketsToFile(in *PacketChan, out io.Writer, limit Limit) error

PacketsToFile writes all packets from 'in' to 'out', writing out all packets in a valid PCAP file format.

func PathDiskFreePercentage

func PathDiskFreePercentage(path string) (int, error)

func V

func V(level int, fmt string, args ...interface{})

V provides verbose logging which can be turned on/off with the -v flag.

func Watchdog

func Watchdog(d time.Duration, msg string) *time.Timer

Watchdog returns a time.Timer which log.Fatals if it goes off. The creator must call Stop before that time (to never die) or Reset (to postpone the inevitable).

Usage:

func couldGetStuck() {
  defer base.Watchdog(time.Minute * 5, "my description").Stop()
  ... do stuff ...
}

Or:

func couldGetStuckOnManyThings(things []thing) {
  fido := base.Watchdog(time.Second * 15)
  defer fido.Stop()
  initialize()  // can take up to 15 secs
  for _, thing := range things {
    fido.Reset(time.Second * 5)
    process(thing)  // can take up to 5 seconds each
  }
}

Types

type Context

type Context interface {
	context.Context
	Cancel()
}

Context wraps a context.Context with its cancel function.

func NewContext

func NewContext(timeout time.Duration) Context

WrapContext wraps a context.Context in our type of Context. Timeout of zero means never time out. Cancel function should be called when operation is completed.

type Limit

type Limit struct {
	Bytes, Packets int64
}

Limit is the amount of data we want to return, or the amount taken by a single upload.

func LimitFromHeaders

func LimitFromHeaders(h http.Header) (a Limit, err error)

LimitFromHeaders returns a Limit based on HTTP headers.

func (*Limit) ShouldStopAfter

func (a *Limit) ShouldStopAfter(b Limit) bool

ShouldStopAfter returns true if output should stop after the next update, where the next update is of size 'b'.

type Packet

type Packet struct {
	Data                 []byte // The actual bytes that make up the packet
	gopacket.CaptureInfo        // Metadata about when/how the packet was captured
}

Packet is a single packet with its metadata.

type PacketChan

type PacketChan struct {

	// C can be used to send packets on this channel in a select.  Do NOT
	// call 'close' on it... instead call the Close function.
	C chan<- *Packet
	// contains filtered or unexported fields
}

PacketChan provides an async method for passing multiple ordered packets between goroutines.

func ConcatPacketChans

func ConcatPacketChans(ctx context.Context, in <-chan *PacketChan) *PacketChan

ConcatPacketChans concatenates packet chans in order.

func MergePacketChans

func MergePacketChans(ctx context.Context, in []*PacketChan) *PacketChan

MergePacketChans merges an incoming set of packet chans, each sorted by time, returning a new single packet chan that's also sorted by time.

func NewPacketChan

func NewPacketChan(buffer int) *PacketChan

NewPacketChan returns a new PacketChan channel for passing packets around.

func (*PacketChan) Close

func (p *PacketChan) Close(err error)

Close closes the sending channel and sets the PacketChan's error based in its input.

func (*PacketChan) Discard

func (p *PacketChan) Discard()

Discard discards all remaining packets on the receiving end. If you stop using the channel before reading all packets, you must call this function. It's a good idea to defer this regardless.

func (*PacketChan) Done

func (p *PacketChan) Done() <-chan struct{}

Done returns a channel that is closed when this packet channel is complete.

func (*PacketChan) Err

func (p *PacketChan) Err() error

Err gets the current error for the channel, if any exists. This may be called during Next(), but if an error occurs it may only be set after Next() returns false the first time.

func (*PacketChan) Receive

func (p *PacketChan) Receive() <-chan *Packet

Receive provides the channel from which to read packets. It always returns the same channel.

func (*PacketChan) Send

func (p *PacketChan) Send(pkt *Packet)

Send sends a single packet on the channel to the receiver.

type Positions

type Positions []int64

Positions detail the offsets of packets within a blockfile.

func (Positions) Intersect

func (a Positions) Intersect(b Positions) (out Positions)

Intersect returns the intersection of a and b. a and b must be sorted in advance. Returned slice will be sorted. a or b may be returned by Intersect, but neither a nor b will be modified.

func (Positions) IsAllPositions

func (p Positions) IsAllPositions() bool

func (Positions) Len

func (a Positions) Len() int

func (Positions) Less

func (a Positions) Less(i, j int) bool

func (Positions) Sort

func (a Positions) Sort()

func (Positions) Swap

func (a Positions) Swap(i, j int)

func (Positions) Union

func (a Positions) Union(b Positions) (out Positions)

Union returns the union of a and b. a and b must be sorted in advance. Returned slice will be sorted. a or b may be returned by Union, but neither a nor b will be modified.

Jump to

Keyboard shortcuts

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