In this project, we’ll implement a message queue broker in Go.
The broker has the following features:
- Synchronous messaging
- Asynchronous messaging
- Overflow handling
- Two-way communication
- Concurrent connection
The project includes a server, client and a broker.
To run the broker use the following command:
go run connection.go -port <port_num>-maxl <max queue length> -wsize <number of workers>
And for running servers and clients use:
go run client.go -host localhost -port <port_num>
go run server.go -host localhost -port <port_num>
The project runs on a tcp server; the broker listens on a socket and the clients/servers connect to that socket and communicate with the broker.
The broker contains a queue of messages i.e. payloads. Each time the server requests a publishing,
the broker appends the message on the queue with the respectable topic.
This appending is a critical section, thus we use a mutex lock to preserve integrity.
Whenever possible, the messages are sent to the topic's subscribers using subscriber handler.
In the async method, the broker sends each message (event) to a channel which
will then be used by the workers (go routines) to be sent to the subscribers.
Note that the client is non-blocking and with each request, it doesn't expect a response.
This was made possible by using a go routine for the response handling.
If there is a new publish request, the broker checks if the queue of the request topic is full or not.
If the queue was full, it will return an OverflowError and cancel the publishing.
The client can both subscribe and publish to a topic using -subscribe and -publish commands.
Many servers and clients can connect to a broker, this is made possible using go routines.
Each time a new client is connected to the socket, a new go routine for the specific client is created
and the communication is resumed in that routine.