johncming / go-spec

my wiki
Apache License 2.0
0 stars 0 forks source link

etcd member ID的来源 #108

Open johncming opened 5 years ago

johncming commented 5 years ago
func NewMember(name string, peerURLs types.URLs, clusterName string, now *time.Time) *Member {
    m := &Member{
        RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
        Attributes:     Attributes{Name: name},
    }
    plog.Infof("%+v", m)

    var b []byte
    sort.Strings(m.PeerURLs)
    for _, p := range m.PeerURLs {
        b = append(b, []byte(p)...)
    }

    b = append(b, []byte(clusterName)...)
    if now != nil {
        b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
    }

    hash := sha1.Sum(b)
    m.ID = types.ID(binary.BigEndian.Uint64(hash[:8]))
    return m
}