Skip to content

Commit

Permalink
fix(audit): fix context cancelled on concurrent calls (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd authored Jun 19, 2024
1 parent be81a3b commit 0970074
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/comment/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s *Service) Create(ctx context.Context, c *domain.Comment) error {
}

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyCreate, c); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err, "appeal_id", c.ParentID, "comment_id", c.ID)
}
Expand Down
2 changes: 2 additions & 0 deletions core/policy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (s *Service) Create(ctx context.Context, p *domain.Policy) error {
}

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyPolicyCreate, p); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down Expand Up @@ -232,6 +233,7 @@ func (s *Service) Update(ctx context.Context, p *domain.Policy) error {
}

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyPolicyUpdate, p); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down
4 changes: 4 additions & 0 deletions core/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (s *Service) BulkUpsert(ctx context.Context, resources []*domain.Resource)
}

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyResoruceBulkUpsert, resources); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down Expand Up @@ -122,6 +123,7 @@ func (s *Service) Update(ctx context.Context, r *domain.Resource) error {
r.UpdatedAt = res.UpdatedAt

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyResourceUpdate, r); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down Expand Up @@ -165,6 +167,7 @@ func (s *Service) Delete(ctx context.Context, id string) error {
s.logger.Info(ctx, "resource deleted", "resource", id)

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyResourceDelete, map[string]interface{}{"id": id}); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand All @@ -181,6 +184,7 @@ func (s *Service) BatchDelete(ctx context.Context, ids []string) error {
s.logger.Info(ctx, "resources deleted", "resources", len(ids))

go func() {
ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyResourceBatchDelete, map[string]interface{}{"ids": ids}); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down

0 comments on commit 0970074

Please sign in to comment.