Documentation
¶
Overview ¶
Example (Http) ¶
Example_http illustrates how to create an HTTP server and client using memconn.
package main import ( "context" "fmt" "io" "net" "net/http" "os" "github.com/akutz/memconn" ) func main() { // Create a new, named listener using the in-memory, unbuffered // network "memu" and address "MyNamedNetwork". lis, err := memconn.Listen("memu", "MyNamedNetwork") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // Create a new HTTP mux and register a handler with it that responds // to requests with the text "Hello, world.". mux := http.NewServeMux() mux.Handle("/", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world.") })) // Start an HTTP server using the HTTP mux. go func() { if err := http.Serve(lis, mux); err != http.ErrServerClosed { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }() // Create a new HTTP client that delegates its dialing to memconn. client := &http.Client{ Transport: &http.Transport{ DialContext: func( ctx context.Context, _, _ string) (net.Conn, error) { return memconn.DialContext(ctx, "memu", "MyNamedNetwork") }, }, } // Get the root resource and copy its response to os.Stdout. Please // note that the URL must contain a host name, even if it's ignored. rep, err := client.Get("http://host/") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer rep.Body.Close() if _, err := io.Copy(os.Stdout, rep.Body); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: Hello, world.
Example (Http_mapped_network) ¶
Example_http_mapped_network illustrates how to create an HTTP server and client with the "tcp" network mapped to "memu" to make creating an HTTP client easier.
package main import ( "fmt" "io" "net/http" "os" "github.com/akutz/memconn" ) func main() { // MemConn's MapNetwork function enables mapping known network // types to different types. This is useful for when using MemConn // with Golang's "http" package since the "http.Client" builds // the network address from the provided resource URL. Please // see the next comment for more information. memconn.MapNetwork("tcp", "memu") // Create a new, named listener using the in-memory, unbuffered // network "memu". // // Please note that the listener's address is "host:80". While // MemConn names do not need to be unique and are free-form, this // name was selected due to the way the "http.Client" builds the // network address from the provided resource URL. // // For example, if the request is "GET http://host/" then the HTTP // client dials "network=tcp, address=host:80". In combination // with the above "MapNetwork" function this means creating // a MemConn listener with the address "host:80" requires no special // address translation is required for the when using // "memconn.DialContext" with the HTTP client transport. lis, err := memconn.Listen("memu", "host:80") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // Create a new HTTP mux and register a handler with it that responds // to requests with the text "Hello, world.". mux := http.NewServeMux() mux.Handle("/", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world.") })) // Start an HTTP server using the HTTP mux. go func() { if err := http.Serve(lis, mux); err != http.ErrServerClosed { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }() // Create a new HTTP client that delegates its dialing to memconn. client := &http.Client{ Transport: &http.Transport{DialContext: memconn.DialContext}, } // Get the root resource and copy its response to os.Stdout. The // absence of a port means the address sent to the HTTP client's // dialer will be "host:80". rep, err := client.Get("http://host/") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer rep.Body.Close() if _, err := io.Copy(os.Stdout, rep.Body); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: Hello, world.
Index ¶
- func Dial(network, address string) (net.Conn, error)
- func DialContext(ctx context.Context, network, address string) (net.Conn, error)
- func Listen(network, address string) (net.Listener, error)
- func MapNetwork(from, to string)
- func Pipe() (net.Conn, net.Conn)
- type Addr
- type Conn
- func (c *Conn) BufferSize() uint64
- func (c *Conn) Close() error
- func (c *Conn) CloseTimeout() time.Duration
- func (c *Conn) CopyOnWrite() bool
- func (c *Conn) Errs() <-chan error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) LocalBuffered() bool
- func (c *Conn) Read(b []byte) (int, error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) RemoteBuffered() bool
- func (c *Conn) SetBufferSize(i uint64)
- func (c *Conn) SetCloseTimeout(duration time.Duration)
- func (c *Conn) SetCopyOnWrite(enabled bool)
- func (p *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(b []byte) (int, error)
- type Listener
- type Provider
- func (p *Provider) Dial(network, address string) (net.Conn, error)
- func (p *Provider) DialContext(ctx context.Context, network, address string) (net.Conn, error)
- func (p *Provider) DialMem(network string, laddr, raddr *Addr) (*Conn, error)
- func (p *Provider) DialMemContext(ctx context.Context, network string, laddr, raddr *Addr) (*Conn, error)
- func (p *Provider) Listen(network, address string) (net.Listener, error)
- func (p *Provider) ListenMem(network string, laddr *Addr) (*Listener, error)
- func (p *Provider) MapNetwork(from, to string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dial ¶
Dial dials a named connection.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
When the provided network is unknown the operation defers to net.Dial.
func DialContext ¶
DialContext dials a named connection using a Go context to provide timeout behavior.
Please see Dial for more information.
func Listen ¶
Listen begins listening at address for the specified network.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
When the specified address is already in use on the specified network an error is returned.
When the provided network is unknown the operation defers to net.Dial.
func MapNetwork ¶
func MapNetwork(from, to string)
MapNetwork enables mapping the network value provided to this Provider's Dial and Listen functions from the specified "from" value to the specified "to" value.
For example, calling MapNetwork("tcp", "memu") means a subsequent Dial("tcp", "address") gets translated to Dial("memu", "address").
Calling MapNetwork("tcp", "") removes any previous translation for the "tcp" network.
Types ¶
type Addr ¶
type Addr struct { // Name is the name of the endpoint. Name string // contains filtered or unexported fields }
Addr represents the address of an in-memory endpoint.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is an in-memory implementation of Golang's "net.Conn" interface.
func DialMem ¶
DialMem dials a named connection.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
If laddr is nil then a new address is generated using time.Now().UnixNano(). Please note that client addresses are not required to be unique.
If raddr is nil then the "localhost" endpoint is used on the specified network.
func DialMemContext ¶
DialMemContext dials a named connection using a Go context to provide timeout behavior.
Please see DialMem for more information.
func (*Conn) BufferSize ¶
BufferSize gets the number of bytes allowed to be queued for asynchrnous Write operations.
Please note that this function will always return zero for unbuffered connections.
Please see the function SetBufferSize for more information.
func (*Conn) CloseTimeout ¶
CloseTimeout gets the time.Duration value used when closing buffered connections.
Please note that this function will always return zero for unbuffered connections.
Please see the function SetCloseTimeout for more information.
func (*Conn) CopyOnWrite ¶
CopyOnWrite gets a flag indicating whether or not copy-on-write is enabled for this connection.
Please note that this function will always return false for unbuffered connections.
Please see the function SetCopyOnWrite for more information.
func (*Conn) Errs ¶
Errs returns a channel that receives errors that may occur as the result of buffered write operations.
This function will always return nil for unbuffered connections.
Please note that the channel returned by this function is not closed when the connection is closed. This is because errors may continue to be sent over this channel as the result of asynchronous writes occurring after the connection is closed. Therefore this channel should not be used to determine when the connection is closed.
func (*Conn) LocalBuffered ¶
LocalBuffered returns a flag indicating whether or not the local side of the connection is buffered.
func (*Conn) RemoteAddr ¶
RemoteAddr implements the net.Conn RemoteAddr method.
func (*Conn) RemoteBuffered ¶
RemoteBuffered returns a flag indicating whether or not the remote side of the connection is buffered.
func (*Conn) SetBufferSize ¶
SetBufferSize sets the number of bytes allowed to be queued for asynchronous Write operations. Once the amount of data pending a Write operation exceeds the specified size, subsequent Writes will block until the queued data no longer exceeds the allowed ceiling.
A value of zero means no maximum is defined.
If a Write operation's payload length exceeds the buffer size (except for zero) then the Write operation is handled synchronously.
Please note that setting the buffer size has no effect on unbuffered connections.
func (*Conn) SetCloseTimeout ¶
SetCloseTimeout sets a time.Duration value used by the Close function to determine the amount of time to wait for pending, buffered Writes to complete before closing the connection.
The default timeout value is 10 seconds. A zero value does not mean there is no timeout, rather it means the timeout is immediate.
Please note that setting this value has no effect on unbuffered connections.
func (*Conn) SetCopyOnWrite ¶
SetCopyOnWrite sets a flag indicating whether or not copy-on-write is enabled for this connection.
When a connection is buffered, data submitted to a Write operation is processed in a goroutine and the function returns control to the caller immediately. Because of this, it's possible to modify the data provided to the Write function before or during the actual Write operation. Enabling copy-on-write causes the payload to be copied to a new buffer before control is returned to the caller.
Please note that enabling copy-on-write will double the amount of memory required for all Write operations.
Please note that enabling copy-on-write has no effect on unbuffered connections.
func (*Conn) SetDeadline ¶
func (*Conn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn SetReadDeadline method.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn SetWriteDeadline method.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener implements the net.Listener interface.
func ListenMem ¶
ListenMem begins listening at laddr.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
If laddr is nil then ListenMem listens on "localhost" on the specified network.
func (*Listener) AcceptMemConn ¶
AcceptMemConn implements the net.Listener Accept method logic and returns a *memconn.Conn object.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is used to track named MemConn objects.
func (*Provider) Dial ¶
Dial dials a named connection.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
When the provided network is unknown the operation defers to net.Dial.
func (*Provider) DialContext ¶
DialContext dials a named connection using a Go context to provide timeout behavior.
Please see Dial for more information.
func (*Provider) DialMem ¶
DialMem dials a named connection.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
If laddr is nil then a new address is generated using time.Now().UnixNano(). Please note that client addresses are not required to be unique.
If raddr is nil then the "localhost" endpoint is used on the specified network.
func (*Provider) DialMemContext ¶
func (p *Provider) DialMemContext( ctx context.Context, network string, laddr, raddr *Addr) (*Conn, error)
DialMemContext dials a named connection using a Go context to provide timeout behavior.
Please see DialMem for more information.
func (*Provider) Listen ¶
Listen begins listening at address for the specified network.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
When the specified address is already in use on the specified network an error is returned.
When the provided network is unknown the operation defers to net.Dial.
func (*Provider) ListenMem ¶
ListenMem begins listening at laddr.
Known networks are "memb" (memconn buffered) and "memu" (memconn unbuffered).
If laddr is nil then ListenMem listens on "localhost" on the specified network.
func (*Provider) MapNetwork ¶
MapNetwork enables mapping the network value provided to this Provider's Dial and Listen functions from the specified "from" value to the specified "to" value.
For example, calling MapNetwork("tcp", "memu") means a subsequent Dial("tcp", "address") gets translated to Dial("memu", "address").
Calling MapNetwork("tcp", "") removes any previous translation for the "tcp" network.