override fun flush()

in core/src/software/aws/toolkits/core/telemetry/TelemetryBatcher.kt [78:112]


    override fun flush(retry: Boolean) {
        if (!isTelemetryEnabled.get()) {
            return
        }

        while (!eventQueue.isEmpty()) {
            val batch: ArrayList<MetricEvent> = arrayListOf()

            while (!eventQueue.isEmpty() && batch.size < maxBatchSize) {
                batch.add(eventQueue.pop())
            }

            val stop = runBlocking {
                try {
                    publisher.publish(batch)
                } catch (e: Exception) {
                    LOG.warn(e) { "Failed to publish metrics" }
                    val shouldRetry = retry && when (e) {
                        is SdkServiceException -> e.statusCode() !in 400..499
                        else -> true
                    }
                    if (shouldRetry) {
                        LOG.warn { "Telemetry metrics failed to publish, retrying later..." }
                        eventQueue.addAll(batch)
                        // don't want an infinite loop...
                        return@runBlocking true
                    }
                }
                return@runBlocking false
            }
            if (stop) {
                return
            }
        }
    }