in business-model/src/main/java/com/google/cloud/orderbook/MatcherContext.java [168:223]
private long getNextEventTimeMillis() {
long eventTimeMillis;
// Just use system time and go as fast as possible
if (mode != TimeThrottleMode.UNTHROTTLED_EVENTS_THROTTLED_SIMULATED_TIME) {
eventTimeMillis = System.currentTimeMillis();
}
//
// Now we need to calculate the synthetic time (bucketing)
//
// Count events in the bucket and total events
eventsInBucket ++;
totalEvents ++;
// If bucket is full, need to delay and/or shift bucket
if (eventsInBucket == eventsPerBucket) {
// If throttled system time, we need a real delay
// (we don't need absolute precision, so interruptions we can ignore)
if (mode == TimeThrottleMode.THROTTLED_SYSTEM_TIME) {
long neededDelay = nextBucketTime - System.currentTimeMillis();
if (neededDelay > 0) {
try {
Thread.sleep(neededDelay);
} catch (InterruptedException e) {}
}
}
// Shift to next time bucket
nextBucketTime += MILLISECOND_BUCKET_SIZE;
eventsInBucket = 0;
}
// If it's simulated time, return the bucket time
if (mode == TimeThrottleMode.UNTHROTTLED_EVENTS_THROTTLED_SIMULATED_TIME) {
eventTimeMillis = nextBucketTime;
} else {
eventTimeMillis = System.currentTimeMillis();
}
// Check if we need to shutdown the queue due to time
if ((maxDurationSeconds > 0) &&
(eventTimeMillis - startTimeMillis)/1000 >= maxDurationSeconds) {
this.que.shutdown();
}
// Or shutdown due to max events
if (maxEvents > 0 && totalEvents >= maxEvents) {
this.que.shutdown();
}
return eventTimeMillis;
}