Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shuddha2021/distributed-system-simulator-in-golang
A lightweight distributed system simulator in Go, featuring concurrency, HTTP endpoints, and unit tests.
https://github.com/shuddha2021/distributed-system-simulator-in-golang
concurrency data-structures distributed-systems error-handling go goroutines http-server restful-api synchronization thread-safety unit-testing
Last synced: 6 days ago
JSON representation
A lightweight distributed system simulator in Go, featuring concurrency, HTTP endpoints, and unit tests.
- Host: GitHub
- URL: https://github.com/shuddha2021/distributed-system-simulator-in-golang
- Owner: shuddha2021
- Created: 2024-05-01T18:19:06.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-01T20:03:54.000Z (8 months ago)
- Last Synced: 2024-11-06T19:50:20.952Z (about 2 months ago)
- Topics: concurrency, data-structures, distributed-systems, error-handling, go, goroutines, http-server, restful-api, synchronization, thread-safety, unit-testing
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Distributed System Simulator
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.
## Key Features
- **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`, and `UpdateNode`.## Core Logic
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 for `ID`, `Name`, `Value`, and `Time`.
- **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, and `sync.WaitGroup` to manage goroutine synchronization.## Technologies Used
- **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` and `WaitGroup`.
- **Encoding/JSON**: For converting data to and from JSON format.
- **Testing Package**: Used to implement unit tests for critical functions.## Getting Started
To run the Distributed System Simulator locally, follow these steps:
1. Clone the repository: `git clone `
2. Navigate to the project directory: `cd `
3. Start the application: `go run .go`
4. Open your browser and visit `http://localhost:8080/` for the welcome message and `http://localhost:8080/nodes` to get the node data.## Contributing
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.
## License
This project is licensed under the MIT License. For more information, see the LICENSE file.
## Acknowledgments
- **Go Documentation**: A comprehensive resource for learning Go and its standard library.
- **Open-Source Community**: For providing resources and platforms that enable collaborative projects.