watch.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v3rpc
  15. import (
  16. "io"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/storage"
  19. "github.com/coreos/etcd/storage/storagepb"
  20. )
  21. type watchServer struct {
  22. watchable storage.Watchable
  23. }
  24. func NewWatchServer(w storage.Watchable) pb.WatchServer {
  25. return &watchServer{w}
  26. }
  27. const (
  28. // We send ctrl response inside the read loop. We do not want
  29. // send to block read, but we still want ctrl response we sent to
  30. // be serialized. Thus we use a buffered chan to solve the problem.
  31. // A small buffer should be OK for most cases, since we expect the
  32. // ctrl requests are infrequent.
  33. ctrlStreamBufLen = 16
  34. )
  35. // serverWatchStream is an etcd server side stream. It receives requests
  36. // from client side gRPC stream. It receives watch events from storage.WatchStream,
  37. // and creates responses that forwarded to gRPC stream.
  38. // It also forwards control message like watch created and canceled.
  39. type serverWatchStream struct {
  40. gRPCStream pb.Watch_WatchServer
  41. watchStream storage.WatchStream
  42. ctrlStream chan *pb.WatchResponse
  43. // closec indicates the stream is closed.
  44. closec chan struct{}
  45. }
  46. func (ws *watchServer) Watch(stream pb.Watch_WatchServer) error {
  47. sws := serverWatchStream{
  48. gRPCStream: stream,
  49. watchStream: ws.watchable.NewWatchStream(),
  50. // chan for sending control response like watcher created and canceled.
  51. ctrlStream: make(chan *pb.WatchResponse, ctrlStreamBufLen),
  52. closec: make(chan struct{}),
  53. }
  54. defer sws.close()
  55. go sws.sendLoop()
  56. return sws.recvLoop()
  57. }
  58. func (sws *serverWatchStream) recvLoop() error {
  59. for {
  60. req, err := sws.gRPCStream.Recv()
  61. if err == io.EOF {
  62. return nil
  63. }
  64. if err != nil {
  65. return err
  66. }
  67. switch {
  68. case req.CreateRequest != nil:
  69. creq := req.CreateRequest
  70. var prefix bool
  71. toWatch := creq.Key
  72. if len(creq.Key) == 0 {
  73. toWatch = creq.Prefix
  74. prefix = true
  75. }
  76. id, _ := sws.watchStream.Watch(toWatch, prefix, creq.StartRevision)
  77. sws.ctrlStream <- &pb.WatchResponse{
  78. // TODO: fill in response header.
  79. WatchId: id,
  80. Created: true,
  81. }
  82. case req.CancelRequest != nil:
  83. id := req.CancelRequest.WatchId
  84. err := sws.watchStream.Cancel(id)
  85. if err == nil {
  86. sws.ctrlStream <- &pb.WatchResponse{
  87. // TODO: fill in response header.
  88. WatchId: id,
  89. Canceled: true,
  90. }
  91. }
  92. // TODO: do we need to return error back to client?
  93. default:
  94. panic("not implemented")
  95. }
  96. }
  97. }
  98. func (sws *serverWatchStream) sendLoop() {
  99. for {
  100. select {
  101. case evs, ok := <-sws.watchStream.Chan():
  102. if !ok {
  103. return
  104. }
  105. // TODO: evs is []storagepb.Event type
  106. // either return []*storagepb.Event from storage package
  107. // or define protocol buffer with []storagepb.Event.
  108. events := make([]*storagepb.Event, len(evs))
  109. for i := range evs {
  110. events[i] = &evs[i]
  111. }
  112. err := sws.gRPCStream.Send(&pb.WatchResponse{Events: events})
  113. storage.ReportEventReceived()
  114. if err != nil {
  115. return
  116. }
  117. case c, ok := <-sws.ctrlStream:
  118. if !ok {
  119. return
  120. }
  121. if err := sws.gRPCStream.Send(c); err != nil {
  122. return
  123. }
  124. case <-sws.closec:
  125. // drain the chan to clean up pending events
  126. for {
  127. _, ok := <-sws.watchStream.Chan()
  128. if !ok {
  129. return
  130. }
  131. storage.ReportEventReceived()
  132. }
  133. }
  134. }
  135. }
  136. func (sws *serverWatchStream) close() {
  137. sws.watchStream.Close()
  138. close(sws.closec)
  139. close(sws.ctrlStream)
  140. }