11package core
22
33import (
4+ "context"
45 "crypto/tls"
6+ "fmt"
7+ "log"
8+ "os"
9+ "strings"
510 "time"
611
712 "github.com/quic-go/quic-go"
13+ "github.com/quic-go/quic-go/logging"
14+ "github.com/quic-go/quic-go/qlog"
815 "github.com/yomorun/yomo/core/auth"
916 "github.com/yomorun/yomo/core/frame"
1017 "github.com/yomorun/yomo/core/ylog"
@@ -28,27 +35,26 @@ type clientOptions struct {
2835 tracerProvider trace.TracerProvider
2936}
3037
31- func defaultClientOption () * clientOptions {
32- logger := ylog .Default ()
33-
34- defaultQuicConfig := & quic.Config {
35- Versions : []quic.VersionNumber {quic .Version1 , quic .Version2 },
36- MaxIdleTimeout : time .Second * 40 ,
37- KeepAlivePeriod : time .Second * 20 ,
38- MaxIncomingStreams : 1000 ,
39- MaxIncomingUniStreams : 1000 ,
40- HandshakeIdleTimeout : time .Second * 3 ,
41- InitialStreamReceiveWindow : 1024 * 1024 * 2 ,
42- InitialConnectionReceiveWindow : 1024 * 1024 * 2 ,
43- TokenStore : quic .NewLRUTokenStore (10 , 5 ),
44- }
38+ // DefaultClientQuicConfig be used when the `quicConfig` of client is nil.
39+ var DefaultClientQuicConfig = & quic.Config {
40+ Versions : []quic.VersionNumber {quic .Version1 , quic .Version2 },
41+ MaxIdleTimeout : time .Second * 40 ,
42+ KeepAlivePeriod : time .Second * 20 ,
43+ MaxIncomingStreams : 1000 ,
44+ MaxIncomingUniStreams : 1000 ,
45+ HandshakeIdleTimeout : time .Second * 3 ,
46+ InitialStreamReceiveWindow : 1024 * 1024 * 2 ,
47+ InitialConnectionReceiveWindow : 1024 * 1024 * 2 ,
48+ TokenStore : quic .NewLRUTokenStore (10 , 5 ),
49+ }
4550
51+ func defaultClientOption () * clientOptions {
4652 opts := & clientOptions {
4753 observeDataTags : make ([]frame.Tag , 0 ),
48- quicConfig : defaultQuicConfig ,
54+ quicConfig : DefaultClientQuicConfig ,
4955 tlsConfig : pkgtls .MustCreateClientTLSConfig (),
5056 credential : auth .NewCredential ("" ),
51- logger : logger ,
57+ logger : ylog . Default () ,
5258 }
5359
5460 return opts
@@ -104,3 +110,22 @@ func WithTracerProvider(tp trace.TracerProvider) ClientOption {
104110 o .tracerProvider = tp
105111 }
106112}
113+
114+ // qlog helps developers to debug quic protocol.
115+ // See more: https://github.com/quic-go/quic-go?tab=readme-ov-file#quic-event-logging-using-qlog
116+ func qlogTraceEnabled () bool {
117+ return strings .ToLower (os .Getenv ("YOMO_QLOG_TRACE" )) == "true"
118+ }
119+
120+ func qlogTracer (ctx context.Context , p logging.Perspective , connID quic.ConnectionID ) * logging.ConnectionTracer {
121+ role := "server"
122+ if p == logging .PerspectiveClient {
123+ role = "client"
124+ }
125+ filename := fmt .Sprintf ("./log_%s_%s.qlog" , connID , role )
126+ f , err := os .Create (filename )
127+ if err != nil {
128+ log .Fatalf ("qlog trace error: %s\n " , err )
129+ }
130+ return qlog .NewConnectionTracer (f , p , connID )
131+ }
0 commit comments