Skip to content

Commit

Permalink
Feature: receiver timeout in config
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 6, 2024
1 parent ebf8354 commit 24c517c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions build/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ metadata:
workers_count: ${RECEIVER_WORKERS_COUNT:-10}
max_attempts: ${MAX_RETRY_COUNT:-5}
delay: 10
timeout: ${RECEIVER_TIMEOUT:-10}

http_timeout: 5
max_retry_count_on_error: ${MAX_RETRY_COUNT:-5}
Expand Down
7 changes: 4 additions & 3 deletions cmd/metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ type FillerConfig struct {

// ReceiverConfig -
type ReceiverConfig struct {
WorkersCount int `yaml:"workers_count" validate:"required,min=1"`
MaxAttempts int `yaml:"max_attempts" validate:"omitempty,min=1"`
Delay int `yaml:"delay" validate:"omitempty,min=1"`
WorkersCount int `yaml:"workers_count" validate:"required,min=1"`
MaxAttempts int `yaml:"max_attempts" validate:"omitempty,min=1"`
Delay int `yaml:"delay" validate:"omitempty,min=1"`
Timeout uint64 `yaml:"timeout" validate:"min=1"`
}
10 changes: 8 additions & 2 deletions cmd/metadata/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Receiver struct {
maxAttempts int
delay int
workersCount int
timeout time.Duration

wg *sync.WaitGroup
}
Expand All @@ -40,6 +41,7 @@ func NewReceiver(cfg ReceiverConfig, tm storage.ITokenMetadata, ipfsNode *ipfs.N
workersCount = 10
maxAttempts = 5
delay = 10
timeout = time.Second * 30
)

if cfg.WorkersCount > 0 {
Expand All @@ -51,6 +53,9 @@ func NewReceiver(cfg ReceiverConfig, tm storage.ITokenMetadata, ipfsNode *ipfs.N
if cfg.Delay > 0 {
delay = cfg.Delay
}
if cfg.Timeout > 0 {
timeout = time.Second * time.Duration(cfg.Timeout)
}

t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
Expand All @@ -67,6 +72,7 @@ func NewReceiver(cfg ReceiverConfig, tm storage.ITokenMetadata, ipfsNode *ipfs.N
maxAttempts: maxAttempts,
delay: delay,
workersCount: workersCount,
timeout: timeout,
wg: new(sync.WaitGroup),
}

Expand Down Expand Up @@ -143,7 +149,7 @@ func (r *Receiver) worker(ctx context.Context, task storage.TokenMetadata) {
Str("uri", *task.Uri).
Msg("try to receive token metadata")

timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*30)
timeoutCtx, cancel := context.WithTimeout(ctx, r.timeout)
defer cancel()

var err error
Expand All @@ -160,7 +166,7 @@ func (r *Receiver) worker(ctx context.Context, task storage.TokenMetadata) {
if err != nil {
eStr := err.Error()
task.Error = &eStr
log.Err(ErrInvalidUri).Str("uri", *task.Uri).Uint("attempt", task.Attempts).Msg("fail to receive metadata")
log.Err(err).Str("uri", *task.Uri).Uint("attempt", task.Attempts).Msg("fail to receive metadata")

if task.Attempts == uint(r.maxAttempts) {
task.Status = storage.StatusFailed
Expand Down

0 comments on commit 24c517c

Please sign in to comment.