direct.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package direct
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/library/conf/env"
  7. "go-common/library/naming"
  8. "go-common/library/net/rpc/warden/resolver"
  9. )
  10. const (
  11. // Name is the name of direct resolver
  12. Name = "direct"
  13. )
  14. var _ naming.Resolver = &Direct{}
  15. // New return Direct
  16. func New() *Direct {
  17. return &Direct{}
  18. }
  19. // Build build direct.
  20. func Build(id string) *Direct {
  21. return &Direct{id: id}
  22. }
  23. // Direct is a resolver for conneting endpoints directly.
  24. // example format: direct://default/192.168.1.1:8080,192.168.1.2:8081
  25. type Direct struct {
  26. id string
  27. }
  28. // Build direct build.
  29. func (d *Direct) Build(id string) naming.Resolver {
  30. return &Direct{id: id}
  31. }
  32. // Scheme return the Scheme of Direct
  33. func (d *Direct) Scheme() string {
  34. return Name
  35. }
  36. // Watch a tree
  37. func (d *Direct) Watch() <-chan struct{} {
  38. ch := make(chan struct{}, 1)
  39. ch <- struct{}{}
  40. return ch
  41. }
  42. //Unwatch a tree
  43. func (d *Direct) Unwatch(id string) {
  44. }
  45. //Fetch fetch isntances
  46. func (d *Direct) Fetch(ctx context.Context) (insMap map[string][]*naming.Instance, found bool) {
  47. var ins []*naming.Instance
  48. addrs := strings.Split(d.id, ",")
  49. for _, addr := range addrs {
  50. ins = append(ins, &naming.Instance{
  51. Addrs: []string{fmt.Sprintf("%s://%s", resolver.Scheme, addr)},
  52. })
  53. }
  54. if len(ins) > 0 {
  55. found = true
  56. }
  57. insMap = map[string][]*naming.Instance{env.Zone: ins}
  58. return
  59. }
  60. //Close close Direct
  61. func (d *Direct) Close() error {
  62. return nil
  63. }