rpcclientpool

package
v0.0.0-...-63c3a42 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rpcclientpool wraps net/rpc.Client to manage shared resource pools.

Package rpcclientpool wraps net/rpc.Client from the standard library so that limited resources (file descriptors used for network connections) may be shared easily with other packages such as lib/connpool and lib/srpc. A typical programming pattern is:

cr0 := New(...)
cr1 := New(...)
go func() {
	for ... {
		c := cr0.Get(...)
		defer c.Put()
		if err { c.Close() }
	}
}()
go func() {
	for ... {
		c := cr1.Get(...)
		defer c.Put()
		if err { c.Close() }
	}
}()

This pattern ensures Get and Put are always matched, and if there is a communications error, Close shuts down the connection so that a subsequent Get creates a new underlying connection.

It is resonable to create one goroutine for each resource, since the Get methods will block, waiting for available resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a managed RPC client. It implements similar methods as rpc.Client from the standard library.

func (*Client) Call

func (client *Client) Call(serviceMethod string, args interface{},
	reply interface{}) error

func (*Client) Close

func (client *Client) Close() error

Close will close the client, immediately freeing the underlying resource. It may no longer be used for communication.

func (*Client) Go

func (client *Client) Go(serviceMethod string, args interface{},
	reply interface{}, done chan *rpc.Call) *rpc.Call

func (*Client) Put

func (client *Client) Put()

Put will release the client. It may no longer be used for communication. It may be internally closed later if required to free limited resources (such as file descriptors). If Put is called after Close, no action is taken (this is a safe operation and is commonly used in some programming patterns).

type ClientResource

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

func New

func New(network, address string, http bool, path string) *ClientResource

New returns a ClientResource for the specified network address. If a RPC over HTTP connection is required then http should be true and the HTTP path should be given by path. If path is empty then the default path (net/rpc.DefaultRPCPath) is used. In general, the default path should be used. The ClientResource may be used later to obtain a Client which is part of a managed pool of connection slots (to limit consumption of resources such as file descriptors). Clients can be released with the Put method but the underlying connection may be kept open for later re-use. The Client is placed on an internal list.

func NewWithDialer

func NewWithDialer(
	network,
	address string,
	http bool,
	path string,
	dialer net.Dialer) *ClientResource

NewWithDialer works just like New but allows this resource to use a custom dialer.

func (*ClientResource) Get

func (cr *ClientResource) Get(cancelChannel <-chan struct{}) (*Client, error)

Get will return a Client. Get will wait until a resource is available or a message is received on cancelChannel. If cancelChannel is nil then Get will wait indefinitely until a resource is available. If the wait is cancelled then Get will return ErrorResourceLimitExceeded. Get will panic if it is called again without an intervening Close or Put.

Jump to

Keyboard shortcuts

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