grpc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2016 The etcd Authors
  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 naming
  15. import (
  16. "encoding/json"
  17. "time"
  18. "github.com/coreos/etcd/clientv3"
  19. "github.com/coreos/etcd/mvcc/mvccpb"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc/naming"
  22. )
  23. const (
  24. gRPCNamingPrefix = "/github.com/grpc/"
  25. )
  26. // GRPCResolver creates a grpc.Watcher for a target to track its resolution changes.
  27. type GRPCResolver struct {
  28. // Client is an initialized etcd client
  29. Client *clientv3.Client
  30. // Timeout for update/delete request.
  31. Timeout time.Duration
  32. }
  33. func (gr *GRPCResolver) Add(target string, addr string, metadata interface{}) error {
  34. update := naming.Update{
  35. Addr: addr,
  36. Metadata: metadata,
  37. }
  38. val, err := json.Marshal(update)
  39. if err != nil {
  40. return err
  41. }
  42. ctx := context.Background()
  43. if gr.Timeout != 0 {
  44. var cancel context.CancelFunc
  45. ctx, cancel = context.WithTimeout(context.Background(), gr.Timeout)
  46. defer cancel()
  47. }
  48. _, err = gr.Client.KV.Put(ctx, gRPCNamingPrefix+target, string(val))
  49. return err
  50. }
  51. func (gr *GRPCResolver) Delete(target string) error {
  52. ctx := context.Background()
  53. if gr.Timeout != 0 {
  54. var cancel context.CancelFunc
  55. ctx, cancel = context.WithTimeout(context.Background(), gr.Timeout)
  56. defer cancel()
  57. }
  58. _, err := gr.Client.Delete(ctx, gRPCNamingPrefix+target)
  59. return err
  60. }
  61. func (gr *GRPCResolver) Resolve(target string) (naming.Watcher, error) {
  62. cctx, cancel := context.WithCancel(context.Background())
  63. wch := gr.Client.Watch(cctx, gRPCNamingPrefix+target)
  64. w := &gRPCWatcher{
  65. cancel: cancel,
  66. wch: wch,
  67. }
  68. return w, nil
  69. }
  70. type gRPCWatcher struct {
  71. cancel context.CancelFunc
  72. wch clientv3.WatchChan
  73. }
  74. func (gw *gRPCWatcher) Next() ([]*naming.Update, error) {
  75. wr, ok := <-gw.wch
  76. if !ok {
  77. return nil, wr.Err()
  78. }
  79. updates := make([]*naming.Update, 0, len(wr.Events))
  80. for _, e := range wr.Events {
  81. switch e.Type {
  82. case mvccpb.PUT:
  83. var jupdate naming.Update
  84. err := json.Unmarshal(e.Kv.Value, &jupdate)
  85. if err != nil {
  86. continue
  87. }
  88. updates = append(updates, &jupdate)
  89. case mvccpb.DELETE:
  90. updates = append(updates, &naming.Update{Op: naming.Delete})
  91. }
  92. }
  93. return updates, nil
  94. }
  95. func (gw *gRPCWatcher) Close() { gw.cancel() }