-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
246 lines (221 loc) · 6.42 KB
/
main.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
type Event struct {
Key string
Data string
IsAsync bool
}
type EventLoop struct {
Events []Event
Handlers map[string]func(data string) string
ProcessedEvents []EventResult
}
type EventResult struct {
Key string
Result string
}
func NewEventLoop() *EventLoop {
return &EventLoop{
Handlers: make(map[string]func(string) string),
ProcessedEvents: []EventResult{},
Events: []Event{},
}
}
// The on() method populates the handlers fields with an identifier
// for a given event and the code that should be executed in response to that event
func (e *EventLoop) on(key string, fn func(data string) string) *EventLoop {
e.Handlers[key] = fn
return e
}
// The dispatch() method is used scheduling/submitting the event for execution
func (e *EventLoop) dispatch(event Event) {
e.Events = append(e.Events, event)
}
func (e *EventLoop) run() {
if len(e.Events) != 0 {
// Poll function implementation - Removing the the first elemt & returning it
event := e.Events[0]
e.Events = e.Events[1:]
fmt.Printf("\nReceived Event: %s\n\n", event.Key)
_, exists := e.Handlers[event.Key]
if exists {
startTime := time.Now()
if event.IsAsync {
e.processAsynchronously(event)
} else {
e.processSynchronously(event)
}
endTime := time.Since(startTime)
fmt.Printf("Event loop was blocked for %v ms due to this operation\n\n", endTime.Milliseconds())
} else {
fmt.Printf("No handler found for %s\n\n", event.Key)
}
}
if len(e.ProcessedEvents) != 0 {
processedEvent := e.ProcessedEvents[0]
e.ProcessedEvents = e.ProcessedEvents[1:]
e.produceOutputFor(processedEvent)
}
}
func (e *EventLoop) produceOutputFor(processedEvent EventResult) {
fmt.Printf("Output for Event %q: %v\n\n", processedEvent.Key, processedEvent.Result)
}
func (e *EventLoop) processSynchronously(event Event) {
handler := e.Handlers[event.Key]
if handler != nil {
result := handler(event.Data)
e.produceOutputFor(EventResult{
Key: event.Key,
Result: result,
})
}
}
func (e *EventLoop) processAsynchronously(event Event) {
go func() {
handler := e.Handlers[event.Key]
if handler != nil {
result := handler(event.Data)
e.ProcessedEvents = append(e.ProcessedEvents, EventResult{
Key: event.Key,
Result: result,
})
}
}()
}
func readFile(filename string) string {
data, err := os.ReadFile(filename)
if err != nil {
exists := os.IsExist(err)
fmt.Printf("Exists: %v\n", exists)
if !exists {
file, err := os.Create(filename)
file.WriteString("New file created")
if err != nil {
return fmt.Sprintf("Error creating file: %s", err)
}
file.Seek(0, 0)
data, err := io.ReadAll(file)
if err != nil {
return fmt.Sprintf("Error reading file: %s", err)
}
return string(data)
}
return fmt.Sprintf("Error reading file: %s", err)
}
return string(data)
}
func fetchDataFromAPI(id string) string {
resp, err := http.Get("https://jsonplaceholder.typicode.com/posts/" + id)
if err != nil {
return "Error fetching data from API"
}
var data struct {
ID int `json:"id"`
UserID int `json:"userId"`
Title string `json:"title"`
Body string `json:"body"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "Error decoding data from API"
}
return fmt.Sprintf("Fetched post from API: %v", data)
}
func isAsync(choice string) bool {
return choice == "2"
}
func generateUniqueEventKey(base string, id int) string {
return fmt.Sprintf("%s-%d", base, id)
}
func main() {
eventLoop := NewEventLoop()
reader := bufio.NewReader(os.Stdin)
utils := struct {
generateUniqueEventKey func(string, int) string
readFile func(string) string
fetchDataFromAPI func(string) string
isAsync func(string) bool
}{
generateUniqueEventKey: generateUniqueEventKey,
readFile: readFile,
fetchDataFromAPI: fetchDataFromAPI,
isAsync: isAsync,
}
eventID := 0
for {
var usersChoice string
for {
fmt.Println("What kind of task would you like to submit to the Event Loop?")
fmt.Println(" 1. Wish me Hello")
fmt.Println(" 2. Print the contents of a file named hello.txt")
fmt.Println(" 3. Retrieve data from API & print it")
fmt.Println(" 4. Print output of previously submitted Async task")
fmt.Println(" 5. Exit!")
fmt.Print(" > ")
usersChoice, _ = reader.ReadString('\n')
usersChoice = strings.TrimSpace(usersChoice)
if usersChoice != "" && (usersChoice >= "1" && usersChoice <= "5") {
break
}
fmt.Println("Invalid input. Please select a valid option (1-5).")
}
if usersChoice == "5" {
break
}
var operationType string
if usersChoice != "4" {
for {
fmt.Println("How would you like to execute this operation?")
fmt.Println(" 1. Synchronously (this would block the Event Loop until the operation completes)")
fmt.Println(" 2. Asynchronously (this won't block Event Loop in any way)")
fmt.Print(" > ")
operationType, _ = reader.ReadString('\n')
operationType = strings.TrimSpace(operationType)
if operationType != "" && (operationType == "1" || operationType == "2") {
break
}
fmt.Println("Invalid input. Please select a valid option (1 or 2).")
}
}
isAsync := utils.isAsync(operationType)
switch usersChoice {
case "1":
uniqueEventKey := utils.generateUniqueEventKey("hello", eventID)
eventID++
eventLoop.on(uniqueEventKey, func(data string) string {
return fmt.Sprintf("Hello! %s", data)
}).dispatch(Event{
Key: uniqueEventKey,
Data: "How are you doing today?",
IsAsync: isAsync,
})
case "2":
uniqueEventKey := utils.generateUniqueEventKey("read-file", eventID)
eventID++
eventLoop.on(uniqueEventKey, utils.readFile).dispatch(Event{
Key: uniqueEventKey,
Data: "hello.txt",
IsAsync: isAsync,
})
case "3":
uniqueEventKey := utils.generateUniqueEventKey("fetch-from-api", eventID)
eventID++
eventLoop.on(uniqueEventKey, utils.fetchDataFromAPI).dispatch(Event{
Key: uniqueEventKey,
Data: "2",
IsAsync: isAsync,
})
case "4":
}
eventLoop.run()
}
}