clients.go 669 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package discov
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/tal-tech/go-zero/core/discov/internal"
  6. )
  7. const (
  8. indexOfKey = iota
  9. indexOfId
  10. )
  11. const timeToLive int64 = 10
  12. var TimeToLive = timeToLive
  13. func extract(etcdKey string, index int) (string, bool) {
  14. if index < 0 {
  15. return "", false
  16. }
  17. fields := strings.FieldsFunc(etcdKey, func(ch rune) bool {
  18. return ch == internal.Delimiter
  19. })
  20. if index >= len(fields) {
  21. return "", false
  22. }
  23. return fields[index], true
  24. }
  25. func extractId(etcdKey string) (string, bool) {
  26. return extract(etcdKey, indexOfId)
  27. }
  28. func makeEtcdKey(key string, id int64) string {
  29. return fmt.Sprintf("%s%c%d", key, internal.Delimiter, id)
  30. }