Skip to content

Commit acbd122

Browse files
author
jeffyanta
authored
Improvements to Twitter registration DB transaction (#108)
1 parent 617ac80 commit acbd122

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

pkg/code/async/user/twitter.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,34 +124,38 @@ func (p *service) processNewTwitterRegistrations(ctx context.Context) error {
124124
}
125125

126126
// Save the updated tipping information
127+
var isRegistered bool
127128
err = p.data.ExecuteInTx(ctx, sql.LevelDefault, func(ctx context.Context) error {
129+
var isNonceUsed bool
128130
err = p.data.MarkTwitterNonceAsUsed(ctx, tweet.ID, *registrationNonce)
129-
if err != nil {
130-
return err
131-
}
132-
133-
err = p.updateCachedTwitterUser(ctx, tweet.AdditionalMetadata.Author, tipAccount)
134-
if err != nil {
131+
switch err {
132+
case nil:
133+
case twitter.ErrDuplicateNonce:
134+
isNonceUsed = true
135+
default:
135136
return err
136137
}
137138

138-
err = p.data.MarkTweetAsProcessed(ctx, tweet.ID)
139-
if err != nil {
140-
return err
139+
// Only save updated tip addresses when the nonce isn't used
140+
if !isNonceUsed {
141+
err = p.updateCachedTwitterUser(ctx, tweet.AdditionalMetadata.Author, tipAccount)
142+
switch err {
143+
case nil:
144+
isRegistered = true
145+
case twitter.ErrDuplicateTipAddress:
146+
default:
147+
return err
148+
}
141149
}
142150

143-
return nil
151+
return p.data.MarkTweetAsProcessed(ctx, tweet.ID)
144152
})
153+
if err != nil {
154+
return errors.Wrap(err, "error saving registration details")
155+
}
145156

146-
switch err {
147-
case nil:
157+
if isRegistered {
148158
go push_util.SendTwitterAccountConnectedPushNotification(ctx, p.data, p.pusher, tipAccount)
149-
case twitter.ErrDuplicateTipAddress, twitter.ErrDuplicateNonce:
150-
// Any race conditions with duplicate nonces or tip addresses will are ignored
151-
//
152-
// todo: In the future, support multiple tip address mappings
153-
default:
154-
return errors.Wrap(err, "error saving new registration")
155159
}
156160
}
157161

0 commit comments

Comments
 (0)