go-zero/core/discov/clients.go

42 lines
702 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package discov
import (
"fmt"
"strings"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/discov/internal"
2020-07-26 17:09:05 +08:00
)
const (
2020-10-16 10:50:43 +08:00
_ = iota
2020-07-26 17:09:05 +08:00
indexOfId
)
const timeToLive int64 = 10
// TimeToLive is seconds to live in etcd.
2020-07-26 17:09:05 +08:00
var TimeToLive = timeToLive
func extract(etcdKey string, index int) (string, bool) {
if index < 0 {
return "", false
}
fields := strings.FieldsFunc(etcdKey, func(ch rune) bool {
return ch == internal.Delimiter
})
if index >= len(fields) {
return "", false
}
return fields[index], true
}
func extractId(etcdKey string) (string, bool) {
return extract(etcdKey, indexOfId)
}
func makeEtcdKey(key string, id int64) string {
return fmt.Sprintf("%s%c%d", key, internal.Delimiter, id)
}