streamwatcher.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "fmt"
  16. "io"
  17. "sync"
  18. "k8s.io/klog/v2"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/net"
  21. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  22. )
  23. // Decoder allows StreamWatcher to watch any stream for which a Decoder can be written.
  24. type Decoder interface {
  25. // Decode should return the type of event, the decoded object, or an error.
  26. // An error will cause StreamWatcher to call Close(). Decode should block until
  27. // it has data or an error occurs.
  28. Decode() (action EventType, object runtime.Object, err error)
  29. // Close should close the underlying io.Reader, signalling to the source of
  30. // the stream that it is no longer being watched. Close() must cause any
  31. // outstanding call to Decode() to return with an error of some sort.
  32. Close()
  33. }
  34. // Reporter hides the details of how an error is turned into a runtime.Object for
  35. // reporting on a watch stream since this package may not import a higher level report.
  36. type Reporter interface {
  37. // AsObject must convert err into a valid runtime.Object for the watch stream.
  38. AsObject(err error) runtime.Object
  39. }
  40. // StreamWatcher turns any stream for which you can write a Decoder interface
  41. // into a watch.Interface.
  42. type StreamWatcher struct {
  43. sync.Mutex
  44. source Decoder
  45. reporter Reporter
  46. result chan Event
  47. done chan struct{}
  48. }
  49. // NewStreamWatcher creates a StreamWatcher from the given decoder.
  50. func NewStreamWatcher(d Decoder, r Reporter) *StreamWatcher {
  51. sw := &StreamWatcher{
  52. source: d,
  53. reporter: r,
  54. // It's easy for a consumer to add buffering via an extra
  55. // goroutine/channel, but impossible for them to remove it,
  56. // so nonbuffered is better.
  57. result: make(chan Event),
  58. // If the watcher is externally stopped there is no receiver anymore
  59. // and the send operations on the result channel, especially the
  60. // error reporting might block forever.
  61. // Therefore a dedicated stop channel is used to resolve this blocking.
  62. done: make(chan struct{}),
  63. }
  64. go sw.receive()
  65. return sw
  66. }
  67. // ResultChan implements Interface.
  68. func (sw *StreamWatcher) ResultChan() <-chan Event {
  69. return sw.result
  70. }
  71. // Stop implements Interface.
  72. func (sw *StreamWatcher) Stop() {
  73. // Call Close() exactly once by locking and setting a flag.
  74. sw.Lock()
  75. defer sw.Unlock()
  76. // closing a closed channel always panics, therefore check before closing
  77. select {
  78. case <-sw.done:
  79. default:
  80. close(sw.done)
  81. sw.source.Close()
  82. }
  83. }
  84. // receive reads result from the decoder in a loop and sends down the result channel.
  85. func (sw *StreamWatcher) receive() {
  86. defer utilruntime.HandleCrash()
  87. defer close(sw.result)
  88. defer sw.Stop()
  89. for {
  90. action, obj, err := sw.source.Decode()
  91. if err != nil {
  92. switch err {
  93. case io.EOF:
  94. // watch closed normally
  95. case io.ErrUnexpectedEOF:
  96. klog.V(1).Infof("Unexpected EOF during watch stream event decoding: %v", err)
  97. default:
  98. if net.IsProbableEOF(err) || net.IsTimeout(err) {
  99. klog.V(5).Infof("Unable to decode an event from the watch stream: %v", err)
  100. } else {
  101. select {
  102. case <-sw.done:
  103. case sw.result <- Event{
  104. Type: Error,
  105. Object: sw.reporter.AsObject(fmt.Errorf("unable to decode an event from the watch stream: %v", err)),
  106. }:
  107. }
  108. }
  109. }
  110. return
  111. }
  112. select {
  113. case <-sw.done:
  114. return
  115. case sw.result <- Event{
  116. Type: action,
  117. Object: obj,
  118. }:
  119. }
  120. }
  121. }