I have a service, call it Service A
, that is built on a Storage Layer
, call it DB
, that is 5-6 levels deep. This layer is depended upon by many other services.
Service A
is having memory issues and the fix is to throw an exception from DB
if the resource we are trying to fetch is too large for Service A
. The resource itself is fetched in multiple roundtrips so we are counting the number of roundtrips and it is too high, we throw and abort and try a different, slower, more expensive code path.
The problem is this limit is specific to Service A
. All other services that depend on DB
do not need this limit.
Solution 1: read global setting set by Service
FetchResource(...) {
if (numberOfRoundTrips > ServiceDefinedLimit) {
throw new Exception("Resource is too large");
}
}
Right now the limit is Infinity for all services but Service A
, which seems sketchy. Otherwise it is not conceptually different from setting some config setting that says "ThrowIfResourceTooLarge".
Solution 2: pass in a boolean
FetchResource(..., bool throwIfSizeTooLarge) {
if (numberOfRoundTrips > HardCodedThreshold && throwIfSizetooLarge) {
throw new Exception("Resource is too large");
}
}
The problem here is that it just seems like a more complex version of Solution 1. In addition, I have to modify every layer from the caller all the way to this function.
Solution 3: Pass in a function
FetchResource(..., Func<State> condition) {
if (condition(numberOfRoundTrips, ...)) {
throw new Exception("Resource is too large");
}
}
The problem with the third approach is, once again, modifying all the layers above it to pipe this value from the caller to this layer. It seems conceptually right to me.
Looking for people's feedback on which would be the best approach. The more I think about it, the more #1 seems correct, and #3 seems like nice to have but too much work to implement now.