From 291a10526918e49c50e1b173bf4da7939bd62558 Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Wed, 16 Oct 2019 19:38:45 +0200 Subject: [PATCH] improve getDefaultAccount use default account on more calls improve API logging fix memory leak --- manager/setup.go | 36 +++++++++++++++++++----------------- manager/ytsync.go | 8 ++++++-- sdk/api.go | 31 ++++++++++++++++++------------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/manager/setup.go b/manager/setup.go index 3c0c4d2..2fbe721 100644 --- a/manager/setup.go +++ b/manager/setup.go @@ -141,26 +141,28 @@ func (s *Sync) walletSetup() error { return nil } + func (s *Sync) getDefaultAccount() (string, error) { - accounts, err := s.daemon.AccountList() - if err != nil { - return "", errors.Err(err) - } - accountsNet := (*accounts).LBCMainnet - if logUtils.IsRegTest() { - accountsNet = (*accounts).LBCRegtest - } - defaultAccount := "" - for _, account := range accountsNet { - if account.IsDefault { - defaultAccount = account.ID - break + if s.defaultAccountID == "" { + accounts, err := s.daemon.AccountList() + if err != nil { + return "", errors.Err(err) + } + accountsNet := (*accounts).LBCMainnet + if logUtils.IsRegTest() { + accountsNet = (*accounts).LBCRegtest + } + for _, account := range accountsNet { + if account.IsDefault { + s.defaultAccountID = account.ID + break + } + } + if s.defaultAccountID == "" { + return "", errors.Err("No default account found") } } - if defaultAccount == "" { - return "", errors.Err("No default account found") - } - return defaultAccount, nil + return s.defaultAccountID, nil } func (s *Sync) ensureEnoughUTXOs() error { diff --git a/manager/ytsync.go b/manager/ytsync.go index 1066719..314dec1 100644 --- a/manager/ytsync.go +++ b/manager/ytsync.go @@ -89,6 +89,7 @@ type Sync struct { transferState int clientPublishAddress string publicKey string + defaultAccountID string } func (s *Sync) AppendSyncedVideo(videoID string, published bool, failureReason string, claimName string, claimID string, metadataVersion int8, size int64) { @@ -342,7 +343,10 @@ func (s *Sync) processTransfers() (e error) { if err != nil { return err } - + defaultAccount, err := s.getDefaultAccount() + if err != nil { + return err + } reallocateSupports := supportAmount > 0.01 if reallocateSupports { err = waitConfirmations(s) @@ -350,7 +354,7 @@ func (s *Sync) processTransfers() (e error) { return err } isTip := true - summary, err := s.daemon.SupportCreate(s.lbryChannelID, fmt.Sprintf("%.6f", supportAmount), &isTip, nil, nil) + summary, err := s.daemon.SupportCreate(s.lbryChannelID, fmt.Sprintf("%.6f", supportAmount), &isTip, nil, []string{defaultAccount}) if err != nil { return errors.Err(err) } diff --git a/sdk/api.go b/sdk/api.go index c240846..07b884c 100644 --- a/sdk/api.go +++ b/sdk/api.go @@ -82,13 +82,14 @@ func (a *APIConfig) FetchChannels(status string, cp *SyncProperties) ([]YoutubeC if err != nil { return nil, errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.FetchChannels(status, cp) } - defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) var response apiJobsResponse err = json.Unmarshal(body, &response) if err != nil { @@ -139,14 +140,14 @@ func (a *APIConfig) SetChannelCert(certHex string, channelID string) error { if err != nil { return errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.SetChannelCert(certHex, channelID) } - defer res.Body.Close() - - body, _ := ioutil.ReadAll(res.Body) var response apiSetChannelCertResponse err = json.Unmarshal(body, &response) if err != nil { @@ -182,13 +183,14 @@ func (a *APIConfig) SetChannelStatus(channelID string, status string, failureRea if err != nil { return nil, nil, errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.SetChannelStatus(channelID, status, failureReason, transferState) } - defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) var response apiChannelStatusResponse err = json.Unmarshal(body, &response) if err != nil { @@ -226,13 +228,14 @@ func (a *APIConfig) SetChannelClaimID(channelID string, channelClaimID string) e if err != nil { return errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.SetChannelClaimID(channelID, channelClaimID) } - defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) var response apiChannelStatusResponse err = json.Unmarshal(body, &response) if err != nil { @@ -264,13 +267,14 @@ func (a *APIConfig) DeleteVideos(videos []string) error { if err != nil { return errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.DeleteVideos(videos) } - defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) var response struct { Success bool `json:"success"` Error null.String `json:"error"` @@ -336,13 +340,14 @@ func (a *APIConfig) MarkVideoStatus(status VideoStatus) error { if err != nil { return errors.Err(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != http.StatusOK { util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(body) time.Sleep(30 * time.Second) return a.MarkVideoStatus(status) } - defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) var response struct { Success bool `json:"success"` Error null.String `json:"error"`