maxPayloadSizeForWrite returns the maximum TLS payload size to use for the next application data record. There is the following trade-off: - For latency-sensitive applications, such as web browsing, each TLS record should fit in one TCP segment. - For throughput-sensitive applications, such as larg
(typ recordType)
| 966 | // In the interests of simplicity and determinism, this code does not attempt |
| 967 | // to reset the record size once the connection is idle, however. |
| 968 | func (c *Conn) maxPayloadSizeForWrite(typ recordType) int { |
| 969 | if c.config.DynamicRecordSizingDisabled || typ != recordTypeApplicationData { |
| 970 | return maxPlaintext |
| 971 | } |
| 972 | |
| 973 | if c.bytesSent >= recordSizeBoostThreshold { |
| 974 | return maxPlaintext |
| 975 | } |
| 976 | |
| 977 | // Subtract TLS overheads to get the maximum payload size. |
| 978 | payloadBytes := tcpMSSEstimate - recordHeaderLen - c.out.explicitNonceLen() |
| 979 | if c.out.cipher != nil { |
| 980 | switch ciph := c.out.cipher.(type) { |
| 981 | case cipher.Stream: |
| 982 | payloadBytes -= c.out.mac.Size() |
| 983 | case cipher.AEAD: |
| 984 | payloadBytes -= ciph.Overhead() |
| 985 | case cbcMode: |
| 986 | blockSize := ciph.BlockSize() |
| 987 | // The payload must fit in a multiple of blockSize, with |
| 988 | // room for at least one padding byte. |
| 989 | payloadBytes = (payloadBytes & ^(blockSize - 1)) - 1 |
| 990 | // The MAC is appended before padding so affects the |
| 991 | // payload size directly. |
| 992 | payloadBytes -= c.out.mac.Size() |
| 993 | default: |
| 994 | panic("unknown cipher type") |
| 995 | } |
| 996 | } |
| 997 | if c.vers == VersionTLS13 { |
| 998 | payloadBytes-- // encrypted ContentType |
| 999 | } |
| 1000 | |
| 1001 | // Allow packet growth in arithmetic progression up to max. |
| 1002 | pkt := c.packetsSent |
| 1003 | c.packetsSent++ |
| 1004 | if pkt > 1000 { |
| 1005 | return maxPlaintext // avoid overflow in multiply below |
| 1006 | } |
| 1007 | |
| 1008 | n := payloadBytes * int(pkt+1) |
| 1009 | if n > maxPlaintext { |
| 1010 | n = maxPlaintext |
| 1011 | } |
| 1012 | return n |
| 1013 | } |
| 1014 | |
| 1015 | func (c *Conn) write(data []byte) (int, error) { |
| 1016 | if c.buffering { |
no test coverage detected