ipc

package
v0.0.0-...-401321f Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 3 Imported by: 0

README

ipc GoDoc

import "github.com/VerticalOps/fakesentry/ipc"

Overview

Package ipc implements a simple set of intra-process-communication net.Conn types, specifically a net.Listener and an associated Dialer that can be used in place of http Servers/Clients or anything that needs a net.Conn to work or test within the same process. This package is built around net.Pipe and so the network is not actually involved. If a buffered implementation is needed there are alternatives.

Index

Examples
Package files

ipc.go

Variables

var ErrListenerClosed = errors.New("fakesentry/ipc: Listener was closed")

ErrListenerClosed is returned from Dial attempts after its associated Listener has been closed. It is also returned from Listener.Accept and Listener.Close if the Listener has been closed once already.

type Dialer

type Dialer interface {
    Dial(network, address string) (net.Conn, error)
    DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer can be used in place of many Dial-related functions of the net package and others. The returned net.Conn is always related to the Listener that returned the Dialer via Listener.NewDialer. Therefore the passed network and address are ignored. The given context is not however, if the context expires before the connection is made then ctx.Err is returned.

type Listener

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

Listener is a simple intra-process-communication type that returns in-memory net.Conn's for testing and other uses. It implements net.Listener though it does not listen on anything network related. NewDialer may be used to return a Dialer that communicates with the Listener.

func NewListener
func NewListener() Listener

NewListener returns a new intra-process Listener for use.

func (Listener) Accept
func (l Listener) Accept() (net.Conn, error)

Accept returns the next intra-process connection for use.

func (Listener) Addr
func (addr Listener) Addr() net.Addr
func (Listener) Close
func (l Listener) Close() error

Close closes the Listener, it stops all Accept calls as well as all Dials to the Listener. Note that it does not close any connections created by the listener, nor is this method safe to call concurrently. It is however fine to call more than once.

func (Listener) Network
func (Listener) Network() string
func (Listener) NewDialer
func (l Listener) NewDialer() Dialer

NewDialer returns a new Dialer that can be used to dail the given Listener.

func (Listener) String
func (Listener) String() string

Generated by godoc2md

Documentation

Overview

Package ipc implements a simple set of intra-process-communication net.Conn types, specifically a net.Listener and an associated Dialer that can be used in place of http Servers/Clients or anything that needs a net.Conn to work or test within the same process. This package is built around net.Pipe and so the network is not actually involved. If a buffered implementation is needed there are alternatives.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"

	"github.com/VerticalOps/fakesentry/ipc"
)

func main() {
	listener := ipc.NewListener()
	defer listener.Close()

	dialer := listener.NewDialer()

	srv := &http.Server{
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintln(w, "Hello World!")
		}),
	}

	//Client setup to use Dialer from Listener
	client := &http.Client{
		Transport: &http.Transport{
			DialContext: dialer.DialContext,
		},
	}

	//Server setup to use Listener
	go srv.Serve(listener)
	defer srv.Close()

	//Does not actually connect to localhost:80
	resp, err := client.Get("http://localhost/myurl")
	if err != nil {
		//Actually handle error
		log.Fatalf("Client HTTP GET: %v", err)
	}
	defer resp.Body.Close()

	io.Copy(os.Stdout, resp.Body)
}
Output:

Hello World!

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrListenerClosed = errors.New("fakesentry/ipc: Listener was closed")

ErrListenerClosed is returned from Dial attempts after its associated Listener has been closed. It is also returned from Listener.Accept and Listener.Close if the Listener has been closed once already.

Functions

This section is empty.

Types

type Dialer

type Dialer interface {
	Dial(network, address string) (net.Conn, error)
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer can be used in place of many Dial-related functions of the net package and others. The returned net.Conn is always related to the Listener that returned the Dialer via Listener.NewDialer. Therefore the passed network and address are ignored. The given context is not however, if the context expires before the connection is made then ctx.Err is returned.

type Listener

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

Listener is a simple intra-process-communication type that returns in-memory net.Conn's for testing and other uses. It implements net.Listener though it does not listen on anything network related. NewDialer may be used to return a Dialer that communicates with the Listener.

func NewListener

func NewListener() Listener

NewListener returns a new intra-process Listener for use.

func (Listener) Accept

func (l Listener) Accept() (net.Conn, error)

Accept returns the next intra-process connection for use.

func (Listener) Addr

func (addr Listener) Addr() net.Addr

func (Listener) Close

func (l Listener) Close() error

Close closes the Listener, it stops all Accept calls as well as all Dials to the Listener. Note that it does not close any connections created by the listener, nor is this method safe to call concurrently. It is however fine to call more than once.

func (Listener) Network

func (Listener) Network() string

func (Listener) NewDialer

func (l Listener) NewDialer() Dialer

NewDialer returns a new Dialer that can be used to dail the given Listener.

func (Listener) String

func (Listener) String() string

Jump to

Keyboard shortcuts

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