wait

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package wait is a library for waiting on events. It currently provides functions for waiting for one or more TCP servers to be ready.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllTCP

func AllTCP(specs []*TCPSpec, waitTimeout time.Duration) <-chan *TCPMessage

AllTCP waits until connections can be made to all given TCP input specifications for at most `waitTimeout` long. It returns a channel through which all wait operation-related messages will be sent. The returned channel is closed after all wait operations have finished.

func OneTCP

func OneTCP(spec *TCPSpec, waitTimeout time.Duration) <-chan *TCPMessage

OneTCP waits until a TCP connection can be made to an address, attempting a connection every defined interval. Both of these are contained in the given specifications. It also accepts a context function, which it uses to listen to cancellation events from the parent context. The returned channel is closed after the wait operation has finished or if the parent context is cancelled.

Types

type Message

type Message interface {
	// Status returns the status of the message.
	Status() Status
	// Target returns the entity being waited.
	Target() string
	// Err returns an error, if the message contains any.
	Err() error
	// ElapsedTime returns the duration of the wait operation at the time of message creation.
	ElapsedTime() time.Duration
}

Message is the interface for messages sent by the wait operations.

type Status

type Status int

Status enumerates possible waiting status.

const (
	// Start is the status emitted at the beginning of the wait operation.
	Start Status = iota
	// Ready is the status for when the wait operation finishes successfully.
	Ready
	// Failed is the status for when the wait operation failed.
	Failed
)

func (Status) String

func (s Status) String() string

String returns the string representation of the Status enum.

type TCPMessage

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

TCPMessage is a container for wait operations on TCP servers.

func (*TCPMessage) Addr

func (msg *TCPMessage) Addr() string

Addr returns the address being waited. If the specifications is nil, this returns `<none>`.

func (*TCPMessage) ElapsedTime

func (msg *TCPMessage) ElapsedTime() time.Duration

ElapsedTime is the duration between waiting operation start and status emission.

func (*TCPMessage) Err

func (msg *TCPMessage) Err() error

Err returns the error contained in the message, if any.

func (*TCPMessage) Status

func (msg *TCPMessage) Status() Status

Status returns the status of the message.

func (*TCPMessage) Target

func (msg *TCPMessage) Target() string

Target returns the target of the wait operation, which is `tcp://` prepended to Addr. If the specifications is nil, this returns `<none>`.

type TCPSpec

type TCPSpec struct {
	// Host is the hostname or IP address being waited.
	Host string
	// Port is the port number for the connection.
	Port string
	// PollFreq is how often a connection is attempted.
	PollFreq time.Duration
}

TCPSpec represents the input specification of a single TCP wait operation.

func ParseTCPSpec

func ParseTCPSpec(rawAddr string, defaultPollFreq time.Duration) (*TCPSpec, error)

ParseTCPSpec parses the given address into a TCPSpec and then returns a pointer to it. The address can be given in several forms: `<host>:<port>`, `<protocol>://<host>`, or `<protocol>://<host>:<port>`. For the second form, if the protocol is known, the port will be inferred from it (e.g. port 80 for HTTP and 443 for HTTPS). For the last form, the `<protocol>` is ignored. This function also takes a `defaultPollFreq` argument, which it will use as the poll frequency of the TCPSpec if the raw address does not specify a poll frequency value. The poll frequency value in the raw address is the string value of time.Duration, appended to the address after a `#` sign.

Example
spec, _ := ParseTCPSpec("golang.org:80", 1*time.Second)
fmt.Println("host:", spec.Host)
fmt.Println("port:", spec.Port)
fmt.Println("poll freq:", spec.PollFreq)
Output:

host: golang.org
port: 80
poll freq: 1s
Example (Freq)
spec, _ := ParseTCPSpec("amqps://127.0.0.1#500ms", 1*time.Second)
fmt.Println("host:", spec.Host)
fmt.Println("port:", spec.Port)
fmt.Println("poll freq:", spec.PollFreq)
Output:

host: 127.0.0.1
port: 5671
poll freq: 500ms
Example (Proto)
spec, _ := ParseTCPSpec("https://golang.org", 1*time.Second)
fmt.Println("host:", spec.Host)
fmt.Println("port:", spec.Port)
fmt.Println("poll freq:", spec.PollFreq)
Output:

host: golang.org
port: 443
poll freq: 1s

func ParseTCPSpecs

func ParseTCPSpecs(rawAddrs []string, defaultPollFreq time.Duration) ([]*TCPSpec, error)

ParseTCPSpecs parses multiple addresses into separate TCPSpecs, returned as a slice of pointers. It has the same semantics as `ParseTCPSpec`, only it works with multiple addresses instead of one.

func (*TCPSpec) Addr

func (spec *TCPSpec) Addr() string

Addr returns the host and port of the TCP specifications, joined by ':'.

Jump to

Keyboard shortcuts

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