NewServerTransport creates a http2 transport with conn and configuration options from config. It returns a non-nil transport and a nil error on success. On failure, it returns a nil transport and a non-nil error. For a special case where the underlying conn gets closed before the client preface cou
(conn net.Conn, config *ServerConfig)
| 149 | // underlying conn gets closed before the client preface could be read, it |
| 150 | // returns a nil transport and a nil error. |
| 151 | func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport, err error) { |
| 152 | var authInfo credentials.AuthInfo |
| 153 | rawConn := conn |
| 154 | if config.Credentials != nil { |
| 155 | var err error |
| 156 | conn, authInfo, err = config.Credentials.ServerHandshake(rawConn) |
| 157 | if err != nil { |
| 158 | // ErrConnDispatched means that the connection was dispatched away |
| 159 | // from gRPC; those connections should be left open. io.EOF means |
| 160 | // the connection was closed before handshaking completed, which can |
| 161 | // happen naturally from probers. Return these errors directly. |
| 162 | if err == credentials.ErrConnDispatched || err == io.EOF { |
| 163 | return nil, err |
| 164 | } |
| 165 | return nil, connectionErrorf(false, err, "ServerHandshake(%q) failed: %v", rawConn.RemoteAddr(), err) |
| 166 | } |
| 167 | } |
| 168 | writeBufSize := config.WriteBufferSize |
| 169 | readBufSize := config.ReadBufferSize |
| 170 | // The default header list size is moving from 16MB to 8KB. The 8KB limit |
| 171 | // is only used if Enable8KBDefaultHeaderListSize is true; otherwise, the |
| 172 | // old 16MB default is used. User-specified options always take precedence. |
| 173 | maxHeaderListSize := defaultServerMaxHeaderListSize |
| 174 | if envconfig.Enable8KBDefaultHeaderListSize { |
| 175 | maxHeaderListSize = upcomingDefaultHeaderListSize |
| 176 | } |
| 177 | if config.MaxHeaderListSize != nil { |
| 178 | maxHeaderListSize = *config.MaxHeaderListSize |
| 179 | } |
| 180 | framer := newFramer(conn, writeBufSize, readBufSize, config.SharedWriteBuffer, maxHeaderListSize, config.BufferPool) |
| 181 | // Send initial settings as connection preface to client. |
| 182 | isettings := []http2.Setting{{ |
| 183 | ID: http2.SettingMaxFrameSize, |
| 184 | Val: http2MaxFrameLen, |
| 185 | }} |
| 186 | if config.MaxStreams != math.MaxUint32 { |
| 187 | isettings = append(isettings, http2.Setting{ |
| 188 | ID: http2.SettingMaxConcurrentStreams, |
| 189 | Val: config.MaxStreams, |
| 190 | }) |
| 191 | } |
| 192 | iwz := int32(initialWindowSize) |
| 193 | if config.InitialWindowSize >= defaultWindowSize { |
| 194 | iwz = config.InitialWindowSize |
| 195 | } |
| 196 | icwz := int32(initialWindowSize) |
| 197 | if config.InitialConnWindowSize >= defaultWindowSize { |
| 198 | icwz = config.InitialConnWindowSize |
| 199 | } |
| 200 | if iwz != defaultWindowSize { |
| 201 | isettings = append(isettings, http2.Setting{ |
| 202 | ID: http2.SettingInitialWindowSize, |
| 203 | Val: uint32(iwz)}) |
| 204 | } |
| 205 | if config.MaxHeaderListSize != nil { |
| 206 | isettings = append(isettings, http2.Setting{ |
| 207 | ID: http2.SettingMaxHeaderListSize, |
| 208 | Val: *config.MaxHeaderListSize, |