streamerrormap.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package h2mux
  2. import (
  3. "sync"
  4. "golang.org/x/net/http2"
  5. )
  6. // StreamErrorMap is used to track stream errors. This is a separate structure to ActiveStreamMap because
  7. // errors can be raised against non-existent or closed streams.
  8. type StreamErrorMap struct {
  9. sync.RWMutex
  10. // errors tracks per-stream errors
  11. errors map[uint32]http2.ErrCode
  12. // hasError is signaled whenever an error is raised.
  13. hasError Signal
  14. }
  15. // NewStreamErrorMap creates a new StreamErrorMap.
  16. func NewStreamErrorMap() *StreamErrorMap {
  17. return &StreamErrorMap{
  18. errors: make(map[uint32]http2.ErrCode),
  19. hasError: NewSignal(),
  20. }
  21. }
  22. // RaiseError raises a stream error.
  23. func (s *StreamErrorMap) RaiseError(streamID uint32, err http2.ErrCode) {
  24. s.Lock()
  25. s.errors[streamID] = err
  26. s.Unlock()
  27. s.hasError.Signal()
  28. }
  29. // GetSignalChan returns a channel that is signalled when an error is raised.
  30. func (s *StreamErrorMap) GetSignalChan() <-chan struct{} {
  31. return s.hasError.WaitChannel()
  32. }
  33. // GetErrors retrieves all errors currently raised. This resets the currently-tracked errors.
  34. func (s *StreamErrorMap) GetErrors() map[uint32]http2.ErrCode {
  35. s.Lock()
  36. errors := s.errors
  37. s.errors = make(map[uint32]http2.ErrCode)
  38. s.Unlock()
  39. return errors
  40. }