prc_client

package module
v0.0.0-...-760fe0a Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 9 Imported by: 0

README

Go client library for: Parallel Request Controller

This is a client library for the Go programming language in order to connect to the Parallel Request Controller Server.

Usage

In order to fetch the library, type:

go get github.com/AgustinSRG/parallel-request-controller/client

Example usage:

package main

import (
    prc_client "github.com/AgustinSRG/parallel-request-controller/client"
)

const MAX_PARALLEL_REQUESTS = 5;

func main() {
    // Create a client for the Parallel Request Controller server
    prcCli := prc_client.NewClient(&ClientConfig{
        Url: "ws://localhost:8080",
        AuthToken: "change_me",
    })

    // For this example, we use a mock server to illustrate the request handling
    server := createServerSomehow(func (req *Request) *Response {
        // We call StartRequest in order to ensure the request limit was not reached
        prcRef, limited, err := prc_client.StartRequest(request.req_type, MAX_PARALLEL_REQUESTS)

        if err != nil {
            // Handle error
            return &Response{
                Status: 500,
            }
        }

        if limited {
            // Request limit reached
            return &Response{
                Status: 429,
            }
        }

        defer prcRef.End() // When the request finished, we must call End()

        // Compute request...
        // ...
    })

    server.listen()
}

Documentation

Testing

In order to test the library, first, make sure to start a Parallel Request Controller Server. Also, set the following env variables:

Variable Description
SERVER_URL Server URL. Default: ws://localhost:8080
AUTH_TOKEN Authentication token

Then, run:

go test -v

Documentation

Index

Constants

View Source
const DEFAULT_RETRY_CONNECTION_DELAY = 5 * time.Second
View Source
const DEFAULT_TIMEOUT = 10 * time.Second
View Source
const HEARTBEAT_MSG_PERIOD_SECONDS = 30

Period to send HEARTBEAT messages to the client

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client for the parallel request controller

func NewClient

func NewClient(config *ClientConfig) *Client

Creates client

func (*Client) Close

func (cli *Client) Close()

Closes all the connections

func (*Client) Connect

func (cli *Client) Connect()

Connects the client

func (*Client) GetRequestCount

func (cli *Client) GetRequestCount(requestType string) (count uint32, err error)

Gets the current number of parallel requests of a type Parameters: - requestType - String to indicate the request type Returns: - count - Current number of parallel requests of the specified type - err - An error that prevented the request count from completing

func (*Client) StartRequest

func (cli *Client) StartRequest(requestType string, limit uint32) (req *StartedRequest, limited bool, err error)

Indicates the start of a request Parameters: - requestType - String to indicate the request type - limit - Máximum number of requests allowed to be run in parallel Returns: - req - Reference to the started request. Keep it to indicate the ending. May be nil in case of error or if the request type reached the limit - limited - True if the limit was reached, so the request should be rejected - err - An error that prevented the request start indication from completing

type ClientConfig

type ClientConfig struct {
	// Parallel request controller base URL. Example: ws://example.com:8080
	Url string

	// Number of connections. 1 by default.
	NumberOfConnections int

	// Authentication token
	AuthToken string

	// Delay retry the connection. 5 seconds by default
	RetryConnectionDelay time.Duration

	// Error handler
	ErrorHandler ErrorHandler

	// Timeout for receiving responses from the server. By default: 10 seconds
	Timeout time.Duration
}

Configuration of the PRC client

func (*ClientConfig) GetFullConnectionUrl

func (config *ClientConfig) GetFullConnectionUrl() (string, error)

Gets full connection URL (with authentication token)

type Connection

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

Connection to a PRC server

func NewConnection

func NewConnection(cli *Client, config *ClientConfig) *Connection

func (*Connection) Close

func (conn *Connection) Close()

Closes the connection

func (*Connection) Connect

func (conn *Connection) Connect()

Opens the connection

func (*Connection) EndRequest

func (conn *Connection) EndRequest(id uint64)

Ends a request, by sending the END-REQUEST message

func (*Connection) GetRequestCount

func (conn *Connection) GetRequestCount(rType string)

Sends GET-REQUEST-COUNT message to get the request count for a specific type

func (*Connection) IsClosed

func (conn *Connection) IsClosed() bool

Checks if the client is closed

func (*Connection) ReceiveRequestCount

func (conn *Connection) ReceiveRequestCount(msg *simple_rpc_message.RPCMessage)

Receives REQUEST-COUNT message

func (*Connection) ReceiveStartRequestAck

func (conn *Connection) ReceiveStartRequestAck(msg *simple_rpc_message.RPCMessage)

Receives message: START-REQUEST-ACK

func (*Connection) RequestCountDone

func (conn *Connection) RequestCountDone(rType string)

Call after a request count is done, either by receiving the response or due to timeout

func (*Connection) Send

func (conn *Connection) Send(msg *simple_rpc_message.RPCMessage)

Send a message to the module

func (*Connection) StartRequest

func (conn *Connection) StartRequest(id uint64, rType string, limit uint32)

Starts request, either by sending a START-REQUEST message or waiting for connection

type ErrorHandler

type ErrorHandler interface {
	// Called on connection error. The client will retry the connection if not manually closed
	OnConnectionError(err error)

	// Called when an ERROR message is received from the server
	OnServerError(code string, message string)
}

Error handler for the PRC client

type PendingRequest

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

Pending request

type RequestCountListener

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

Listener for request count

type RequestStartAckListener

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

Listener for request start ack

type StartedRequest

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

Started request. Keep it to indicate the ending.

func (*StartedRequest) End

func (request *StartedRequest) End()

Indicates the ending of the request

Jump to

Keyboard shortcuts

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