util.go 772 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package yamux
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Logger is a abstract of *log.Logger
  7. type Logger interface {
  8. Print(v ...interface{})
  9. Printf(format string, v ...interface{})
  10. Println(v ...interface{})
  11. }
  12. var (
  13. timerPool = &sync.Pool{
  14. New: func() interface{} {
  15. timer := time.NewTimer(time.Hour * 1e6)
  16. timer.Stop()
  17. return timer
  18. },
  19. }
  20. )
  21. // asyncSendErr is used to try an async send of an error
  22. func asyncSendErr(ch chan error, err error) {
  23. if ch == nil {
  24. return
  25. }
  26. select {
  27. case ch <- err:
  28. default:
  29. }
  30. }
  31. // asyncNotify is used to signal a waiting goroutine
  32. func asyncNotify(ch chan struct{}) {
  33. select {
  34. case ch <- struct{}{}:
  35. default:
  36. }
  37. }
  38. // min computes the minimum of two values
  39. func min(a, b uint32) uint32 {
  40. if a < b {
  41. return a
  42. }
  43. return b
  44. }