Skip to content

Commit

Permalink
Fix: deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed May 17, 2022
1 parent 345fb2f commit 1bb8707
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cmd/metadata/models/contract_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"context"
"sync"
"time"

"github.com/dipdup-net/go-lib/database"
Expand Down Expand Up @@ -87,11 +88,13 @@ func (cm *ContractMetadata) BeforeUpdate(ctx context.Context) (context.Context,
// Contracts -
type Contracts struct {
db *database.PgGo

mx sync.Mutex
}

// NewContracts -
func NewContracts(db *database.PgGo) *Contracts {
return &Contracts{db}
return &Contracts{db: db}
}

// Get -
Expand All @@ -116,6 +119,9 @@ func (contracts *Contracts) Update(metadata []*ContractMetadata) error {
return nil
}

contracts.mx.Lock()
defer contracts.mx.Unlock()

_, err := contracts.db.DB().Model(&metadata).Column("metadata", "update_id", "status", "retry_count", "error").WherePK().Update()
return err
}
Expand All @@ -138,6 +144,9 @@ func (contracts *Contracts) Save(metadata []*ContractMetadata) error {
return nil
}

contracts.mx.Lock()
defer contracts.mx.Unlock()

_, err := contracts.db.DB().Model(&savings).
OnConflict("(network, contract) DO UPDATE").
Set("metadata = excluded.metadata, link = excluded.link, update_id = excluded.update_id, status = excluded.status").
Expand Down
11 changes: 10 additions & 1 deletion cmd/metadata/models/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"context"
"fmt"
"sync"
"time"

"github.com/dipdup-net/go-lib/database"
Expand Down Expand Up @@ -99,11 +100,13 @@ type TokenRepository interface {
// Tokens -
type Tokens struct {
db *database.PgGo

mx sync.Mutex
}

// NewTokens -
func NewTokens(db *database.PgGo) *Tokens {
return &Tokens{db}
return &Tokens{db: db}
}

// Get -
Expand All @@ -128,6 +131,9 @@ func (tokens *Tokens) Update(metadata []*TokenMetadata) error {
return nil
}

tokens.mx.Lock()
defer tokens.mx.Unlock()

_, err := tokens.db.DB().Model(&metadata).Column("metadata", "update_id", "status", "retry_count", "error").WherePK().Update()
return err
}
Expand All @@ -152,6 +158,9 @@ func (tokens *Tokens) Save(metadata []*TokenMetadata) error {
return nil
}

tokens.mx.Lock()
defer tokens.mx.Unlock()

_, err := tokens.db.DB().Model(&savings).
OnConflict("(network, contract, token_id) DO UPDATE").
Set("metadata = excluded.metadata, link = excluded.link, update_id = excluded.update_id, status = excluded.status").
Expand Down

1 comment on commit 1bb8707

@aopoltorzhicky
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.