Skip to content

Commit

Permalink
feat: webtransport fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tx7do committed Sep 9, 2022
1 parent 6e86e4d commit 0ec6bf2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
8 changes: 4 additions & 4 deletions transport/webtransport/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (c *Client) doAcceptStream(session *Session) {
if err != nil {
log.Error("[webtransport] read data failed: ", err.Error())
}
log.Debug("[webtransport] receive data: ", string(data))
// log.Debug("[webtransport] receive data: ", string(data))
_ = c.messageHandler(data)
}
}
Expand All @@ -221,14 +221,14 @@ func (c *Client) doAcceptUniStream(session *Session) {
for {
acceptStream, err := session.AcceptUniStream(c.ctx)
if err != nil {
log.Debug("[webtransport] accept stream failed: ", err.Error())
log.Debug("[webtransport] accept uni stream failed: ", err.Error())
break
}
data, err := io.ReadAll(acceptStream)
if err != nil {
log.Error("[webtransport] read data failed: ", err.Error())
log.Error("[webtransport] read uni data failed: ", err.Error())
}
log.Debug("[webtransport] receive data: ", string(data))
//log.Debug("[webtransport] receive uni data: ", string(data))
_ = c.messageHandler(data)
}
}
Expand Down
37 changes: 37 additions & 0 deletions transport/webtransport/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webtransport
import (
"crypto/tls"
"github.com/go-kratos/kratos/v2/encoding"
"github.com/lucas-clemente/quic-go"
"time"
)

Expand All @@ -26,6 +27,24 @@ func WithTimeout(timeout time.Duration) ServerOption {
}
}

func WithMaxIdleTimeout(timeout time.Duration) ServerOption {
return func(s *Server) {
if s.Server.QuicConfig == nil {
s.Server.QuicConfig = &quic.Config{}
}
s.Server.QuicConfig.MaxIdleTimeout = timeout
}
}

func WithKeepAlivePeriod(timeout time.Duration) ServerOption {
return func(s *Server) {
if s.Server.QuicConfig == nil {
s.Server.QuicConfig = &quic.Config{}
}
s.Server.QuicConfig.KeepAlivePeriod = timeout
}
}

func WithPath(path string) ServerOption {
return func(s *Server) {
s.path = path
Expand Down Expand Up @@ -65,3 +84,21 @@ func WithClientCodec(c encoding.Codec) ClientOption {
o.codec = c
}
}

func WithClientMaxIdleTimeout(timeout time.Duration) ClientOption {
return func(s *Client) {
if s.transport.QuicConfig == nil {
s.transport.QuicConfig = &quic.Config{}
}
s.transport.QuicConfig.MaxIdleTimeout = timeout
}
}

func WithClientKeepAlivePeriod(timeout time.Duration) ClientOption {
return func(s *Client) {
if s.transport.QuicConfig == nil {
s.transport.QuicConfig = &quic.Config{}
}
s.transport.QuicConfig.KeepAlivePeriod = timeout
}
}
14 changes: 10 additions & 4 deletions transport/webtransport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ func NewServer(opts ...ServerOption) *Server {
}

func (s *Server) init(opts ...ServerOption) {
const idleTimeout = 30 * time.Second

s.Server = &http3.Server{
Addr: ":443",
QuicConfig: &quic.Config{
MaxIdleTimeout: idleTimeout,
KeepAlivePeriod: idleTimeout / 2,
},
}

for _, o := range opts {
Expand Down Expand Up @@ -234,8 +240,8 @@ func (s *Server) addHandler(w http.ResponseWriter, r *http.Request) {
return
}

str := httpStreamer.HTTPStream()
sID := SessionID(str.StreamID())
httpStream := httpStreamer.HTTPStream()
sID := SessionID(httpStream.StreamID())

hijacker, ok := w.(http3.Hijacker)
if !ok { // should never happen, unless quic-go changed the API
Expand Down Expand Up @@ -279,15 +285,15 @@ func (s *Server) doAcceptUniStream(session *Session) {
for {
acceptStream, err := session.AcceptUniStream(s.ctx)
if err != nil {
log.Debug("[webtransport] accept stream failed: ", err.Error())
log.Debug("[webtransport] accept uni stream failed: ", err.Error())
if s.connectHandler != nil {
s.connectHandler(session.SessionID(), false)
}
break
}
data, err := io.ReadAll(acceptStream)
if err != nil {
log.Error("[webtransport] read data failed: ", err.Error())
log.Error("[webtransport] read uni data failed: ", err.Error())
}
//log.Debug("receive data: ", string(data))
_ = s.messageHandler(session.SessionID(), data)
Expand Down

0 comments on commit 0ec6bf2

Please sign in to comment.