streamwatcher.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "io"
  16. "sync"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/net"
  19. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  20. "k8s.io/klog"
  21. )
  22. // Decoder allows StreamWatcher to watch any stream for which a Decoder can be written.
  23. type Decoder interface {
  24. // Decode should return the type of event, the decoded object, or an error.
  25. // An error will cause StreamWatcher to call Close(). Decode should block until
  26. // it has data or an error occurs.
  27. Decode() (action EventType, object runtime.Object, err error)
  28. // Close should close the underlying io.Reader, signalling to the source of
  29. // the stream that it is no longer being watched. Close() must cause any
  30. // outstanding call to Decode() to return with an error of some sort.
  31. Close()
  32. }
  33. // StreamWatcher turns any stream for which you can write a Decoder interface
  34. // into a watch.Interface.
  35. type StreamWatcher struct {
  36. sync.Mutex
  37. source Decoder
  38. result chan Event
  39. stopped bool
  40. }
  41. // NewStreamWatcher creates a StreamWatcher from the given decoder.
  42. func NewStreamWatcher(d Decoder) *StreamWatcher {
  43. sw := &StreamWatcher{
  44. source: d,
  45. // It's easy for a consumer to add buffering via an extra
  46. // goroutine/channel, but impossible for them to remove it,
  47. // so nonbuffered is better.
  48. result: make(chan Event),
  49. }
  50. go sw.receive()
  51. return sw
  52. }
  53. // ResultChan implements Interface.
  54. func (sw *StreamWatcher) ResultChan() <-chan Event {
  55. return sw.result
  56. }
  57. // Stop implements Interface.
  58. func (sw *StreamWatcher) Stop() {
  59. // Call Close() exactly once by locking and setting a flag.
  60. sw.Lock()
  61. defer sw.Unlock()
  62. if !sw.stopped {
  63. sw.stopped = true
  64. sw.source.Close()
  65. }
  66. }
  67. // stopping returns true if Stop() was called previously.
  68. func (sw *StreamWatcher) stopping() bool {
  69. sw.Lock()
  70. defer sw.Unlock()
  71. return sw.stopped
  72. }
  73. // receive reads result from the decoder in a loop and sends down the result channel.
  74. func (sw *StreamWatcher) receive() {
  75. defer close(sw.result)
  76. defer sw.Stop()
  77. defer utilruntime.HandleCrash()
  78. for {
  79. action, obj, err := sw.source.Decode()
  80. if err != nil {
  81. // Ignore expected error.
  82. if sw.stopping() {
  83. return
  84. }
  85. switch err {
  86. case io.EOF:
  87. // watch closed normally
  88. case io.ErrUnexpectedEOF:
  89. klog.V(1).Infof("Unexpected EOF during watch stream event decoding: %v", err)
  90. default:
  91. msg := "Unable to decode an event from the watch stream: %v"
  92. if net.IsProbableEOF(err) {
  93. klog.V(5).Infof(msg, err)
  94. } else {
  95. klog.Errorf(msg, err)
  96. }
  97. }
  98. return
  99. }
  100. sw.result <- Event{
  101. Type: action,
  102. Object: obj,
  103. }
  104. }
  105. }