redis_nats_proxy

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Redis-NATS-Proxy

Redis universal client proxy powered by NATS transport.

Description

This project is a Redis client proxy that uses NATS as a transport layer. It is written in Go and uses the go.mod for managing dependencies.

Getting Started

Dependencies
  • Go
  • NATS
  • Redis
Installing
  • Clone the repository
  • Run go mod download to download the necessary dependencies
Executing program
  • Look at the example folder

Help

Any advise for common problems or issues.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DebugLogNetConn

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

DebugLogNetConn is a type that wraps a net.Conn and logs debugging information for read, write, and close operations. Read reads data from the connection and logs the bytes read and the length of the buffer.

func NewDebugCustomLogNetConn

func NewDebugCustomLogNetConn(conn net.Conn, log DebugLogger) *DebugLogNetConn

NewDebugCustomLogNetConn returns a new DebugLogNetConn instance with the provided debug logger.

func NewDebugLogNetConn

func NewDebugLogNetConn(conn net.Conn) *DebugLogNetConn

NewDebugLogNetConn returns a new DebugLogNetConn instance. It wraps the provided net.Conn and adds debug logging to Read, Write, Close, LocalAddr, RemoteAddr, SetDeadline, SetReadDeadline, and SetWriteDeadline methods.

func (*DebugLogNetConn) Close

func (lc *DebugLogNetConn) Close() error

Close closes the underlying net.Conn and logs a debug message.

func (*DebugLogNetConn) LocalAddr

func (lc *DebugLogNetConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*DebugLogNetConn) Read

func (lc *DebugLogNetConn) Read(b []byte) (n int, err error)

Read reads data from the underlying net.Conn into the provided byte slice.

func (*DebugLogNetConn) RemoteAddr

func (lc *DebugLogNetConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address of the underlying net.Conn connection.

func (*DebugLogNetConn) SetDeadline

func (lc *DebugLogNetConn) SetDeadline(t time.Time) error

SetDeadline sets the deadline for all I/O operations on the underlying net.Conn. The deadline is an absolute time after which I/O operations will fail with a timeout error. If `t` is in the past, I/O operations will fail immediately with a timeout error.

The logs the deadline information including the deadline time and the time remaining until the deadline.

This method returns an error if the net.Conn implementation returns an error when setting the deadline.

func (*DebugLogNetConn) SetReadDeadline

func (lc *DebugLogNetConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline for the underlying net.Conn. After the specified time, if no data is read, the Read operation will return with an error. The time difference between the specified time and the current time is logged. It returns an error if there was an error while setting the read deadline.

func (*DebugLogNetConn) SetWriteDeadline

func (lc *DebugLogNetConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline for the current DebugLogNetConn. The provided time `t` specifies the deadline. After the deadline, any write operation will fail with a timeout error. The difference between the current time and the deadline time is logged using slog.Debug.

func (*DebugLogNetConn) Write

func (lc *DebugLogNetConn) Write(b []byte) (n int, err error)

Write writes the provided byte slice to the underlying net.Conn. Before writing, the byte slice is compacted and cloned. After writing, debug logging is performed, including the number of bytes written and the length of the original byte slice.

type DebugLogger

type DebugLogger interface {
	Debug(msg string, args ...any)
}

DebugLogger is an interface that defines a method for logging debug messages. The Debug method takes a message string and optional variadic arguments and logs the debug message. Example usage: debug := defaultDebugLogger debug.Debug("This is a debug message") debug.Debug("This is a debug message with arguments, "arg1", 42, "arg2", "foo")

type DebugLoggerFunc

type DebugLoggerFunc func(msg string, args ...any)

DebugLoggerFunc represents a function type that can be used as a debug logger. The DebugLoggerFunc type takes a message string and optional variadic arguments and logs the debug message.

func (DebugLoggerFunc) Debug

func (d DebugLoggerFunc) Debug(msg string, args ...any)

Debug prints the debug message using the specified DebugLoggerFunc. The message and any additional arguments are passed to the DebugLoggerFunc as arguments.

type DialFn

type DialFn func(network, addr string) (net.Conn, error)

DialFn represents a function that dials a network address.

var DefaultDial DialFn = net.Dial

DefaultDial is the default dial function that dials a network address using net.Dial.

type GetOption

type GetOption func(*getOptions)

GetOption represents a function type for setting Get function options.

func WithUUID

func WithUUID(uuid string) GetOption

WithUUID sets the UUID option for the Get function.

type NatsConnProxy

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

NatsConnProxy represents a proxy for NATS connections. It is responsible for handling read and write requests from NATS messages and forwarding them to the appropriate network connections. NatsConnProxy uses the NetConnManager interface to manage network connections from a pool or create new ones. The proxy starts handling requests by calling the Start method, which takes a context.Context as a parameter and returns an error if any occurs.

Example usage:

ncp := NewNatsConnProxy(nc, subject, connPool) err := ncp.Start(ctx)

func NewNatsConnProxy

func NewNatsConnProxy(nc *nats.Conn, subject string, connPool NetConnManager) *NatsConnProxy

NewNatsConnProxy creates a new NatsConnProxy with the provided NATS connection, subject, and connection pool.

The NatsConnProxy is responsible for handling read and write requests from NATS messages and forwarding them to the corresponding network connections.

Parameters: - nc: The NATS connection to use for communication. - subject: The subject to listen for NATS messages on. - connPool: The connection pool to use for network connections.

Returns: - *NatsConnProxy: The created NatsConnProxy instance.

func (NatsConnProxy) Start

func (ncp NatsConnProxy) Start(ctx context.Context) error

Start starts the NatsConnProxy instance by subscribing to NATS messages for read and write requests and handling them.

Parameters: - ctx: The context.Context used to control the execution of the method.

Returns: - error: An error if there was a problem subscribing to the NATS messages, otherwise nil.

type NatsNetConn

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

NatsNetConn is a type that wraps a nats.Conn and provides methods for reading, writing, closing, and managing deadlines on network connections.

func NewNatsNetConn

func NewNatsNetConn(nc *nats.Conn, subject string, addr *net.TCPAddr) (*NatsNetConn, error)

NewNatsNetConn returns a new NatsNetConn instance.

func (*NatsNetConn) Close

func (c *NatsNetConn) Close() error

func (*NatsNetConn) LocalAddr

func (c *NatsNetConn) LocalAddr() net.Addr

func (*NatsNetConn) Read

func (c *NatsNetConn) Read(b []byte) (n int, err error)

Read reads data from the underlying nats.Conn into the provided byte slice.

func (*NatsNetConn) RemoteAddr

func (c *NatsNetConn) RemoteAddr() net.Addr

func (*NatsNetConn) SetDeadline

func (c *NatsNetConn) SetDeadline(t time.Time) error

func (*NatsNetConn) SetReadDeadline

func (c *NatsNetConn) SetReadDeadline(t time.Time) error

func (*NatsNetConn) SetWriteDeadline

func (c *NatsNetConn) SetWriteDeadline(t time.Time) error

func (*NatsNetConn) Write

func (c *NatsNetConn) Write(b []byte) (n int, err error)

Write writes the provided byte slice to the underlying nats.Conn.

type NetConnManager

type NetConnManager interface {
	io.Closer
	// Get returns a connection from the pool or creates a new one.
	Get(addr *net.TCPAddr, options ...GetOption) (net.Conn, error)
}

NetConnManager represents an interface for managing network connections.

type NetConnPullManager

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

NetConnPullManager represents a pool manager for network connections.

func NewNetConnPullManager

func NewNetConnPullManager(fn DialFn) *NetConnPullManager

NewNetConnPullManager creates a new instance of NetConnPullManager. It takes a DialFn function as a parameter, which is a function type for dialing a network connection, and returns a pointer to a NetConnPullManager. If fn is nil, it uses the DefaultDial function.

func (*NetConnPullManager) Close

func (cp *NetConnPullManager) Close() error

Close closes all the connections in the NetConnPullManager pool. It iterates over each connection in the pool and calls the Close() method on them. If an error occurs while closing a connection, it returns an error with a formatted message. If all connections are successfully closed, it returns a nil error.

func (*NetConnPullManager) Get

func (cp *NetConnPullManager) Get(addr *net.TCPAddr, options ...GetOption) (net.Conn, error)

Get retrieves a network connection from the NetConnPullManager pool based on the given address. It takes a *net.TCPAddr as the address parameter and returns a net.Conn and an error. The function first attempts to find the connection in the pool using the generated key from the address. If the connection is found, it is returned along with a nil error. If the connection is not found, the function uses the cp.dial function to create a new connection. If an error occurs while dialing, an error is returned with a formatted message. Otherwise, the newly created connection is added to the pool using the generated key, and the connection along with a nil error is returned.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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