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 ¶
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) Close ¶
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.