-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
148 lines (116 loc) · 2.74 KB
/
main.go
File metadata and controls
148 lines (116 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/json"
"sync"
maelstrom "github.com/jepsen-io/maelstrom/demo/go"
)
func main() {
n := maelstrom.NewNode()
ns := newNodeServer(n)
n.Handle("broadcast", ns.handleBroadcast)
n.Handle("read", ns.handleRead)
n.Handle("topology", ns.handleTopology)
if err := n.Run(); err != nil {
panic(err)
}
}
type nodeServer struct {
node *maelstrom.Node
messagesMu sync.RWMutex
messages map[int]struct{}
neighborsMu sync.RWMutex
neighbors []string
}
func newNodeServer(n *maelstrom.Node) *nodeServer {
return &nodeServer{
node: n,
messages: make(map[int]struct{}, 0),
}
}
func (ns *nodeServer) handleRead(msg maelstrom.Message) error {
ns.messagesMu.RLock()
m := ns.messages
ns.messagesMu.RUnlock()
messages := make([]int, 0, len(m))
for k := range m {
messages = append(messages, k)
}
return ns.node.Reply(msg, map[string]any{
"type": "read_ok",
"messages": messages,
})
}
func (ns *nodeServer) handleBroadcast(msg maelstrom.Message) error {
body := struct {
Message int `json:"message"`
MsgID int `json:"msg_id"`
}{}
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
isDuplicateMsg := false
// Save the message to the node's state
ns.messagesMu.Lock()
_, isDuplicateMsg = ns.messages[body.Message]
if !isDuplicateMsg {
ns.messages[body.Message] = struct{}{}
}
ns.messagesMu.Unlock()
if isDuplicateMsg {
if body.MsgID == 0 {
// Do not reply to fire-and-forget messages
return nil
}
// Skip broadcasting the message if it has been seen before
return ns.node.Reply(msg, map[string]any{
"type": "broadcast_ok",
})
}
// Broadcast the message to all node's neighbors
ns.neighborsMu.RLock()
neighbors := ns.neighbors
ns.neighborsMu.RUnlock()
wg := sync.WaitGroup{}
wg.Add(len(neighbors))
for _, neighbor := range neighbors {
if msg.Src == neighbor {
wg.Done()
// Skip the neighbor that sent the message initially
continue
}
go func() {
defer wg.Done()
// Retry sending the message until it is successful
for {
if err := ns.node.Send(neighbor, map[string]any{
"type": "broadcast",
"message": body.Message,
}); err == nil {
break
}
}
}()
}
wg.Wait()
if body.MsgID == 0 {
// Do not reply to fire-and-forget messages
return nil
}
return ns.node.Reply(msg, map[string]any{
"type": "broadcast_ok",
})
}
func (ns *nodeServer) handleTopology(msg maelstrom.Message) error {
body := struct {
Topology map[string][]string `json:"topology"`
}{}
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
ns.neighborsMu.Lock()
ns.neighbors = body.Topology[ns.node.ID()]
ns.neighborsMu.Unlock()
return ns.node.Reply(msg, map[string]any{
"type": "topology_ok",
})
}