> ## Content Index
> Fetch the complete content index at: https://blog.hacking.pub/llms.txt
> Use this file to discover other available public pages before exploring further.

# 分布式id生成器
- URL: https://blog.hacking.pub/2018/12/27/etcd-fen-bu-shi-id/
- Published: 2018-12-27T12:45:23.000Z
- Updated: 2018-12-27T12:47:22.000Z
- Author: chainhelen
- Tags: id, 唯一id, 生成器, 分布式, 分布式Id

1. etcd  
为什么要使用etcd，主要是要考虑到workid需要抢占锁

[https://yuerblog.cc/2017/12/12/etcd-v3-sdk-usage/](https://yuerblog.cc/2017/12/12/etcd-v3-sdk-usage/?ref=blog.hacking.pub)

分布式锁  
[https://blog.csdn.net/cadem/article/details/56495834](https://blog.csdn.net/cadem/article/details/56495834?ref=blog.hacking.pub)  
[https://www.jianshu.com/p/d3068d0ac7c1](https://www.jianshu.com/p/d3068d0ac7c1?ref=blog.hacking.pub)  
[https://github.com/etcd-io/etcd/blob/master/clientv3/concurrency/mutex.go#L26](https://github.com/etcd-io/etcd/blob/master/clientv3/concurrency/mutex.go?ref=blog.hacking.pub#L26)

github.com\\coreos\\etcd\\clientv3\\lease.go recvKeepAliveLoop

```
for {
	resp, err := stream.Recv()
	if err != nil {
		if canceledByCaller(l.stopCtx, err) {
			return err
		}

		if toErr(l.stopCtx, err) == rpctypes.ErrNoLeader {
			l.closeRequireLeader()
		}
		break
	}

	l.recvKeepAlive(resp)
}

```

这样可以知道`<-chan *clientv3.LeaseKeepAliveResponse`是读取接收报文过后的

```
	// retryConnWait is how long to wait before retrying request due to an error
	retryConnWait = 500 * time.Millisecond

```

发生错误时的重试时间

```
	// send update to all channels
	nextKeepAlive := time.Now().Add((time.Duration(karesp.TTL) * time.Second) / 3.0)

```

正常keepAlive的时间