Code Monkey home page Code Monkey logo

idgo's Introduction

idgo

GoDoc CircleCI Travis Go Report Card

idgo is a very fast id generator that generates an int id that can specify the maximum value

Install

go get github.com/minami14/idgo

Usage

package main

import (
	"fmt"
	"log"
	"math"

	"github.com/minami14/idgo/idgo"
)

func main() {
	store := idgo.NewLocalStore(math.MaxInt16)
	gen, err := idgo.NewIDGenerator(store)
	if err != nil {
		log.Fatal(err)
	}

	// Generate a id.
	id, err := gen.Generate()
	if err != nil {
		log.Println(err)
	}

	// Generated id.
	fmt.Println(id)

	// Allocated id count.
	fmt.Println(gen.GetAllocatedIDCount())

	// id is allocated.
	fmt.Println(gen.IsAllocated(id))
	isAllocated, err := gen.IsAllocated(id)
	if err != nil {
		log.Println(err)
	}
	fmt.Println(isAllocated)

	// Free the id.
	if err := gen.Free(id); err != nil {
		log.Println(err)
	}

	// Allocate the id.
	if err := gen.Allocate(id); err != nil {
		log.Println(err)
	}

	// Free all id.
	if err := gen.FreeAll(); err != nil {
		log.Println(err)
	}
}

Server

Build

go build -o idgo-server ./cmd/server/main.go
chmod +x idgo-server

Show usage

./idgo-server -h

Run server

./idgo-server -m [maximum value of id] -p [port number]

Client

package main

import (
	"fmt"
	"log"
	"net"

	"github.com/minami14/idgo/idgo"
)

func main() {
	tcpAddr, err := net.ResolveTCPAddr("tcp", ":49152")
	if err != nil {
		log.Fatal(err)
	}
	
	client := idgo.NewClient()
	if err := client.Connect(tcpAddr); err != nil {
		log.Fatal(err)
	}
	
	id, err := client.Generate()
	if err != nil {
		log.Println(id)
	}
	
	if err := client.Free(id); err != nil {
		log.Println(err)
	}
}

License

MIT License

idgo uses the following libraries

idgo's People

Contributors

minami14 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.