Skip to content

Commit

Permalink
Moved subscription backoff max and alert threshold to subscribe opts
Browse files Browse the repository at this point in the history
Instead of defining those values per-contract we defined them only one
time now.
  • Loading branch information
pdyraga committed Jan 28, 2021
1 parent f5ebf3c commit b3b56f6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 36 deletions.
16 changes: 16 additions & 0 deletions pkg/chain/ethereum/ethutil/subscribe_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const (
// pulled from the chain by the subscription monitoring mechanism if no
// other value is provided in SubscribeOpts when creating the subscription.
DefaultSubscribeOptsPastBlocks = 100

// SubscriptionBackoffMax is the maximum backoff time between event
// resubscription attempts.
SubscriptionBackoffMax = 2 * time.Minute

// SubscriptionAlertThreshold is time threshold below which event
// resubscription emits an error to the logs.
// WS connection can be dropped at any moment and event resubscription will
// follow. However, if WS connection for event subscription is getting
// dropped too often, it may indicate something is wrong with Ethereum
// client. This constant defines the minimum lifetime of an event
// subscription required before the subscription failure happens and
// resubscription follows so that the resubscription does not emit an error
// to the logs alerting about potential problems with Ethereum client
// connection.
SubscriptionAlertThreshold = 15 * time.Minute
)

// SubscribeOpts specifies optional configuration options that can be passed
Expand Down
15 changes: 0 additions & 15 deletions tools/generators/ethereum/contract.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ import (
// included or excluded from logging at startup by name.
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")

const (
// Maximum backoff time between event resubscription attempts.
{{.ShortVar}}SubscriptionBackoffMax = 2 * time.Minute

// Threshold below which event resubscription emits an error to the logs.
// WS connection can be dropped at any moment and event resubscription will
// follow. However, if WS connection for event subscription is getting
// dropped too often, it may indicate something is wrong with Ethereum
// client. This constant defines the minimum lifetime of an event
// subscription required before the subscription failure happens and
// resubscription follows so that the resubscription does not emit an error
// to the logs alerting about potential problems with Ethereum client.
{{.ShortVar}}SubscriptionAlertThreshold = 15 * time.Minute
)

type {{.Class}} struct {
contract *abi.{{.AbiClass}}
contractAddress common.Address
Expand Down
6 changes: 3 additions & 3 deletions tools/generators/ethereum/contract_events.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) watch{{$event.CapsName}}(
)
}

sub := ethutil.WithResubscription(
{{$contract.ShortVar}}SubscriptionBackoffMax,
return ethutil.WithResubscription(
ethutil.SubscriptionBackoffMax,
subscribeFn,
{{$contract.ShortVar}}SubscriptionAlertThreshold,
ethutil.SubscriptionAlertThreshold,
thresholdViolatedFn,
subscriptionFailedFn,
)
Expand Down
6 changes: 3 additions & 3 deletions tools/generators/ethereum/contract_events_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) watch{{$event.CapsName}}(
)
}
sub := ethutil.WithResubscription(
{{$contract.ShortVar}}SubscriptionBackoffMax,
return ethutil.WithResubscription(
ethutil.SubscriptionBackoffMax,
subscribeFn,
{{$contract.ShortVar}}SubscriptionAlertThreshold,
ethutil.SubscriptionAlertThreshold,
thresholdViolatedFn,
subscriptionFailedFn,
)
Expand Down
15 changes: 0 additions & 15 deletions tools/generators/ethereum/contract_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ import (
// included or excluded from logging at startup by name.
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")
const (
// Maximum backoff time between event resubscription attempts.
{{.ShortVar}}SubscriptionBackoffMax = 2 * time.Minute
// Threshold below which event resubscription emits an error to the logs.
// WS connection can be dropped at any moment and event resubscription will
// follow. However, if WS connection for event subscription is getting
// dropped too often, it may indicate something is wrong with Ethereum
// client. This constant defines the minimum lifetime of an event
// subscription required before the subscription failure happens and
// resubscription follows so that the resubscription does not emit an error
// to the logs alerting about potential problems with Ethereum client.
{{.ShortVar}}SubscriptionAlertThreshold = 15 * time.Minute
)
type {{.Class}} struct {
contract *abi.{{.AbiClass}}
contractAddress common.Address
Expand Down

0 comments on commit b3b56f6

Please sign in to comment.