mux.go 3.7 KB

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