github.com/lingyao2333/mo-zero@v1.4.1/core/discov/clients.go (about)

     1  package discov
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/lingyao2333/mo-zero/core/discov/internal"
     8  )
     9  
    10  const (
    11  	_ = iota
    12  	indexOfId
    13  )
    14  
    15  const timeToLive int64 = 10
    16  
    17  // TimeToLive is seconds to live in etcd.
    18  var TimeToLive = timeToLive
    19  
    20  func extract(etcdKey string, index int) (string, bool) {
    21  	if index < 0 {
    22  		return "", false
    23  	}
    24  
    25  	fields := strings.FieldsFunc(etcdKey, func(ch rune) bool {
    26  		return ch == internal.Delimiter
    27  	})
    28  	if index >= len(fields) {
    29  		return "", false
    30  	}
    31  
    32  	return fields[index], true
    33  }
    34  
    35  func extractId(etcdKey string) (string, bool) {
    36  	return extract(etcdKey, indexOfId)
    37  }
    38  
    39  func makeEtcdKey(key string, id int64) string {
    40  	return fmt.Sprintf("%s%c%d", key, internal.Delimiter, id)
    41  }