@@ -63,8 +63,6 @@ const (
63
63
// The maximum write buffer size. This *must* be multiple of
64
64
// altsRecordDefaultLength.
65
65
altsWriteBufferMaxSize = 512 * 1024 // 512KiB
66
- // The initial buffer used to read from the network.
67
- altsReadBufferInitialSize = 32 * 1024 // 32KiB
68
66
)
69
67
70
68
var (
@@ -85,7 +83,7 @@ type conn struct {
85
83
net.Conn
86
84
crypto ALTSRecordCrypto
87
85
// buf holds data that has been read from the connection and decrypted,
88
- // but has not yet been returned by Read. It is a sub-slice of protected.
86
+ // but has not yet been returned by Read.
89
87
buf []byte
90
88
payloadLengthLimit int
91
89
// protected holds data read from the network but have not yet been
@@ -113,13 +111,21 @@ func NewConn(c net.Conn, side core.Side, recordProtocol string, key []byte, prot
113
111
}
114
112
overhead := MsgLenFieldSize + msgTypeFieldSize + crypto .EncryptionOverhead ()
115
113
payloadLengthLimit := altsRecordDefaultLength - overhead
116
- // We pre-allocate protected to be of size 32KB during initialization.
117
- // We increase the size of the buffer by the required amount if it can't
118
- // hold a complete encrypted record.
119
- protectedBuf := make ([]byte , max (altsReadBufferInitialSize , len (protected )))
120
- // Copy additional data from hanshaker service.
121
- copy (protectedBuf , protected )
122
- protectedBuf = protectedBuf [:len (protected )]
114
+ var protectedBuf []byte
115
+ if protected == nil {
116
+ // We pre-allocate protected to be of size
117
+ // 2*altsRecordDefaultLength-1 during initialization. We only
118
+ // read from the network into protected when protected does not
119
+ // contain a complete frame, which is at most
120
+ // altsRecordDefaultLength-1 (bytes). And we read at most
121
+ // altsRecordDefaultLength (bytes) data into protected at one
122
+ // time. Therefore, 2*altsRecordDefaultLength-1 is large enough
123
+ // to buffer data read from the network.
124
+ protectedBuf = make ([]byte , 0 , 2 * altsRecordDefaultLength - 1 )
125
+ } else {
126
+ protectedBuf = make ([]byte , len (protected ))
127
+ copy (protectedBuf , protected )
128
+ }
123
129
124
130
altsConn := & conn {
125
131
Conn : c ,
@@ -156,26 +162,11 @@ func (p *conn) Read(b []byte) (n int, err error) {
156
162
// Check whether a complete frame has been received yet.
157
163
for len (framedMsg ) == 0 {
158
164
if len (p .protected ) == cap (p .protected ) {
159
- // We can parse the length header to know exactly how large
160
- // the buffer needs to be to hold the entire frame.
161
- length , didParse := parseMessageLength (p .protected )
162
- if ! didParse {
163
- // The protected buffer is initialized with a capacity of
164
- // larger than 4B. It should always hold the message length
165
- // header.
166
- panic (fmt .Sprintf ("protected buffer length shorter than expected: %d vs %d" , len (p .protected ), MsgLenFieldSize ))
167
- }
168
- oldProtectedBuf := p .protected
169
- // The new buffer must be able to hold the message length header
170
- // and the entire message.
171
- requiredCapacity := int (length ) + MsgLenFieldSize
172
- p .protected = make ([]byte , requiredCapacity )
173
- // Copy the contents of the old buffer and set the length of the
174
- // new buffer to the number of bytes already read.
175
- copy (p .protected , oldProtectedBuf )
176
- p .protected = p .protected [:len (oldProtectedBuf )]
165
+ tmp := make ([]byte , len (p .protected ), cap (p .protected )+ altsRecordDefaultLength )
166
+ copy (tmp , p .protected )
167
+ p .protected = tmp
177
168
}
178
- n , err = p .Conn .Read (p .protected [len (p .protected ):cap (p .protected )])
169
+ n , err = p .Conn .Read (p .protected [len (p .protected ):min ( cap (p .protected ), len ( p . protected ) + altsRecordDefaultLength )])
179
170
if err != nil {
180
171
return 0 , err
181
172
}
@@ -194,15 +185,6 @@ func (p *conn) Read(b []byte) (n int, err error) {
194
185
}
195
186
ciphertext := msg [msgTypeFieldSize :]
196
187
197
- // Decrypt directly into the buffer, avoiding a copy from p.buf if
198
- // possible.
199
- if len (b ) >= len (ciphertext ) {
200
- dec , err := p .crypto .Decrypt (b [:0 ], ciphertext )
201
- if err != nil {
202
- return 0 , err
203
- }
204
- return len (dec ), nil
205
- }
206
188
// Decrypt requires that if the dst and ciphertext alias, they
207
189
// must alias exactly. Code here used to use msg[:0], but msg
208
190
// starts MsgLenFieldSize+msgTypeFieldSize bytes earlier than
0 commit comments