| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Package discovery implements RPC service introspection. |
| 6 package discovery |
| 7 |
| 8 import ( |
| 9 "crypto/sha1" |
| 10 "fmt" |
| 11 |
| 12 "github.com/golang/protobuf/proto" |
| 13 "golang.org/x/net/context" |
| 14 |
| 15 "github.com/luci/luci-go/common/proto/google/descriptor" |
| 16 "github.com/luci/luci-go/server/prpc" |
| 17 ) |
| 18 |
| 19 // New creates a discovery server for all the given services. |
| 20 // The service descriptions must be registered using |
| 21 // RegisterDescriptorSetCompressed which is called by init() function |
| 22 // generated by github.com/luci/luci-go/tools/cmd/cproto. |
| 23 func New(serviceNames ...string) (DiscoveryServer, error) { |
| 24 desc, err := combineDescriptors(serviceNames) |
| 25 if err != nil { |
| 26 return nil, err |
| 27 } |
| 28 descBytes, err := proto.Marshal(desc) |
| 29 if err != nil { |
| 30 return nil, err |
| 31 } |
| 32 |
| 33 cpy := make([]string, len(serviceNames)) |
| 34 copy(cpy, serviceNames) |
| 35 return &service{cpy, descBytes}, nil |
| 36 } |
| 37 |
| 38 // Enable registers a discovery service on the server. |
| 39 // It makes all currently registered services and the discovery service |
| 40 // discoverable. |
| 41 func Enable(server *prpc.Server) { |
| 42 serviceNames := append(server.ServiceNames(), "discovery.Discovery") |
| 43 service, err := New(serviceNames...) |
| 44 if err != nil { |
| 45 panic(err) |
| 46 } |
| 47 RegisterDiscoveryServer(server, service) |
| 48 } |
| 49 |
| 50 type service struct { |
| 51 serviceNames []string |
| 52 fileDescriptionSet []byte |
| 53 } |
| 54 |
| 55 func (s *service) Describe(c context.Context, _ *Void) (*DescribeResponse, error
) { |
| 56 return &DescribeResponse{ |
| 57 FileDescriptionSet: s.fileDescriptionSet, |
| 58 Services: s.serviceNames, |
| 59 }, nil |
| 60 } |
| 61 |
| 62 // combineDescriptors creates one FileDescriptorSet that covers all services |
| 63 // and their dependencies. |
| 64 func combineDescriptors(serviceNames []string) (*descriptor.FileDescriptorSet, e
rror) { |
| 65 result := &descriptor.FileDescriptorSet{} |
| 66 // seenFiles is a set of descriptor files keyed by SHA1 of their content
s.. |
| 67 seenFiles := map[[sha1.Size]byte]bool{} |
| 68 |
| 69 for _, s := range serviceNames { |
| 70 desc, err := GetDescriptorSet(s) |
| 71 if err != nil { |
| 72 return nil, fmt.Errorf("service %s: %s", s, err) |
| 73 } |
| 74 if desc == nil { |
| 75 return nil, fmt.Errorf( |
| 76 "descriptor for service %q is not found. "+ |
| 77 "Did you compile it with github.com/luci
/luci-go/tools/cmd/cproto?", |
| 78 s) |
| 79 } |
| 80 for _, f := range desc.GetFile() { |
| 81 binary, err := proto.Marshal(f) |
| 82 if err != nil { |
| 83 return nil, fmt.Errorf("could not marshal descri
ption of %s", f.GetName()) |
| 84 } |
| 85 hash := sha1.Sum(binary) |
| 86 if !seenFiles[hash] { |
| 87 result.File = append(result.File, f) |
| 88 seenFiles[hash] = true |
| 89 } |
| 90 |
| 91 } |
| 92 } |
| 93 return result, nil |
| 94 } |
| OLD | NEW |