Skip to content

Commit

Permalink
fix: try to handle concurrent saving better
Browse files Browse the repository at this point in the history
  • Loading branch information
fritterhoff committed Jan 23, 2024
1 parent 37a0395 commit 2bf6b1d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
14 changes: 11 additions & 3 deletions acme/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,17 @@ func http01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWeb
ch.Status = StatusValid
ch.Error = nil
ch.ValidatedAt = clock.Now().Format(time.RFC3339)

if err = db.UpdateChallenge(ctx, ch); err != nil {
return WrapErrorISE(err, "error updating challenge")
for {
if err = db.UpdateChallenge(ctx, ch); err != nil {
if strings.Contains(err.Error(), "changed since last read") {
// If the challenge has changed since we read it, then we
// don't want to overwrite the error.
logrus.Warn("challenge changed since last read -> retry saving")
continue
}
return WrapErrorISE(err, "error updating challenge")
}
break
}
return nil
}
Expand Down
16 changes: 12 additions & 4 deletions acme/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -74,11 +75,18 @@ func Connect(acmeDB acme.DB, host, user, password, organization string) (validat
ch.Status = acme.StatusValid
ch.Error = nil
ch.ValidatedAt = clock.Now().Format(time.RFC3339)

if err = acmeDB.UpdateChallenge(ctx, ch); err != nil {
logrus.Errorf("error updating challenge: %v", err)
} else {
for {
if err = acmeDB.UpdateChallenge(ctx, ch); err != nil {
if strings.Contains(err.Error(), "changed since last read") {
// If the challenge has changed since we read it, then we
// don't want to overwrite the error.
logrus.Warn("challenge changed since last read -> retry saving")
continue
}
logrus.Errorf("error updating challenge: %v", err)
}
logrus.Infof("challenge %s updated to valid", u.String())
break
}

})
Expand Down

0 comments on commit 2bf6b1d

Please sign in to comment.