github.com/hashicorp/yamux源码解析

James Phillips a785d62dc2 Adds mitigation for the ping of death. %!s(int64=9) %!d(string=hai) anos
.gitignore 40ab2c2f0a Initial commit %!s(int64=10) %!d(string=hai) anos
LICENSE 40ab2c2f0a Initial commit %!s(int64=10) %!d(string=hai) anos
README.md 576a45c561 README cleanup %!s(int64=10) %!d(string=hai) anos
addr.go 83a32c00ea Moving the addr stuff to a single file %!s(int64=10) %!d(string=hai) anos
bench_test.go 2ed5ac90b3 Adding basic benchmarks %!s(int64=10) %!d(string=hai) anos
const.go 612eb1754a Adds header write timeout to prevent deadlocking reads on window updates. %!s(int64=9) %!d(string=hai) anos
const_test.go 3b131e6dd7 Removing compression %!s(int64=10) %!d(string=hai) anos
mux.go 612eb1754a Adds header write timeout to prevent deadlocking reads on window updates. %!s(int64=9) %!d(string=hai) anos
session.go a785d62dc2 Adds mitigation for the ping of death. %!s(int64=9) %!d(string=hai) anos
session_test.go a785d62dc2 Adds mitigation for the ping of death. %!s(int64=9) %!d(string=hai) anos
spec.md d8a2feb541 README cleanup %!s(int64=10) %!d(string=hai) anos
stream.go b2e55852dd Adding backpressure to Open to avoid RST %!s(int64=10) %!d(string=hai) anos
util.go eb6b77436e Adding utility methods %!s(int64=10) %!d(string=hai) anos
util_test.go eb6b77436e Adding utility methods %!s(int64=10) %!d(string=hai) anos

README.md

Yamux

Yamux (Yet another Multiplexer) is a multiplexing library for Golang. It relies on an underlying connection to provide reliability and ordering, such as TCP or Unix domain sockets, and provides stream-oriented multiplexing. It is inspired by SPDY but is not interoperable with it.

Yamux features include:

  • Bi-directional streams
    • Streams can be opened by either client or server
    • Useful for NAT traversal
    • Server-side push support
  • Flow control
    • Avoid starvation
    • Back-pressure to prevent overwhelming a receiver
  • Keep Alives
    • Enables persistent connections over a load balancer
  • Efficient
    • Enables thousands of logical streams with low overhead

Documentation

For complete documentation, see the associated Godoc.

Specification

The full specification for Yamux is provided in the spec.md file. It can be used as a guide to implementors of interoperable libraries.

Usage

Using Yamux is remarkably simple:


func client() {
    // Get a TCP connection
    conn, err := net.Dial(...)
    if err != nil {
        panic(err)
    }

    // Setup client side of yamux
    session, err := yamux.Client(conn, nil)
    if err != nil {
        panic(err)
    }

    // Open a new stream
    stream, err := session.Open()
    if err != nil {
        panic(err)
    }

    // Stream implements net.Conn
    stream.Write([]byte("ping"))
}

func server() {
    // Accept a TCP connection
    conn, err := listener.Accept()
    if err != nil {
        panic(err)
    }

    // Setup server side of yamux
    session, err := yamux.Server(conn, nil)
    if err != nil {
        panic(err)
    }

    // Accept a stream
    stream, err := session.Accept()
    if err != nil {
        panic(err)
    }

    // Listen for a message
    buf := make([]byte, 4)
    stream.Read(buf)
}