mux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package yamux
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. "time"
  8. )
  9. // Config is used to tune the Yamux session
  10. type Config struct {
  11. // AcceptBacklog is used to limit how many streams may be
  12. // waiting an accept.
  13. AcceptBacklog int
  14. // EnableKeepalive is used to do a period keep alive
  15. // messages using a ping.
  16. EnableKeepAlive bool
  17. // KeepAliveInterval is how often to perform the keep alive
  18. KeepAliveInterval time.Duration
  19. // ConnectionWriteTimeout is meant to be a "safety valve" timeout after
  20. // we which will suspect a problem with the underlying connection and
  21. // close it. This is only applied to writes, where's there's generally
  22. // an expectation that things will move along quickly.
  23. ConnectionWriteTimeout time.Duration
  24. // MaxStreamWindowSize is used to control the maximum
  25. // window size that we allow for a stream.
  26. MaxStreamWindowSize uint32
  27. // StreamOpenTimeout is the maximum amount of time that a stream will
  28. // be allowed to remain in pending state while waiting for an ack from the peer.
  29. // Once the timeout is reached the session will be gracefully closed.
  30. // A zero value disables the StreamOpenTimeout allowing unbounded
  31. // blocking on OpenStream calls.
  32. StreamOpenTimeout time.Duration
  33. // StreamCloseTimeout is the maximum time that a stream will allowed to
  34. // be in a half-closed state when `Close` is called before forcibly
  35. // closing the connection. Forcibly closed connections will empty the
  36. // receive buffer, drop any future packets received for that stream,
  37. // and send a RST to the remote side.
  38. StreamCloseTimeout time.Duration
  39. // LogOutput is used to control the log destination. Either Logger or
  40. // LogOutput can be set, not both.
  41. LogOutput io.Writer
  42. // Logger is used to pass in the logger to be used. Either Logger or
  43. // LogOutput can be set, not both.
  44. Logger *log.Logger
  45. }
  46. // DefaultConfig is used to return a default configuration
  47. func DefaultConfig() *Config {
  48. return &Config{
  49. AcceptBacklog: 256,
  50. EnableKeepAlive: true,
  51. KeepAliveInterval: 30 * time.Second,
  52. ConnectionWriteTimeout: 10 * time.Second,
  53. MaxStreamWindowSize: initialStreamWindow,
  54. StreamCloseTimeout: 5 * time.Minute,
  55. StreamOpenTimeout: 75 * time.Second,
  56. LogOutput: os.Stderr,
  57. }
  58. }
  59. // VerifyConfig is used to verify the sanity of configuration
  60. func VerifyConfig(config *Config) error {
  61. if config.AcceptBacklog <= 0 {
  62. return fmt.Errorf("backlog must be positive")
  63. }
  64. if config.KeepAliveInterval == 0 {
  65. return fmt.Errorf("keep-alive interval must be positive")
  66. }
  67. if config.MaxStreamWindowSize < initialStreamWindow {
  68. return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
  69. }
  70. if config.LogOutput != nil && config.Logger != nil {
  71. return fmt.Errorf("both Logger and LogOutput may not be set, select one")
  72. } else if config.LogOutput == nil && config.Logger == nil {
  73. return fmt.Errorf("one of Logger or LogOutput must be set, select one")
  74. }
  75. return nil
  76. }
  77. // Server is used to initialize a new server-side connection.
  78. // There must be at most one server-side connection. If a nil config is
  79. // provided, the DefaultConfiguration will be used.
  80. func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  81. if config == nil {
  82. config = DefaultConfig()
  83. }
  84. if err := VerifyConfig(config); err != nil {
  85. return nil, err
  86. }
  87. return newSession(config, conn, false), nil
  88. }
  89. // Client is used to initialize a new client-side connection.
  90. // There must be at most one client-side connection.
  91. func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  92. if config == nil {
  93. config = DefaultConfig()
  94. }
  95. if err := VerifyConfig(config); err != nil {
  96. return nil, err
  97. }
  98. return newSession(config, conn, true), nil
  99. }