The Distributed System Simulator is a lightweight Go application designed to simulate a set of nodes in a distributed system. This project showcases key concepts in Go, such as concurrency with goroutines, thread-safe data access with mutexes, and HTTP server setup with basic RESTful endpoints.
- HTTP Server: The application creates an HTTP server with endpoints for retrieving node data and providing a welcome message.
- Concurrency with Goroutines: A goroutine periodically updates a random node to simulate a distributed system's behavior.
- Thread-Safe Data Access: The code uses
sync.RWMutex
to ensure thread-safe operations when accessing shared data. - Error Handling: Proper error handling with appropriate HTTP status codes and log messages.
- Unit Tests: Comprehensive unit tests to validate the behavior of key functions, including
GetNodeData
,RootHandler
, andUpdateNode
.



The core logic of the application revolves around simulating a set of nodes in a distributed system and providing HTTP endpoints to interact with them. Here's a brief overview of how it works:
- Node Data Structure: The
NodeData
struct represents a node with fields forID
,Name
,Value
, andTime
. - Initialization: The
InitNodes
function initializes a slice of nodes with random data. - HTTP Endpoints:
/nodes
: Returns the current state of all nodes in JSON format./
: Provides a welcome message with instructions for users.
- Concurrency: A goroutine periodically updates a random node's data every 5 seconds, demonstrating concurrency.
- Synchronization: The code uses
sync.RWMutex
to ensure thread-safe operations, andsync.WaitGroup
to manage goroutine synchronization.
- Go (Golang): The primary programming language used for building the application.
- Net/HTTP: Used to create the HTTP server and define HTTP handlers.
- Sync Package: Provides synchronization primitives such as
RWMutex
andWaitGroup
. - Encoding/JSON: For converting data to and from JSON format.
- Testing Package: Used to implement unit tests for critical functions.
To run the Distributed System Simulator locally, follow these steps:
- Clone the repository:
git clone <your-repo-url>
- Navigate to the project directory:
cd <your-project-directory>
- Start the application:
go run <your-go-file>.go
- Open your browser and visit
http://localhost:8080/
for the welcome message andhttp://localhost:8080/nodes
to get the node data.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request. Be sure to follow the existing code style and write appropriate tests for any new functionality.
This project is licensed under the MIT License. For more information, see the LICENSE file.
- Go Documentation: A comprehensive resource for learning Go and its standard library.
- Open-Source Community: For providing resources and platforms that enable collaborative projects.