Skip to content

Commit

Permalink
add simple retry mechanism for stripe tips
Browse files Browse the repository at this point in the history
better catch APIs issues
  • Loading branch information
nikooo777 committed Sep 11, 2023
1 parent 8fa6596 commit d72d655
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/fatih/color v1.15.0
github.com/fatih/structs v1.1.0
github.com/friendsofgo/errors v0.9.2
github.com/go-co-op/gocron v1.32.1
github.com/go-co-op/gocron v1.33.1
github.com/go-sql-driver/mysql v1.7.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/rpc v1.2.0
Expand All @@ -28,7 +28,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/profile v1.7.0
github.com/prometheus/client_golang v1.16.0
github.com/rs/cors v1.9.0
github.com/rs/cors v1.10.0
github.com/rubenv/sql-migrate v1.5.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cast v1.5.1
Expand All @@ -55,7 +55,7 @@ require (
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/johntdyer/slack-go v0.0.0-20230314151037-c5bf334f9b6e // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmV
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-co-op/gocron v1.32.1 h1:h+StA6Qzlv+ImlCaLfA26rLN9eS/l4sO7oWmPUbRVIY=
github.com/go-co-op/gocron v1.32.1/go.mod h1:UGz2oYvVS6PsqlwuOdo5L1Djsg/cQjxJ6T5ntkhp9Bg=
github.com/go-co-op/gocron v1.33.1 h1:wjX+Dg6Ae29a/f9BSQjY1Rl+jflTpW9aDyMqseCj78c=
github.com/go-co-op/gocron v1.33.1/go.mod h1:NLi+bkm4rRSy1F8U7iacZOz0xPseMoIOnvabGoSe/no=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -279,8 +279,9 @@ github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8I
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
Expand Down Expand Up @@ -511,8 +512,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/cors v1.10.0 h1:62NOS1h+r8p1mW6FM0FSB0exioXLhd/sh15KpjWBZ+8=
github.com/rs/cors v1.10.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rubenv/sql-migrate v1.5.2 h1:bMDqOnrJVV/6JQgQ/MxOpU+AdO8uzYYA/TxFUBzFtS0=
github.com/rubenv/sql-migrate v1.5.2/go.mod h1:H38GW8Vqf8F0Su5XignRyaRcbXbJunSWxs+kmzlg0Is=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
20 changes: 12 additions & 8 deletions server/lbry/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lbry
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -126,21 +127,24 @@ func notify(options NotifyOptions) error {
return errors.Err("No response from internal APIs")
}
defer helper.CloseBody(response.Body)
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Err(err)
}
var me CommentResponse
err = json.Unmarshal(b, &me)
b, err := io.ReadAll(response.Body)
if err != nil {
return errors.Err(err)
}
if response.StatusCode > 200 {
if response.StatusCode <= 300 {
if response.StatusCode > http.StatusOK {
if response.StatusCode <= http.StatusMultipleChoices {
logrus.Warning("Notification Failure[Status - ", response.StatusCode, "] : ")
} else {
logrus.Error("Notification Failure[Status - ", response.StatusCode, "] : ")
}
return errors.Err("API returned non-200 status code: %d - %s", response.StatusCode, string(b))
}
//todo: add recursion if necessary

var me CommentResponse
err = json.Unmarshal(b, &me)
if err != nil {
return errors.Err(err)
}
return nil
}
13 changes: 9 additions & 4 deletions server/services/v1/comments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func updateSupportInfo(request *createRequest) error {
triesLeft := 3
for {
triesLeft--
err := updateSupportInfoAttempt(request)
err := updateSupportInfoAttempt(request, true)
if err == nil {
return nil
}
Expand All @@ -581,7 +581,7 @@ func updateSupportInfo(request *createRequest) error {
}
}

func updateSupportInfoAttempt(request *createRequest) error {
func updateSupportInfoAttempt(request *createRequest, retry bool) error {
if request.args.PaymentIntentID != nil {
env := ""
if request.args.Environment != nil {
Expand All @@ -590,8 +590,13 @@ func updateSupportInfoAttempt(request *createRequest) error {
paymentintentClient := &paymentintent.Client{B: stripe.GetBackend(stripe.APIBackend), Key: config.ConnectAPIKey(config.From(env))}
pi, err := paymentintentClient.Get(*request.args.PaymentIntentID, &stripe.PaymentIntentParams{})
if err != nil {
logrus.Error(errors.Prefix("could not get payment intent %s", *request.args.PaymentIntentID))
return errors.Err("could not validate tip")
if !retry {
logrus.Error(errors.Prefix("could not get payment intent %s", *request.args.PaymentIntentID))
return errors.Err("could not validate tip")
}
// in the rare event that the payment intent is not found, wait a bit and try again once
time.Sleep(5 * time.Second)
return updateSupportInfoAttempt(request, false)
}
request.comment.Amount.SetValid(uint64(pi.Amount))
request.comment.IsFiat = true
Expand Down

0 comments on commit d72d655

Please sign in to comment.