Package com.tnsai.resilience
Class MeteredHandle
java.lang.Object
com.tnsai.resilience.MeteredHandle
- All Implemented Interfaces:
AutoCloseable
Per-invocation handle for a
@Metered action — opened by
MeteredResolver.start(com.tnsai.metadata.ActionMetadata, com.tnsai.roles.Role) immediately before action dispatch
and closed in a finally block. Records exactly one
counter+latency entry to ResilienceMetrics per invocation.
Caller pattern (already wired in
ActionExecutor):
MeteredHandle metered = MeteredResolver.start(action, role);
try {
Object result = executor.execute(...);
metered.recordSuccess();
return result;
} catch (Exception e) {
// Narrow to Exception per the codebase's no-broad-catch rule
// — JVM-fatal Errors propagate; the close() finally still runs.
metered.recordFailure();
throw e;
} finally {
metered.close();
}
close() is idempotent and safe to call without
having recorded an outcome — in that case the call is logged as
a failure (the action threw before reaching either branch) so the
counter stays consistent.
The NOOP singleton is returned by
MeteredResolver.start(com.tnsai.metadata.ActionMetadata, com.tnsai.roles.Role) when no @Metered annotation
is present, so the calling pattern never branches.
- Since:
- 0.5.4 (issue #173, Phase 1)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MeteredHandleNo-op handle returned for actions without@Metered. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidMarks the action's invocation as failed.voidMarks the action's invocation as successful.
-
Field Details
-
NOOP
No-op handle returned for actions without@Metered. All methods are cheap no-ops; the calling site uses the same try/finally shape whether metering is on or off.
-
-
Method Details
-
recordSuccess
public void recordSuccess()Marks the action's invocation as successful. Call exactly once beforeclose(); subsequent calls in the same handle are ignored to keep the counter consistent. -
recordFailure
public void recordFailure()Marks the action's invocation as failed. Same single-call contract asrecordSuccess(). -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-