@@ -69,7 +69,6 @@ var serverConnectionCounter uint64
69
69
// http2Server implements the ServerTransport interface with HTTP2.
70
70
type http2Server struct {
71
71
lastRead int64 // Keep this field 64-bit aligned. Accessed atomically.
72
- ctx context.Context
73
72
done chan struct {}
74
73
conn net.Conn
75
74
loopy * loopyWriter
@@ -249,7 +248,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
249
248
250
249
done := make (chan struct {})
251
250
t := & http2Server {
252
- ctx : setConnection (context .Background (), rawConn ),
253
251
done : done ,
254
252
conn : conn ,
255
253
remoteAddr : conn .RemoteAddr (),
@@ -272,8 +270,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
272
270
bufferPool : newBufferPool (),
273
271
}
274
272
t .logger = prefixLoggerForServerTransport (t )
275
- // Add peer information to the http2server context.
276
- t .ctx = peer .NewContext (t .ctx , t .getPeer ())
277
273
278
274
t .controlBuf = newControlBuffer (t .done )
279
275
if dynamicWindow {
@@ -282,14 +278,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
282
278
updateFlowControl : t .updateFlowControl ,
283
279
}
284
280
}
285
- for _ , sh := range t .stats {
286
- t .ctx = sh .TagConn (t .ctx , & stats.ConnTagInfo {
287
- RemoteAddr : t .remoteAddr ,
288
- LocalAddr : t .localAddr ,
289
- })
290
- connBegin := & stats.ConnBegin {}
291
- sh .HandleConn (t .ctx , connBegin )
292
- }
293
281
t .channelzID , err = channelz .RegisterNormalSocket (t , config .ChannelzParentID , fmt .Sprintf ("%s -> %s" , t .remoteAddr , t .localAddr ))
294
282
if err != nil {
295
283
return nil , err
@@ -347,7 +335,7 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
347
335
348
336
// operateHeaders takes action on the decoded headers. Returns an error if fatal
349
337
// error encountered and transport needs to close, otherwise returns nil.
350
- func (t * http2Server ) operateHeaders (frame * http2.MetaHeadersFrame , handle func (* Stream )) error {
338
+ func (t * http2Server ) operateHeaders (ctx context. Context , frame * http2.MetaHeadersFrame , handle func (* Stream )) error {
351
339
// Acquire max stream ID lock for entire duration
352
340
t .maxStreamMu .Lock ()
353
341
defer t .maxStreamMu .Unlock ()
@@ -374,10 +362,11 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
374
362
375
363
buf := newRecvBuffer ()
376
364
s := & Stream {
377
- id : streamID ,
378
- st : t ,
379
- buf : buf ,
380
- fc : & inFlow {limit : uint32 (t .initialWindowSize )},
365
+ id : streamID ,
366
+ st : t ,
367
+ buf : buf ,
368
+ fc : & inFlow {limit : uint32 (t .initialWindowSize )},
369
+ headerWireLength : int (frame .Header ().Length ),
381
370
}
382
371
var (
383
372
// if false, content-type was missing or invalid
@@ -516,9 +505,9 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
516
505
s .state = streamReadDone
517
506
}
518
507
if timeoutSet {
519
- s .ctx , s .cancel = context .WithTimeout (t . ctx , timeout )
508
+ s .ctx , s .cancel = context .WithTimeout (ctx , timeout )
520
509
} else {
521
- s .ctx , s .cancel = context .WithCancel (t . ctx )
510
+ s .ctx , s .cancel = context .WithCancel (ctx )
522
511
}
523
512
524
513
// Attach the received metadata to the context.
@@ -597,18 +586,6 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
597
586
s .requestRead = func (n int ) {
598
587
t .adjustWindow (s , uint32 (n ))
599
588
}
600
- for _ , sh := range t .stats {
601
- s .ctx = sh .TagRPC (s .ctx , & stats.RPCTagInfo {FullMethodName : s .method })
602
- inHeader := & stats.InHeader {
603
- FullMethod : s .method ,
604
- RemoteAddr : t .remoteAddr ,
605
- LocalAddr : t .localAddr ,
606
- Compression : s .recvCompress ,
607
- WireLength : int (frame .Header ().Length ),
608
- Header : mdata .Copy (),
609
- }
610
- sh .HandleRPC (s .ctx , inHeader )
611
- }
612
589
s .ctxDone = s .ctx .Done ()
613
590
s .wq = newWriteQuota (defaultWriteQuota , s .ctxDone )
614
591
s .trReader = & transportReader {
@@ -634,7 +611,7 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
634
611
// HandleStreams receives incoming streams using the given handler. This is
635
612
// typically run in a separate goroutine.
636
613
// traceCtx attaches trace to ctx and returns the new context.
637
- func (t * http2Server ) HandleStreams (handle func (* Stream )) {
614
+ func (t * http2Server ) HandleStreams (ctx context. Context , handle func (* Stream )) {
638
615
defer close (t .readerDone )
639
616
for {
640
617
t .controlBuf .throttle ()
@@ -669,7 +646,7 @@ func (t *http2Server) HandleStreams(handle func(*Stream)) {
669
646
}
670
647
switch frame := frame .(type ) {
671
648
case * http2.MetaHeadersFrame :
672
- if err := t .operateHeaders (frame , handle ); err != nil {
649
+ if err := t .operateHeaders (ctx , frame , handle ); err != nil {
673
650
t .Close (err )
674
651
break
675
652
}
@@ -1247,10 +1224,6 @@ func (t *http2Server) Close(err error) {
1247
1224
for _ , s := range streams {
1248
1225
s .cancel ()
1249
1226
}
1250
- for _ , sh := range t .stats {
1251
- connEnd := & stats.ConnEnd {}
1252
- sh .HandleConn (t .ctx , connEnd )
1253
- }
1254
1227
}
1255
1228
1256
1229
// deleteStream deletes the stream s from transport's active streams.
@@ -1316,6 +1289,10 @@ func (t *http2Server) closeStream(s *Stream, rst bool, rstCode http2.ErrCode, eo
1316
1289
})
1317
1290
}
1318
1291
1292
+ func (t * http2Server ) LocalAddr () net.Addr {
1293
+ return t .localAddr
1294
+ }
1295
+
1319
1296
func (t * http2Server ) RemoteAddr () net.Addr {
1320
1297
return t .remoteAddr
1321
1298
}
@@ -1438,7 +1415,8 @@ func (t *http2Server) getOutFlowWindow() int64 {
1438
1415
}
1439
1416
}
1440
1417
1441
- func (t * http2Server ) getPeer () * peer.Peer {
1418
+ // Peer returns the peer of the transport.
1419
+ func (t * http2Server ) Peer () * peer.Peer {
1442
1420
return & peer.Peer {
1443
1421
Addr : t .remoteAddr ,
1444
1422
AuthInfo : t .authInfo , // Can be nil
@@ -1454,18 +1432,3 @@ func getJitter(v time.Duration) time.Duration {
1454
1432
j := grpcrand .Int63n (2 * r ) - r
1455
1433
return time .Duration (j )
1456
1434
}
1457
-
1458
- type connectionKey struct {}
1459
-
1460
- // GetConnection gets the connection from the context.
1461
- func GetConnection (ctx context.Context ) net.Conn {
1462
- conn , _ := ctx .Value (connectionKey {}).(net.Conn )
1463
- return conn
1464
- }
1465
-
1466
- // SetConnection adds the connection to the context to be able to get
1467
- // information about the destination ip and port for an incoming RPC. This also
1468
- // allows any unary or streaming interceptors to see the connection.
1469
- func setConnection (ctx context.Context , conn net.Conn ) context.Context {
1470
- return context .WithValue (ctx , connectionKey {}, conn )
1471
- }
0 commit comments