websocket

package
v0.0.0-...-6a8eb6d Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error)

Upgrade transform a http connection to a websocket one

Types

type Client

type Client struct {
	Identifier int             // Unique Identifier, used for debug
	Conn       *websocket.Conn // Websocket connection
	IsSlave    bool

	CurrentSearchRequest *SearchRequest // If it is a slave, the current search request that he is working on
	Server               *Server
	// contains filtered or unexported fields
}

func (*Client) Read

func (client *Client) Read()

Read gets all the messages that the client send

func (*Client) Write

func (client *Client) Write(message string)

Write sends a message through the websocket connection

type SearchRequest

type SearchRequest struct {
	Hash        string           // Hash that is being searched
	Slaves      map[*Client]bool // Slaves that are working one resolving the hash
	Client      *Client          // Client that made the request
	Result      string           // Decoded hash
	RequestedAt time.Time        // Time when the request has been received
	StartedAt   time.Time        // Time when the slaves started to work on this request
	EndedAt     time.Time        // Time when the result has been found
}

func NewSearchRequest

func NewSearchRequest(hash string, client *Client) *SearchRequest

NewSearchRequest initialize a SearchRequest object with default values and given parameters

func (*SearchRequest) FormatResponse

func (searchRequest *SearchRequest) FormatResponse() string

FormatResponse format the object in order to be returned in a response to a Client

type Server

type Server struct {
	Slaves              map[*Client]bool        // List of Clients that are slaves
	AvailableSlaves     map[*Client]bool        // List of slaves that are available for work
	Clients             map[*Client]bool        // List of connected Clients
	Queue               *list.List              // Queue containing the SearchRequest that are waiting to be resolved
	Searching           map[*SearchRequest]bool // List of SearchRequest that are being searched
	MaxSearch           string                  // The limit at which the message would be, can only be 9 from 2 to 8 times
	MaxSlavesPerRequest int                     // Maximum of slaves that will be dedicated to a SearchRequest
	SlavesCount         int                     // Number of slaves that we want (not equals to len(Slaves) if we are currently scaling)
	AutoScale           bool                    // Tells whether the app should scale the slave automatically depending on the queue
	// contains filtered or unexported fields
}

func NewServer

func NewServer(client *redis.Client) *Server

NewServer initialized a Server object with default values and parameters

func (*Server) AddClient

func (server *Server) AddClient(client *Client)

AddClient adds a client to the Clients list

func (*Server) AddSlave

func (server *Server) AddSlave(slave *Client)

AddSlave adds a slave to the Slaves list and the AvailableSlaves list, and calls BroadcastSlaveStatus

func (*Server) AddToQueue

func (server *Server) AddToQueue(request *SearchRequest)

AddToQueue adds a search request to the queue and calls BroadcastQueueStatus

func (*Server) BroadcastConfiguration

func (server *Server) BroadcastConfiguration()

BroadcastConfiguration sends to all Clients the current configuration in the format: max-search MaxSearch slaves SlavesCount numberOfSlavesNotWorking numberOfSlavesWorking max-slaves-per-request MaxSlavesPerRequest auto-scale true|false

func (*Server) BroadcastQueueStatus

func (server *Server) BroadcastQueueStatus()

BroadcastQueueStatus sends to all Clients the current status of the queue in the format: queue numberOfRequestInQueue numberOfRequestBeingHandled

func (*Server) BroadcastSlaveStatus

func (server *Server) BroadcastSlaveStatus()

BroadcastSlaveStatus sends to all Clients the current status of slaves in the format: slaves SlavesCount numberOfSlavesNotWorking numberOfSlavesWorking

func (*Server) Found

func (server *Server) Found(request *SearchRequest, result string)

Found called when a slave has found the result of a SearchRequest, it then sends the result to the client and tells the other Slaves that were working on the SearchRequest to stop

func (*Server) PrintConfiguration

func (server *Server) PrintConfiguration(client *Client)

PrintConfiguration sends the current configuration to a client in the format max-search MaxSearch slaves SlavesCount numberOfSlavesNotWorking numberOfSlavesWorking max-slaves-per-request MaxSlavesPerRequest auto-scale true|false

func (*Server) RemoveClient

func (server *Server) RemoveClient(client *Client)

RemoveClient removes a client from the Clients list

func (*Server) RemoveSlave

func (server *Server) RemoveSlave(slave *Client)

RemoveSlave removes a slave from the Slaves and the AvailableSlaves list, and calls BroadcastSlaveStatus

func (*Server) Scale

func (server *Server) Scale(number int) error

Scale choose the number of Slaves that we want in our application, must be between 0 and 16 for performance reason

func (*Server) SetAutoScale

func (server *Server) SetAutoScale(isAutoScale bool)

SetAutoScale set if the server should scale automatically the slaves or not

func (*Server) SetMaxSearch

func (server *Server) SetMaxSearch(maxSearch string)

SetMaxSearch set the MaxSearch parameter, it defines to what limit the slaves must search the word, it can only be 9 from 2 to 8 times

func (*Server) SetMaxSlavesPerRequest

func (server *Server) SetMaxSlavesPerRequest(maxSlavesPerRequest int)

SetMaxSlavesPerRequest set the maximum number of slaves that we assign to a SearchRequest

func (*Server) Start

func (server *Server) Start()

Start Loop that will handle autoscaling, soft downscale, and queue handling

func (*Server) StopAll

func (server *Server) StopAll()

StopAll stop all the SearchRequest that are running, clear the queue, and tells the Slaves to stop

Jump to

Keyboard shortcuts

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