add daemon timeout option, stop ytsync gracefully during daemon startup

This commit is contained in:
Alex Grintsvayg 2018-03-08 19:27:54 -05:00
parent f34354059f
commit 1b6c47c841
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
3 changed files with 23 additions and 3 deletions

View file

@ -3,6 +3,7 @@ package jsonrpc
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"sort"
"strconv"
@ -124,6 +125,10 @@ func (d *Client) call(response interface{}, command string, params map[string]in
return decode(result, response)
}
func (d *Client) SetRPCTimeout(timeout time.Duration) {
d.conn.SetHTTPClient(&http.Client{Timeout: timeout})
}
func (d *Client) Commands() (*CommandsResponse, error) {
response := new(CommandsResponse)
return response, d.call(response, "commands", map[string]interface{}{})

View file

@ -13,11 +13,11 @@ func New() *Stopper {
return &s
}
func (s Stopper) Chan() <-chan struct{} {
func (s *Stopper) Chan() <-chan struct{} {
return s.ch
}
func (s Stopper) Stop() {
func (s *Stopper) Stop() {
s.once.Do(func() {
close(s.ch)
})

View file

@ -134,7 +134,22 @@ func (s *Sync) FullCycle() error {
}
log.Infoln("Waiting for daemon to finish starting...")
s.daemon = jsonrpc.NewClientAndWait("")
s.daemon = jsonrpc.NewClient("")
s.daemon.SetRPCTimeout(5 * time.Minute)
WaitForDaemonStart:
for {
select {
case <-s.stop.Chan():
return nil
default:
_, err := s.daemon.WalletBalance()
if err == nil {
break WaitForDaemonStart
}
time.Sleep(5 * time.Second)
}
}
err = s.doSync()
if err != nil {