watch.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. default:
  83. // TODO: support cancellation
  84. panic("not implemented")
  85. }
  86. }
  87. }
  88. func (sws *serverWatchStream) sendLoop() {
  89. for {
  90. select {
  91. case evs, ok := <-sws.watchStream.Chan():
  92. if !ok {
  93. return
  94. }
  95. // TODO: evs is []storagepb.Event type
  96. // either return []*storagepb.Event from storage package
  97. // or define protocol buffer with []storagepb.Event.
  98. events := make([]*storagepb.Event, len(evs))
  99. for i := range evs {
  100. events[i] = &evs[i]
  101. }
  102. err := sws.gRPCStream.Send(&pb.WatchResponse{Events: events})
  103. storage.ReportEventReceived()
  104. if err != nil {
  105. return
  106. }
  107. case c, ok := <-sws.ctrlStream:
  108. if !ok {
  109. return
  110. }
  111. if err := sws.gRPCStream.Send(c); err != nil {
  112. return
  113. }
  114. case <-sws.closec:
  115. // drain the chan to clean up pending events
  116. for {
  117. _, ok := <-sws.watchStream.Chan()
  118. if !ok {
  119. return
  120. }
  121. storage.ReportEventReceived()
  122. }
  123. }
  124. }
  125. }
  126. func (sws *serverWatchStream) close() {
  127. sws.watchStream.Close()
  128. close(sws.closec)
  129. close(sws.ctrlStream)
  130. }