-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (48 loc) · 1.41 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
package main
import (
"fmt"
"os"
"os/signal"
"time"
)
var LOGGER *Logger
func init() {
LOGGER = NewLogger(TRACE, "")
LOGGER.Trace.Println("Setting up data feeds")
}
func main() {
// Close channel will be used by exchanges and writers to shut down before ending the program
var closeChannel = make(chan bool)
// Channel will be used for transferring data between exchanges to writers
var broadcastChannel = make(chan []TradeRow)
// Handle interrupt. Let others know its time to close
c := make(chan os.Signal, 10)
signal.Notify(c, os.Interrupt)
go func() {
<-c // waits infinitely till interrupt signal is sent
fmt.Println("\nCleaning up. Sending close signal")
closeChannel <- true // Inform others to close now. Sleep few seconds before exiting
time.Sleep(5)
close(closeChannel)
close(broadcastChannel)
os.Exit(0)
}()
// Let the show begin.
// Writers and Exchanges are unaware of each other and only communicate via channel
WRITERS.Listen(closeChannel)
EXCHANGES.Listen(closeChannel, broadcastChannel)
fmt.Println("Working...")
// Loop infinitely till close channel is called
// Otherwise, listen for new trades and on its arrival have writers handle it in a separate routine
for {
select {
case <-closeChannel:
break
case tradeRows := <-broadcastChannel:
// Launch a new go routine for this, so its not blocking the write
go func() {
WRITERS.Write(tradeRows)
}()
}
}
}