This Go program simulates a gossip algorithm for communication between nodes in a distributed system. The implementation, inspired by system design principles book
, introduces features such as dynamic gossip intervals, simulated network delays, and handling unresponsive nodes.
The Node
structure encapsulates essential properties of a gossip node:
type Node struct {
ID int
Data string
Peers map[int]*Node
mutex sync.Mutex
gossipLog []string
}
The Gossip
method initiates the gossip process, asynchronously sending data to all peers, simulating network delays,
and handling unresponsive peers.
func (n *Node) Gossip() {
// ... (see code)
}
Methods like simulateNetworkDelay and simulateProcessingDelay introduce random delays to simulate network and processing delays.
func (n *Node) simulateNetworkDelay() {
// ... (see code)
}
func (n *Node) simulateProcessingDelay() {
// ... (see code)
}
The system can handle unresponsive nodes by notifying other peers and implementing a method to handle notifications.
func (n *Node) notifyPeersAboutUnresponsiveNode(unresponsiveNodeID int) {
// ... (see code)
}
func (n *Node) HandleUnresponsiveNode(unresponsiveNodeID int) {
// ... (see code)
}
The main function orchestrates the simulation, loading environment variables, creating nodes, establishing peer connections, and simulating gossiping over a specified duration.
func main() {
// ... (see code)
} ```
## Logging
The writeLogToFile method writes the gossip log for each node to an external file.
```go
func (n *Node) writeLogToFile() {
// ... (see code)
}
This implementation is part of a learning project aimed at applying concepts learned from the system design book. The code is specifically written in Go to gain a deeper familiarity with the language and to practically implement various design principles discussed in the system design book.