Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure the goroutine wont shut down in a docker #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 55 additions & 50 deletions cmd/exchange/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"bufio"
"flag"
"fmt"
"github.com/robaho/go-trader/pkg/protocol"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
"os"
Expand All @@ -15,15 +12,18 @@ import (
"sync"
"time"

"github.com/robaho/go-trader/pkg/protocol"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/quickfixgo/quickfix"
"github.com/robaho/go-trader/internal/exchange"
"github.com/robaho/go-trader/pkg/common"
)

import _ "net/http/pprof"
_ "net/http/pprof"
)

func main() {

fix := flag.String("fix", "configs/qf_got_settings", "set the fix session file")
props := flag.String("props", "configs/got_settings", "set the exchange properties file")
instruments := flag.String("instruments", "configs/instruments.txt", "the instrument file")
Expand Down Expand Up @@ -106,54 +106,59 @@ func main() {

scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
s := scanner.Text()
parts := strings.Fields(s)
if len(parts) == 0 {
goto again
}
if "help" == parts[0] {
fmt.Println("The available commands are: quit, sessions, book SYMBOL, watch SYMBOL, unwatch SYMBOL, list")
} else if "quit" == parts[0] {
break
} else if "sessions" == parts[0] {
fmt.Println("Active sessions: ", ex.ListSessions())
} else if "book" == parts[0] {
book := exchange.GetBook(parts[1])
if book != nil {
fmt.Println(book)
go func() {
for scanner.Scan() {
s := scanner.Text()
parts := strings.Fields(s)
if len(parts) == 0 {
goto again
}
} else if "watch" == parts[0] && len(parts) == 2 {
fmt.Println("You are now watching ", parts[1], ", use 'unwatch ", parts[1], "' to stop.")
watching.Store(parts[1], "watching")
go func(symbol string) {
var lastBook *common.Book = nil
for {
if _, ok := watching.Load(symbol); !ok {
break
}
book := exchange.GetBook(symbol)
if book != nil {
if lastBook != book {
fmt.Println(book)
lastBook = book
if "help" == parts[0] {
fmt.Println("The available commands are: quit, sessions, book SYMBOL, watch SYMBOL, unwatch SYMBOL, list")
} else if "quit" == parts[0] {
break
} else if "sessions" == parts[0] {
fmt.Println("Active sessions: ", ex.ListSessions())
} else if "book" == parts[0] {
book := exchange.GetBook(parts[1])
if book != nil {
fmt.Println(book)
}
} else if "watch" == parts[0] && len(parts) == 2 {
fmt.Println("You are now watching ", parts[1], ", use 'unwatch ", parts[1], "' to stop.")
watching.Store(parts[1], "watching")
go func(symbol string) {
var lastBook *common.Book = nil
for {
if _, ok := watching.Load(symbol); !ok {
break
}
book := exchange.GetBook(symbol)
if book != nil {
if lastBook != book {
fmt.Println(book)
lastBook = book
}
}
time.Sleep(1 * time.Second)
}
time.Sleep(1 * time.Second)
}(parts[1])
} else if "unwatch" == parts[0] && len(parts) == 2 {
watching.Delete(parts[1])
fmt.Println("You are no longer watching ", parts[1])
} else if "list" == parts[0] {
for _, symbol := range common.IMap.AllSymbols() {
instrument := common.IMap.GetBySymbol(symbol)
fmt.Println(instrument)
}
}(parts[1])
} else if "unwatch" == parts[0] && len(parts) == 2 {
watching.Delete(parts[1])
fmt.Println("You are no longer watching ", parts[1])
} else if "list" == parts[0] {
for _, symbol := range common.IMap.AllSymbols() {
instrument := common.IMap.GetBySymbol(symbol)
fmt.Println(instrument)
} else {
fmt.Println("Unknown command, '", s, "' use 'help'")
}
} else {
fmt.Println("Unknown command, '", s, "' use 'help'")
again:
fmt.Print("Command?")
}
again:
fmt.Print("Command?")
}
}()

// Keep the main goroutine running
select {}
}