/
main.go
264 lines (231 loc) · 5.85 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bufio"
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
"regexp"
"sync"
"sync/atomic"
"syscall"
"time"
)
const (
connType = "tcp"
port = "4000"
maxConnections = 5
logFile = "numbers.log"
InputLength = 9
DefaultReportDuration = 10
DefaultFlushDuration = 30
)
var (
totalUnique, duplicates, unique int32
invalidNumberRegex = regexp.MustCompile(`[^0-9]`)
)
type (
rwCache struct {
sync.RWMutex
m map[string]string
}
server struct {
addr string
server net.Listener
maxConnections int32
connectionsCount int32
isTerminated bool
}
)
func main() {
file, err := initializeFile(logFile)
if err != nil {
log.Fatal(err)
}
defer func() {
err = file.Close()
if err != nil {
log.Fatal(err)
}
}()
// TODO:
// - make bufio.Writer object is not thread safe
// - fix issue where the size of the buffered data is bigger than the buffer used to write to the file;
// either check before writing if there is space available or check for the error and split the data into multiple pieces that fit.
// - clean up the goroutines of the open connections during graceful termination
writer := bufio.NewWriter(file)
cache := &rwCache{m: make(map[string]string)}
server := newServer(":"+port, maxConnections, 0)
// log report of unique and duplicates every 10 seconds
go func(n time.Duration) {
for range time.Tick(n) {
atomic.AddInt32(&totalUnique, unique)
logReport(unique, totalUnique, duplicates)
atomic.StoreInt32(&unique, 0)
atomic.StoreInt32(&duplicates, 0)
}
}(DefaultReportDuration * time.Second)
// flush buffered data to log file every 30 seconds to improve performance. Flushin data more frequently reduces speed.
go func(n time.Duration) {
for range time.Tick(n) {
if err := flushWriteBuffer(writer); err != nil {
fmt.Printf("got error while flushing buffer: %v", err)
}
}
}(DefaultFlushDuration * time.Second)
server.run(writer, cache)
log.Fatal(err)
}
func initializeFile(filename string) (*os.File, error) {
file, err := os.Create(filename)
if err != nil {
return nil, err
}
return file, nil
}
func newServer(addr string, maxConnections int32, connectionsCount int32) *server {
return &server{
addr: addr,
maxConnections: maxConnections,
connectionsCount: connectionsCount,
}
}
func logReport(unique, totalUnique, duplicates int32) {
fmt.Printf("Received %d unique numbers, %d duplicates. Unique total: %d \n",
unique, duplicates, totalUnique)
}
func isInvalidInput(input string) bool {
return len(input) != InputLength || invalidNumberRegex.MatchString(input)
}
func isDuplicate(cache *rwCache, value string) bool {
if cache.get(value) != "" {
return true
}
return false
}
func (s *server) run(writer *bufio.Writer, cache *rwCache) error {
var err error
s.server, err = net.Listen(connType, s.addr)
if err != nil {
return err
}
fmt.Println("started TCP server")
defer func() {
if err = s.close(writer); err != nil {
fmt.Printf("got error while closing server connection: %v", err)
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigChan
if err = s.close(writer); err != nil {
fmt.Printf("got error while closing server connection: %v", err)
}
}()
for {
conn, err := s.server.Accept()
if err != nil {
return err
}
if s.connectionsCount >= s.maxConnections {
fmt.Println("Max concurrent connections reached")
if err = conn.Close(); err != nil {
fmt.Printf("got error `%v` while closing connection for client: %s", err, conn.RemoteAddr().String())
}
continue
}
fmt.Println("successfully connected")
// handle connections
go func() {
err = s.handleConn(conn, writer, cache)
if err != nil {
fmt.Println(err)
}
}()
}
}
func (s *server) close(writer *bufio.Writer) error {
fmt.Println("Gracefully shutting down server...")
fmt.Println("Flushing data to write buffer...")
if !s.isTerminated {
s.isTerminated = true
if err := s.server.Close(); err != nil {
_ = writer.Flush()
return err
}
if err := writer.Flush(); err != nil {
return err
}
}
return nil
}
func (s *server) handleConn(conn net.Conn, writer *bufio.Writer, cache *rwCache) error {
atomic.AddInt32(&s.connectionsCount, 1)
fmt.Printf("Now listening: %s \n", conn.RemoteAddr().String())
err := s.handleContentFromConnection(conn, writer, cache)
if err != nil {
return errors.New(err.Error())
}
// close connection
defer func(conn net.Conn) {
atomic.AddInt32(&s.connectionsCount, -1)
fmt.Printf("Closing connection for : %s \n", conn.RemoteAddr().String())
if err := conn.Close(); err != nil {
fmt.Printf("got error `%v` while closing connection for client: %s", err, conn.RemoteAddr().String())
}
}(conn)
return nil
}
func (s *server) handleContentFromConnection(conn net.Conn, writer *bufio.Writer, cache *rwCache) error {
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
if line == "terminate" {
conn.Close()
err := s.close(writer)
if err != nil {
return err
}
}
if isInvalidInput(line) {
break
}
if !isDuplicate(cache, line) {
atomic.AddInt32(&unique, 1)
cache.set(line)
err := writeToFile(writer, line)
if err != nil {
return err
}
} else {
atomic.AddInt32(&duplicates, 1)
}
}
return nil
}
func writeToFile(writer *bufio.Writer, value string) error {
if _, err := writer.WriteString(value + "\n"); err != nil {
return err
}
return nil
}
func flushWriteBuffer(writer *bufio.Writer) error {
fmt.Println("Flushing buffer...")
if err := writer.Flush(); err != nil {
return err
}
return nil
}
func (r *rwCache) get(key string) string {
r.RLock()
defer r.RUnlock()
return r.m[key]
}
func (r *rwCache) set(key string) {
r.Lock()
defer r.Unlock()
r.m[key] = key
}