watch.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. func (ws *watchServer) Watch(stream pb.Watch_WatchServer) error {
  28. closec := make(chan struct{})
  29. defer close(closec)
  30. watcher := ws.watchable.NewWatcher()
  31. defer watcher.Close()
  32. go sendLoop(stream, watcher, closec)
  33. for {
  34. req, err := stream.Recv()
  35. if err == io.EOF {
  36. return nil
  37. }
  38. if err != nil {
  39. return err
  40. }
  41. var prefix bool
  42. toWatch := req.Key
  43. if len(req.Key) == 0 {
  44. toWatch = req.Prefix
  45. prefix = true
  46. }
  47. // TODO: support cancellation
  48. watcher.Watch(toWatch, prefix, req.StartRevision)
  49. }
  50. }
  51. func sendLoop(stream pb.Watch_WatchServer, watcher storage.Watcher, closec chan struct{}) {
  52. for {
  53. select {
  54. case evs, ok := <-watcher.Chan():
  55. if !ok {
  56. return
  57. }
  58. // TODO: evs is []storagepb.Event type
  59. // either return []*storagepb.Event from storage package
  60. // or define protocol buffer with []storagepb.Event.
  61. events := make([]*storagepb.Event, len(evs))
  62. for i := range evs {
  63. events[i] = &evs[i]
  64. }
  65. err := stream.Send(&pb.WatchResponse{Events: events})
  66. storage.ReportEventReceived()
  67. if err != nil {
  68. return
  69. }
  70. case <-closec:
  71. // drain the chan to clean up pending events
  72. for {
  73. _, ok := <-watcher.Chan()
  74. if !ok {
  75. return
  76. }
  77. storage.ReportEventReceived()
  78. }
  79. }
  80. }
  81. }