ExponentialRetryPolicy materializes GetIntervals()
See original GitHub issueThe constructor of ExponentialRetryPolicy
materializes the IEnumerable<TimeSpan>
returned from GetIntervals()
. This seems directly opposed to its nature:
- It is an iterator function.
- It treats
Int32.MaxValue
as unlimited.
When using retryLimit: Int32.MaxValue
, which GetIntervals()
explicitly special-cases, the loop is infinite (until ToArray()
tries to allocate an array that is too large).
Even when avoiding Int32.MaxValue
, when trying to accommodate a very large number of attempts, the materialized array can take a lot of memory, when the intervals could just be calculated on-the-fly. Materialization also incurs a lot of calls to Random.Next()
that would otherwise have been amortized over the lifetime of the policy instance.
A good alternative property type to TimeSpan[]
might be IEnumerator<TimeSpan>
, since it enforces forward-only behavior. It is a public property, unfortunately, so I’m not sure how far the impact reaches.
I ran into this issue when I was trying to use exponential message retries in MassTransit 7.0.7.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
FYI, I finally got around to cleaning this up a bit.
No worries, appreciate the concern to recheck the logic!