Skip to content

Commit

Permalink
fix: don't emit SessionIssued and SessionChanged events multiple time…
Browse files Browse the repository at this point in the history
…s when retrying database transactions
  • Loading branch information
alnr committed Apr 17, 2024
1 parent 4521229 commit bbd7447
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions persistence/sql/persister_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,21 @@ func (p *Persister) UpsertSession(ctx context.Context, s *session.Session) (err

s.NID = p.NetworkID(ctx)

var updated bool
defer func() {
if err != nil {
return
}
if updated {
trace.SpanFromContext(ctx).AddEvent(events.NewSessionChanged(ctx, string(s.AuthenticatorAssuranceLevel), s.ID, s.IdentityID))
} else {
trace.SpanFromContext(ctx).AddEvent(events.NewSessionIssued(ctx, string(s.AuthenticatorAssuranceLevel), s.ID, s.IdentityID))
}
}()
return errors.WithStack(p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
updated = false
exists := false
if !s.ID.IsNil() {
var err error
exists, err = tx.Where("id = ? AND nid = ?", s.ID, s.NID).Exists(new(session.Session))
if err != nil {
return sqlcon.HandleError(err)
Expand All @@ -201,7 +212,7 @@ func (p *Persister) UpsertSession(ctx context.Context, s *session.Session) (err
if err := tx.Update(s, "issued_at", "identity_id", "nid"); err != nil {
return sqlcon.HandleError(err)
}
trace.SpanFromContext(ctx).AddEvent(events.NewSessionChanged(ctx, string(s.AuthenticatorAssuranceLevel), s.ID, s.IdentityID))
updated = true
return nil
}

Expand All @@ -227,7 +238,6 @@ func (p *Persister) UpsertSession(ctx context.Context, s *session.Session) (err
}
}

trace.SpanFromContext(ctx).AddEvent(events.NewSessionIssued(ctx, string(s.AuthenticatorAssuranceLevel), s.ID, s.IdentityID))
return nil
}))
}
Expand Down

0 comments on commit bbd7447

Please sign in to comment.