import { MetricType } from '../MetricType' ; import { Counting , Metric } from '../domain' ; /**
 * <pre>
 * An incrementing and decrementing counter metric.
 *
 * 计数器型指标，适用于记录调用总量等类型的数据
 * </pre>
 */ export interface ICounter extends Metric , Counting { /**
     * Increment the counter by one.
     * 计数器加1
     */ inc ( ) ; param n inc ( n ) ; /**
     * Decrement the counter by one.
     * 计数器减1
     */ dec ( ) ; param n dec ( n ) ; } const MAX_COUNTER_VALUE = Math . pow ( 2 , 32 ) ; export class BaseCounter implements ICounter { count : number = 0 ; type = MetricType . COUNTER ; inc ( n ? ) { if ( ! n ) { n = 1 ; } this . count += n ; if ( this . count > MAX_COUNTER_VALUE ) { this . count -= ( MAX_COUNTER_VALUE + 1 ) ; } } dec ( n ? ) { if ( ! n ) { n = 1 ; } this . count -= n ; if ( this . count < 0 ) { this . count = 0 ; } } clear ( ) { this . count = 0 ; } getCount ( ) { return this . count ; } }