onResponse method
Called when the response is about to be resolved.
Implementation
@override
Future<void> onResponse(
Response<dynamic> response,
ResponseInterceptorHandler handler,
) async {
final options = response.requestOptions;
// Pass-through: log real elapsed time, no extra delay.
if (options.extra.containsKey(_startKey)) {
final startUs = options.extra[_startKey] as int;
final elapsedMs = (_nowMicros() - startUs) ~/ 1000;
_engine.record(
_entry(options, RequestOutcome.ok, '${elapsedMs}ms', null),
);
handler.next(response);
return;
}
final priorDelay = (options.extra[_planKey] as Duration?) ?? Duration.zero;
// Download bandwidth delay based on the response size.
final responseBytes = _bytesOf(response.headers.map, response.data);
final downloadDelay = _engine.bandwidthDelay(responseBytes);
if (downloadDelay > Duration.zero) {
await Future<void>.delayed(downloadDelay);
}
final artificial = priorDelay + downloadDelay;
final throttled = artificial > Duration.zero;
_engine.record(
_entry(
options,
throttled ? RequestOutcome.throttled : RequestOutcome.ok,
throttled ? '+${artificial.inMilliseconds}ms' : '0ms',
throttled ? artificial : null,
),
);
handler.next(response);
}