Skip to content

Commit

Permalink
feat: retry on 5xx (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Apr 15, 2024
1 parent b34b1a3 commit 86ab584
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ func NewProduct(product string) (Product, error) {
}, nil
}

const maxRetries = 5

// CheckWithContext verifies a license key against a product in Gumroad.
func (gp Product) VerifyWithContext(ctx context.Context, key string) error {
return gp.doVerify(ctx, key, 1)
}

func (gp Product) doVerify(ctx context.Context, key string, try int) error {
// early return if license key is empty
if key == "" {
return errors.New("license: license key cannot be empty")
Expand All @@ -86,6 +92,15 @@ func (gp Product) VerifyWithContext(ctx context.Context, key string) error {
}
defer resp.Body.Close()

// something on server side, should probably retry...
if resp.StatusCode >= 500 {
if try == maxRetries {
return fmt.Errorf("license: likely gumroad issue: %s", string(bts))
}
time.Sleep(time.Duration(try*500) * time.Millisecond)
return gp.doVerify(ctx, key, try+1)
}

var gumroad GumroadResponse
if err := json.Unmarshal(bts, &gumroad); err != nil {
return fmt.Errorf("license: failed check license: %w", err)
Expand Down
46 changes: 45 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gumroad
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand All @@ -14,6 +15,8 @@ import (
"time"
)

const license = "DEADBEEF-CAFE1234-5678DEAD-BEEFCAFE"

func TestIntegrationInvalidLicense(t *testing.T) {
t.Parallel()
expected := "license: invalid license: That license does not exist for the provided product."
Expand Down Expand Up @@ -71,6 +74,47 @@ func TestErrors(t *testing.T) {
}
}

func Test5xx(t *testing.T) {
t.Parallel()

calls := 0

// server will stand in for GumRoad, and assume that any license it sees is invalid
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
calls++
t.Log("try", calls)
if calls == 3 {
bts, _ := json.Marshal(GumroadResponse{
Success: true,
Purchase: Purchase{
SaleTimestamp: time.Now(),
Email: "[email protected]",
},
})
_, _ = w.Write(bts)
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`some error`))
}))
t.Cleanup(server.Close)

p, err := NewProduct("product")
if err != nil {
t.Errorf("unexpected error %v", err)
}
p.API = server.URL
p.Client = server.Client()

err = p.Verify(license)
if err != nil {
t.Fatal("expected no error")
}
if calls != 3 {
t.Errorf("should have called the api 3 times, but called %d", calls)
}
}

func TestMITM(t *testing.T) {
t.Parallel()
license := "DEADBEEF-CAFE1234-5678DEAD-BEEFCAFE"
Expand Down Expand Up @@ -136,7 +180,7 @@ func TestMITM(t *testing.T) {
}))
t.Cleanup(mitm.Close)
// Throw away the log message from http.Server.go complaining about the invalid TLS cert
mitm.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
mitm.Config.ErrorLog = log.New(io.Discard, "", 0)

p.API = mitm.URL
// Set the client back to the default, which doesn't trust the test certificate used by mitm
Expand Down

0 comments on commit 86ab584

Please sign in to comment.