Skip to content

Commit

Permalink
Upgrade smux library to v1.5.16
Browse files Browse the repository at this point in the history
  • Loading branch information
yuting-fan authored and Yangtao-Hua committed May 19, 2022
1 parent 0788d4c commit d7d0847
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 24 deletions.
24 changes: 24 additions & 0 deletions vendor/src/github.com/xtaci/smux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
20 changes: 20 additions & 0 deletions vendor/src/github.com/xtaci/smux/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
arch:
- amd64
- ppc64le
language: go
go:
- 1.9.x
- 1.10.x
- 1.11.x

before_install:
- go get -t -v ./...

install:
- go get github.com/xtaci/smux

script:
- go test -coverprofile=coverage.txt -covermode=atomic -bench .

after_success:
- bash <(curl -s https://codecov.io/bash)
4 changes: 2 additions & 2 deletions vendor/src/github.com/xtaci/smux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<img src="mux.jpg" alt="smux" height="120px" />

[1]: https://godoc.org/github.com/xtaci/smux?status.svg
[2]: https://pkg.go.dev/github.com/xtaci/smux
[2]: https://godoc.org/github.com/xtaci/smux
[3]: https://img.shields.io/badge/license-MIT-blue.svg
[4]: LICENSE
[5]: https://travis-ci.org/xtaci/smux.svg?branch=master
Expand Down Expand Up @@ -34,7 +34,7 @@ Smux ( **S**imple **MU**ltiple**X**ing) is a multiplexing library for Golang. It

## Documentation

For complete documentation, see the associated [Godoc](https://pkg.go.dev/github.com/xtaci/smux).
For complete documentation, see the associated [Godoc](https://godoc.org/github.com/xtaci/smux).

## Benchmark
```
Expand Down
22 changes: 13 additions & 9 deletions vendor/src/github.com/xtaci/smux/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"sync"
)

var defaultAllocator *Allocator
var (
defaultAllocator *Allocator
debruijinPos = [...]byte{0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31}
)

func init() {
defaultAllocator = NewAllocator()
Expand Down Expand Up @@ -57,12 +60,13 @@ func (alloc *Allocator) Put(buf []byte) error {
}

// msb return the pos of most significiant bit
func msb(size int) uint16 {
var pos uint16
size >>= 1
for size > 0 {
size >>= 1
pos++
}
return pos
// http://supertech.csail.mit.edu/papers/debruijn.pdf
func msb(size int) byte {
v := uint32(size)
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
return debruijinPos[(v*0x07C4ACDD)>>27]
}
15 changes: 10 additions & 5 deletions vendor/src/github.com/xtaci/smux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Config struct {
// SMUX Protocol version, support 1,2
Version int

// Disabled keepalive
KeepAliveDisabled bool

// KeepAliveInterval is how often to send a NOP command to the remote
KeepAliveInterval time.Duration

Expand Down Expand Up @@ -54,11 +57,13 @@ func VerifyConfig(config *Config) error {
if !(config.Version == 1 || config.Version == 2) {
return errors.New("unsupported protocol version")
}
if config.KeepAliveInterval == 0 {
return errors.New("keep-alive interval must be positive")
}
if config.KeepAliveTimeout < config.KeepAliveInterval {
return fmt.Errorf("keep-alive timeout must be larger than keep-alive interval")
if !config.KeepAliveDisabled {
if config.KeepAliveInterval == 0 {
return errors.New("keep-alive interval must be positive")
}
if config.KeepAliveTimeout < config.KeepAliveInterval {
return fmt.Errorf("keep-alive timeout must be larger than keep-alive interval")
}
}
if config.MaxFrameSize <= 0 {
return errors.New("max frame size must be positive")
Expand Down
8 changes: 5 additions & 3 deletions vendor/src/github.com/xtaci/smux/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

type writeRequest struct {
prio uint64
prio uint32
frame Frame
result chan writeResult
}
Expand Down Expand Up @@ -104,7 +104,9 @@ func newSession(config *Config, conn io.ReadWriteCloser, client bool) *Session {
go s.shaperLoop()
go s.recvLoop()
go s.sendLoop()
go s.keepalive()
if !config.KeepAliveDisabled {
go s.keepalive()
}
return s
}

Expand Down Expand Up @@ -494,7 +496,7 @@ func (s *Session) writeFrame(f Frame) (n int, err error) {
}

// internal writeFrame version to support deadline used in keepalive
func (s *Session) writeFrameInternal(f Frame, deadline <-chan time.Time, prio uint64) (int, error) {
func (s *Session) writeFrameInternal(f Frame, deadline <-chan time.Time, prio uint32) (int, error) {
req := writeRequest{
prio: prio,
frame: f,
Expand Down
6 changes: 5 additions & 1 deletion vendor/src/github.com/xtaci/smux/shaper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package smux

func _itimediff(later, earlier uint32) int32 {
return (int32)(later - earlier)
}

type shaperHeap []writeRequest

func (h shaperHeap) Len() int { return len(h) }
func (h shaperHeap) Less(i, j int) bool { return h[i].prio < h[j].prio }
func (h shaperHeap) Less(i, j int) bool { return _itimediff(h[j].prio, h[i].prio) > 0 }
func (h shaperHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *shaperHeap) Push(x interface{}) { *h = append(*h, x.(writeRequest)) }

Expand Down
6 changes: 4 additions & 2 deletions vendor/src/github.com/xtaci/smux/shaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ func TestShaper(t *testing.T) {
w2 := writeRequest{prio: 10}
w3 := writeRequest{prio: 20}
w4 := writeRequest{prio: 100}
w5 := writeRequest{prio: (1 << 32) - 1}

var reqs shaperHeap
heap.Push(&reqs, w5)
heap.Push(&reqs, w4)
heap.Push(&reqs, w3)
heap.Push(&reqs, w2)
heap.Push(&reqs, w1)

var lastPrio uint64
var lastPrio = reqs[0].prio
for len(reqs) > 0 {
w := heap.Pop(&reqs).(writeRequest)
if w.prio < lastPrio {
if int32(w.prio-lastPrio) < 0 {
t.Fatal("incorrect shaper priority")
}

Expand Down
10 changes: 8 additions & 2 deletions vendor/src/github.com/xtaci/smux/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (s *Stream) waitRead() error {
case <-s.chReadEvent:
return nil
case <-s.chFinEvent:
// BUG(xtaci): Fix for https://github.com/xtaci/smux/issues/82
s.bufferLock.Lock()
defer s.bufferLock.Unlock()
if len(s.buffers) > 0 {
return nil
}
return io.EOF
case <-s.sess.chSocketReadError:
return s.sess.socketReadError.Load().(error)
Expand Down Expand Up @@ -319,7 +325,7 @@ func (s *Stream) Write(b []byte) (n int, err error) {
}
frame.data = bts[:sz]
bts = bts[sz:]
n, err := s.sess.writeFrameInternal(frame, deadline, uint64(s.numWritten))
n, err := s.sess.writeFrameInternal(frame, deadline, s.numWritten)
s.numWritten++
sent += n
if err != nil {
Expand Down Expand Up @@ -387,7 +393,7 @@ func (s *Stream) writeV2(b []byte) (n int, err error) {
}
frame.data = bts[:sz]
bts = bts[sz:]
n, err := s.sess.writeFrameInternal(frame, deadline, uint64(atomic.LoadUint32(&s.numWritten)))
n, err := s.sess.writeFrameInternal(frame, deadline, atomic.LoadUint32(&s.numWritten))
atomic.AddUint32(&s.numWritten, uint32(sz))
sent += n
if err != nil {
Expand Down

0 comments on commit d7d0847

Please sign in to comment.