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)
| 936 | // In the interests of simplicity and determinism, this code does not attempt |
| 937 | // to reset the record size once the connection is idle, however. |
| 938 | func (c *Conn) maxPayloadSizeForWrite(typ recordType) int { |
| 939 | if c.config.DynamicRecordSizingDisabled || typ != recordTypeApplicationData { |
| 940 | return maxPlaintext |
| 941 | } |
| 942 | |
| 943 | if c.bytesSent >= recordSizeBoostThreshold { |
| 944 | return maxPlaintext |
| 945 | } |
| 946 | |
| 947 | // Subtract TLS overheads to get the maximum payload size. |
| 948 | payloadBytes := tcpMSSEstimate - recordHeaderLen - c.out.explicitNonceLen() |
| 949 | if c.out.cipher != nil { |
| 950 | switch ciph := c.out.cipher.(type) { |
| 951 | case cipher.Stream: |
| 952 | payloadBytes -= c.out.mac.Size() |
| 953 | case cipher.AEAD: |
| 954 | payloadBytes -= ciph.Overhead() |
| 955 | case cbcMode: |
| 956 | blockSize := ciph.BlockSize() |
| 957 | // The payload must fit in a multiple of blockSize, with |
| 958 | // room for at least one padding byte. |
| 959 | payloadBytes = (payloadBytes & ^(blockSize - 1)) - 1 |
| 960 | // The MAC is appended before padding so affects the |
| 961 | // payload size directly. |
| 962 | payloadBytes -= c.out.mac.Size() |
| 963 | default: |
| 964 | panic("unknown cipher type") |
| 965 | } |
| 966 | } |
| 967 | if c.vers == VersionTLS13 { |
| 968 | payloadBytes-- // encrypted ContentType |
| 969 | } |
| 970 | |
| 971 | // Allow packet growth in arithmetic progression up to max. |
| 972 | pkt := c.packetsSent |
| 973 | c.packetsSent++ |
| 974 | if pkt > 1000 { |
| 975 | return maxPlaintext // avoid overflow in multiply below |
| 976 | } |
| 977 | |
| 978 | n := payloadBytes * int(pkt+1) |
| 979 | if n > maxPlaintext { |
| 980 | n = maxPlaintext |
| 981 | } |
| 982 | return n |
| 983 | } |
| 984 | |
| 985 | func (c *Conn) write(data []byte) (int, error) { |
| 986 | if c.buffering { |
no test coverage detected