cmd/chihaya: clean up
This commit is contained in:
parent
0e125b8a71
commit
0e0f8e7ad1
1 changed files with 34 additions and 25 deletions
|
@ -102,71 +102,80 @@ func main() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO create PeerStore
|
|
||||||
// TODO create Hooks
|
// TODO create Hooks
|
||||||
logic := middleware.NewLogic(cfg.Config, peerStore, nil, nil, nil, nil)
|
logic := middleware.NewLogic(cfg.Config, peerStore, nil, nil, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shutdown := make(chan struct{})
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
closedChan := make(chan struct{})
|
|
||||||
|
|
||||||
var hFrontend *httpfrontend.Frontend
|
var httpFrontend *httpfrontend.Frontend
|
||||||
var uFrontend *udpfrontend.Frontend
|
var udpFrontend *udpfrontend.Frontend
|
||||||
|
|
||||||
if cfg.HTTPConfig.Addr != "" {
|
if cfg.HTTPConfig.Addr != "" {
|
||||||
// TODO get the real TrackerLogic
|
httpFrontend = httpfrontend.NewFrontend(logic, cfg.HTTPConfig)
|
||||||
hFrontend = httpfrontend.NewFrontend(logic, cfg.HTTPConfig)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("started serving HTTP on", cfg.HTTPConfig.Addr)
|
log.Println("started serving HTTP on", cfg.HTTPConfig.Addr)
|
||||||
if err := hFrontend.ListenAndServe(); err != nil {
|
if err := httpFrontend.ListenAndServe(); err != nil {
|
||||||
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error())
|
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.UDPConfig.Addr != "" {
|
if cfg.UDPConfig.Addr != "" {
|
||||||
// TODO get the real TrackerLogic
|
udpFrontend = udpfrontend.NewFrontend(logic, cfg.UDPConfig)
|
||||||
uFrontend = udpfrontend.NewFrontend(logic, cfg.UDPConfig)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("started serving UDP on", cfg.UDPConfig.Addr)
|
log.Println("started serving UDP on", cfg.UDPConfig.Addr)
|
||||||
if err := uFrontend.ListenAndServe(); err != nil {
|
if err := udpFrontend.ListenAndServe(); err != nil {
|
||||||
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error())
|
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
shutdown := make(chan os.Signal)
|
sigChan := make(chan os.Signal)
|
||||||
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
<-shutdown
|
select {
|
||||||
|
case <-sigChan:
|
||||||
if uFrontend != nil {
|
case <-shutdown:
|
||||||
uFrontend.Stop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if hFrontend != nil {
|
if udpFrontend != nil {
|
||||||
hFrontend.Stop()
|
udpFrontend.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: stop PeerStore
|
if httpFrontend != nil {
|
||||||
|
httpFrontend.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
for err := range peerStore.Stop() {
|
||||||
|
if err != nil {
|
||||||
|
errChan <- err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
close(errChan)
|
close(errChan)
|
||||||
close(closedChan)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for err := range errChan {
|
closed := false
|
||||||
|
var bufErr error
|
||||||
|
for err = range errChan {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
close(shutdown)
|
if !closed {
|
||||||
<-closedChan
|
close(shutdown)
|
||||||
return err
|
closed = true
|
||||||
|
} else {
|
||||||
|
log.Println(bufErr)
|
||||||
|
}
|
||||||
|
bufErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return bufErr
|
||||||
}(); err != nil {
|
}(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue