@@ -553,6 +553,18 @@ synchronized void increment(long ms)
553553 summedTime += ms ;
554554 }
555555
556+ /**
557+ * Check whether adding a certain duration to the already sent time
558+ * would exceed the length of a cost interval.
559+ * @param durationInMs the duration to check.
560+ * @return true if the summed time plus the given duration
561+ * would exceed the length of a cost interval, false otherwise.
562+ */
563+ boolean aboveCostInterval (long durationInMs )
564+ {
565+ return summedTime + durationInMs >= INTERVAL_LENGTH_MS ;
566+ }
567+
556568 /**
557569 * Increments the Google Cloud Speech API requests counter.
558570 */
@@ -723,6 +735,12 @@ void sentRequest(TranscriptionRequest request)
723735 // ByteString
724736 byte [] audio = request .getAudio ();
725737 ByteString audioBytes = ByteString .copyFrom (audio );
738+ long durationInMs = request .getDurationInMs ();
739+
740+ if (costLogger .aboveCostInterval (durationInMs ))
741+ {
742+ this .terminateCurrentSession ();
743+ }
726744
727745 synchronized (currentRequestObserverLock )
728746 {
@@ -735,7 +753,7 @@ void sentRequest(TranscriptionRequest request)
735753 = createObserver (getRecognitionConfig (request ));
736754 }
737755
738- costLogger .increment (request . getDurationInMs () );
756+ costLogger .increment (durationInMs );
739757 costLogger .incrementRequestsCounter ();
740758
741759 currentRequestObserver .onNext (
0 commit comments