Snowflak算法(C#版本)-创新互联

最近看了twitter的分布式自增ID算法Snowflak,自己根据开源的代码写了一个C#的版本,以记录学习。 twitter的开源项目地址为:https://github.com/twitter/snowflake 用Scala实现

创新互联公司长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为徐水企业提供专业的成都网站建设、成都网站设计,徐水网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
class IdWorker
    {
        //机器标识位数
        private const int WORKER_ID_BITS = 4;
        //机器标识位的大值
        private const long MAX_WORKER_ID = -1L ^ -1L << WORKER_ID_BITS;
        //毫秒内自增位
        private const int SEQUENCE_BITS = 10;
        //自增位大值
        private const long SEQUENCE_Mask = -1L ^ -1L << SEQUENCE_BITS;
        private const long twepoch = 1398049504651L;
        //时间毫秒值向左偏移位
        private const int timestampLeftShift = SEQUENCE_BITS + WORKER_ID_BITS;
        //机器标识位向左偏移位
        private const int WORKER_ID_SHIFT = SEQUENCE_BITS;
        private static readonly DateTime START_TIME = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
        private static readonly object LOCK = new object();
        private long sequence = 0L;
        private long lastTimestamp = -1L;
        private long workerId;
        public IdWorker(long workerId)
        {
            if (workerId > MAX_WORKER_ID || workerId < 0)
            {
                throw new ArgumentException(string.Format("worker id can't be greater than {0} or less than 0", MAX_WORKER_ID));
            }
            this.workerId = workerId;
        }
        /// 
        /// 获取下一个id值
        /// 
        /// 
        public long NextId()
        {
            lock (LOCK)
            {
                long timestamp = TimeGen();
                //当前时间小于上一次时间,错误
                if (timestamp < this.lastTimestamp)
                {
                    throw new Exception(string.Format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
                }
                //当前毫秒内
                if (this.lastTimestamp == timestamp)
                {
                    //+1 求余
                    this.sequence = (this.sequence + 1) & SEQUENCE_Mask;
                    //当前毫秒内计数满了,等待下一秒
                    if (this.sequence == 0)
                    {
                        timestamp = tilNextMillis(lastTimestamp);
                    }
                }
                else      //不是当前毫秒内
                {
                    this.sequence = 0;   //重置当前毫秒计数
                }
                this.lastTimestamp = timestamp;
                //当前毫秒值 | 机器标识值 | 当前毫秒内自增值
                long nextId = ((timestamp - twepoch << timestampLeftShift))
                | (this.workerId << WORKER_ID_SHIFT) | (this.sequence);
                return nextId;
            }
        }
        /// 
        /// 等待下一个毫秒
        /// 
        /// 
        /// 
        private long tilNextMillis(long lastTimestamp)
        {
            long timestamp = TimeGen();
            while (timestamp <= lastTimestamp)
            {
                timestamp = TimeGen();
            }
            return timestamp;
        }
        /// 
        /// 获取当前时间的Unix时间戳
        /// 
        /// 
        private long TimeGen()
        {
            return (DateTime.UtcNow.Ticks - START_TIME.Ticks) / 10000;
        }
    }

调用方式如下

static void Main(string[] args)
{
    IdWorker worker = new IdWorker(12L);
    for (int i = 0; i < 100000; i++)
    {
        Console.WriteLine(worker.NextId());
    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页标题:Snowflak算法(C#版本)-创新互联
URL网址:http://ybzwz.com/article/cecegj.html