@@ -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
@@ -244,7 +243,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
244
243
245
244
done := make (chan struct {})
246
245
t := & http2Server {
247
- ctx : setConnection (context .Background (), rawConn ),
248
246
done : done ,
249
247
conn : conn ,
250
248
remoteAddr : conn .RemoteAddr (),
@@ -267,8 +265,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
267
265
bufferPool : newBufferPool (),
268
266
}
269
267
t .logger = prefixLoggerForServerTransport (t )
270
- // Add peer information to the http2server context.
271
- t .ctx = peer .NewContext (t .ctx , t .getPeer ())
272
268
273
269
t .controlBuf = newControlBuffer (t .done )
274
270
if dynamicWindow {
@@ -277,14 +273,6 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
277
273
updateFlowControl : t .updateFlowControl ,
278
274
}
279
275
}
280
- for _ , sh := range t .stats {
281
- t .ctx = sh .TagConn (t .ctx , & stats.ConnTagInfo {
282
- RemoteAddr : t .remoteAddr ,
283
- LocalAddr : t .localAddr ,
284
- })
285
- connBegin := & stats.ConnBegin {}
286
- sh .HandleConn (t .ctx , connBegin )
287
- }
288
276
t .channelzID , err = channelz .RegisterNormalSocket (t , config .ChannelzParentID , fmt .Sprintf ("%s -> %s" , t .remoteAddr , t .localAddr ))
289
277
if err != nil {
290
278
return nil , err
@@ -342,7 +330,7 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
342
330
343
331
// operateHeaders takes action on the decoded headers. Returns an error if fatal
344
332
// error encountered and transport needs to close, otherwise returns nil.
345
- func (t * http2Server ) operateHeaders (frame * http2.MetaHeadersFrame , handle func (* Stream )) error {
333
+ func (t * http2Server ) operateHeaders (ctx context. Context , frame * http2.MetaHeadersFrame , handle func (* Stream )) error {
346
334
// Acquire max stream ID lock for entire duration
347
335
t .maxStreamMu .Lock ()
348
336
defer t .maxStreamMu .Unlock ()
@@ -369,10 +357,11 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
369
357
370
358
buf := newRecvBuffer ()
371
359
s := & Stream {
372
- id : streamID ,
373
- st : t ,
374
- buf : buf ,
375
- fc : & inFlow {limit : uint32 (t .initialWindowSize )},
360
+ id : streamID ,
361
+ st : t ,
362
+ buf : buf ,
363
+ fc : & inFlow {limit : uint32 (t .initialWindowSize )},
364
+ headerWireLength : int (frame .Header ().Length ),
376
365
}
377
366
var (
378
367
// if false, content-type was missing or invalid
@@ -511,9 +500,9 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
511
500
s .state = streamReadDone
512
501
}
513
502
if timeoutSet {
514
- s .ctx , s .cancel = context .WithTimeout (t . ctx , timeout )
503
+ s .ctx , s .cancel = context .WithTimeout (ctx , timeout )
515
504
} else {
516
- s .ctx , s .cancel = context .WithCancel (t . ctx )
505
+ s .ctx , s .cancel = context .WithCancel (ctx )
517
506
}
518
507
519
508
// Attach the received metadata to the context.
@@ -592,18 +581,6 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
592
581
s .requestRead = func (n int ) {
593
582
t .adjustWindow (s , uint32 (n ))
594
583
}
595
- for _ , sh := range t .stats {
596
- s .ctx = sh .TagRPC (s .ctx , & stats.RPCTagInfo {FullMethodName : s .method })
597
- inHeader := & stats.InHeader {
598
- FullMethod : s .method ,
599
- RemoteAddr : t .remoteAddr ,
600
- LocalAddr : t .localAddr ,
601
- Compression : s .recvCompress ,
602
- WireLength : int (frame .Header ().Length ),
603
- Header : mdata .Copy (),
604
- }
605
- sh .HandleRPC (s .ctx , inHeader )
606
- }
607
584
s .ctxDone = s .ctx .Done ()
608
585
s .wq = newWriteQuota (defaultWriteQuota , s .ctxDone )
609
586
s .trReader = & transportReader {
@@ -629,7 +606,7 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
629
606
// HandleStreams receives incoming streams using the given handler. This is
630
607
// typically run in a separate goroutine.
631
608
// traceCtx attaches trace to ctx and returns the new context.
632
- func (t * http2Server ) HandleStreams (handle func (* Stream )) {
609
+ func (t * http2Server ) HandleStreams (ctx context. Context , handle func (* Stream )) {
633
610
defer close (t .readerDone )
634
611
for {
635
612
t .controlBuf .throttle ()
@@ -664,7 +641,7 @@ func (t *http2Server) HandleStreams(handle func(*Stream)) {
664
641
}
665
642
switch frame := frame .(type ) {
666
643
case * http2.MetaHeadersFrame :
667
- if err := t .operateHeaders (frame , handle ); err != nil {
644
+ if err := t .operateHeaders (ctx , frame , handle ); err != nil {
668
645
t .Close (err )
669
646
break
670
647
}
@@ -1242,10 +1219,6 @@ func (t *http2Server) Close(err error) {
1242
1219
for _ , s := range streams {
1243
1220
s .cancel ()
1244
1221
}
1245
- for _ , sh := range t .stats {
1246
- connEnd := & stats.ConnEnd {}
1247
- sh .HandleConn (t .ctx , connEnd )
1248
- }
1249
1222
}
1250
1223
1251
1224
// deleteStream deletes the stream s from transport's active streams.
@@ -1311,6 +1284,10 @@ func (t *http2Server) closeStream(s *Stream, rst bool, rstCode http2.ErrCode, eo
1311
1284
})
1312
1285
}
1313
1286
1287
+ func (t * http2Server ) LocalAddr () net.Addr {
1288
+ return t .localAddr
1289
+ }
1290
+
1314
1291
func (t * http2Server ) RemoteAddr () net.Addr {
1315
1292
return t .remoteAddr
1316
1293
}
@@ -1433,7 +1410,8 @@ func (t *http2Server) getOutFlowWindow() int64 {
1433
1410
}
1434
1411
}
1435
1412
1436
- func (t * http2Server ) getPeer () * peer.Peer {
1413
+ // Peer returns the peer of the transport.
1414
+ func (t * http2Server ) Peer () * peer.Peer {
1437
1415
return & peer.Peer {
1438
1416
Addr : t .remoteAddr ,
1439
1417
AuthInfo : t .authInfo , // Can be nil
@@ -1449,18 +1427,3 @@ func getJitter(v time.Duration) time.Duration {
1449
1427
j := grpcrand .Int63n (2 * r ) - r
1450
1428
return time .Duration (j )
1451
1429
}
1452
-
1453
- type connectionKey struct {}
1454
-
1455
- // GetConnection gets the connection from the context.
1456
- func GetConnection (ctx context.Context ) net.Conn {
1457
- conn , _ := ctx .Value (connectionKey {}).(net.Conn )
1458
- return conn
1459
- }
1460
-
1461
- // SetConnection adds the connection to the context to be able to get
1462
- // information about the destination ip and port for an incoming RPC. This also
1463
- // allows any unary or streaming interceptors to see the connection.
1464
- func setConnection (ctx context.Context , conn net.Conn ) context.Context {
1465
- return context .WithValue (ctx , connectionKey {}, conn )
1466
- }
0 commit comments