package quic

import (
	

	
)

var (
	sendStreamBurstFreshness = 10 * time.Microsecond
	sendStreamTailDelay      = 5 * time.Microsecond
)

const (
	sendStreamBurstMinWrites      = 4
	sendStreamActivationThreshold = 1200
)

// activateOrDelayLocked decides whether a write that found the stream inactive
// should wake the sender now. It reports whether the caller must call
// onHasStreamData, which it must do without holding the mutex.
//
// go-iroh addition. Waking the sender on the first write of a burst hands the
// packetizer whatever few bytes have arrived, so a stream written in pieces
// much smaller than a packet sends half-empty packets: the producer never gets
// far enough ahead of the sender to fill one. A stream that has just been
// drained after a burst is therefore corked for sendStreamTailDelay, which
// lets the writes behind the first accumulate, and is uncorked early once the
// buffer holds sendStreamActivationThreshold bytes, roughly a packet's worth,
// so the delay never costs latency on a stream that already has enough to send.
// Only a stream that was drained mid-burst is corked, so an isolated write --
// the request half of a ping-pong -- still wakes the sender immediately.
func ( *SendStream) () bool {
	if !.corkPending {
		if .burstUntil.IsZero() || !monotime.Now().Before(.burstUntil) {
			.burstUntil = 0
			.active = true
			return true
		}
		.corkPending = true
	}
	if sendStreamTailDelay <= 0 || .bufferedWriteLen() >= sendStreamActivationThreshold {
		.stopActivationTimerLocked()
		.burstUntil = 0
		.corkPending = false
		.active = true
		return true
	}
	if .activationTimer == nil {
		.activationGen++
		 := .activationGen
		.activationTimer = time.AfterFunc(sendStreamTailDelay, func() {
			.activateAfterDelay()
		})
	}
	return false
}

// stopActivationTimerLocked cancels a pending cork timer. The generation
// counter makes a timer that has already fired a no-op.
func ( *SendStream) () {
	if .activationTimer == nil {
		return
	}
	.activationTimer.Stop()
	.activationTimer = nil
	.activationGen++
}

// activateAfterDelay wakes the sender for a stream whose cork timer expired.
func ( *SendStream) ( uint64) {
	.mutex.Lock()
	if  != .activationGen {
		.mutex.Unlock()
		return
	}
	.activationTimer = nil
	if .active || .shutdownErr != nil || .resetErr != nil ||
		(.bufferedWriteLen() == 0 && .dataForWriting == nil) {
		.mutex.Unlock()
		return
	}
	.burstUntil = 0
	.corkPending = false
	.active = true
	.mutex.Unlock()
	recordCorkTimerActivation()
	.sender.onHasStreamData(.streamID, )
}

// uncorkLocked drops any pending cork. An event other than an application
// write -- a flow control window opening, a shutdown -- must reach the sender
// without waiting out a tail delay that exists only to batch writes.
func ( *SendStream) () {
	.stopActivationTimerLocked()
	.burstUntil = 0
	.corkPending = false
}

// noteBufferedWriteLocked counts a write into the current burst episode.
func ( *SendStream) () {
	if .writesInEpisode < ^uint16(0) {
		.writesInEpisode++
	}
}

// endEpisodeLocked is called when the sender has drained the stream. A stream
// drained in the middle of a burst is marked fresh, so the next write corks
// rather than waking the sender on its own.
func ( *SendStream) () {
	if .writesInEpisode >= sendStreamBurstMinWrites {
		.burstUntil = monotime.Now().Add(sendStreamBurstFreshness)
	} else {
		.burstUntil = 0
	}
	.writesInEpisode = 0
	.corkPending = false
	.active = false
}