idgenerator

package module
v0.0.0-...-422bbaa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 3 Imported by: 0

README

idgenerator is an id generator based on redis

dgenerator是基于redis的id生成器

Installation

获取

go get github.com/lbfatcgf/idgenerator

Quick start
package main

import (
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	"github.com/go-redis/redis"
	"github.com/lbfatcgf/idgenerator"
	"github.com/lbfatcgf/idgenerator/recorder"
)

func main() {
	c := redis.NewClient(&redis.Options{
		Addr:     "192.168.122.208:6379",
		Password: "",
		DB:       0,
	})
	r := recorder.NewRedisRecorder(c, fmt.Sprintf("idg_%d", 0xff), 0)

	conf := idgenerator.NewIDConfig()

	// idg, err :=idgenerator.NewIdGenerator(conf, 0xff, r)
	idg, err := idgenerator.NewCachedIdGenerator(conf, 0xff, r)

	if err != nil {
		fmt.Println(err)
		return
	}

	go func() {
		c := make(chan os.Signal)
		signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTSTP)

		 <-c
		idg.IDRecorder.(recorder.Recorder).Close()
		os.Exit(0)
	}()
	
	http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
		id, _ := idg.GetID()
		rw.Header().Set("content-type","text/json")
		rw.WriteHeader(http.StatusOK)
		rw.Write([]byte(fmt.Sprintf("{\"id\":%d}",id)))
	})
	http.ListenAndServe(":8080",nil)
}

The record customization in id can be realized through the Recorder interface

可以通过Recorder接口实现在id的记录自定义

//Recorder 记录器接口
type Recorder interface {
	Save(data uint64)      //Save id to recorder.   保存id到记录器
	Load() (uint64, error) //Load id from recorder. 从记录器加载id
	IsClose() bool         //Get closed status.     获取关闭状态
	Close()                //Turn off the recorder. 关闭记录器
}

Documentation

Overview

idgenerator id generator,id生成器

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	Config               *IDConfig //Structure of custom id.		自定义id的结构
	CustomIntervalNumber uint64    //Business area, used to distinguish the id generated by different services.		业务区间,用来区分不同服务生成的id
	SerialNumber         uint64    //Self-increasing serial number.		自增序列号

	IDRecorder recorder.Recorder //id recorder, customizable implementation interface.		id记录器,可自定义实现接口
	// contains filtered or unexported fields
}

Generator Implementation of the generator. 生成器的实现

func NewCachedIdGenerator

func NewCachedIdGenerator(config *IDConfig, custominterval uint64, r recorder.Recorder) (*Generator, error)

NewCachedIdGenerator Get a generator with customizable buffer size. 获得可自定义缓冲大小的生成器

func NewIdGenerator

func NewIdGenerator(config *IDConfig, custominterval uint64, r recorder.Recorder) (*Generator, error)

NewIdGenerator Get a cache-free generator. 获得一个无缓存的生成器

func (*Generator) GetID

func (this *Generator) GetID() (uint64, bool)

GetID Get an id, block when there is a buffer. 获取一个id,有缓冲时阻塞

type IDConfig

type IDConfig struct {
	CustomIntervalBits uint16 //The size of the CustomInterval.						业务区间的占位大小
	SerialBits         uint16 //The size of the self-incrementing serial number.	自增序列号的占位大小
	BufferSize         int    //缓冲区大小
}

IDConfig id生成器的配置

func NewIDConfig

func NewIDConfig() *IDConfig

NewIDConfig Get a default configuration. 获取一个默认配置

return &IDConfig{

	CustomIntervalBits: 10,
 	SerialBits:         53,
 	BufferSize:         4096,
 }

func (*IDConfig) OverflowDetect

func (this *IDConfig) OverflowDetect()

OverflowDetect 溢出检测

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL