idgo

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 9 Imported by: 2

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllocatedIDStore

type AllocatedIDStore interface {
	// contains filtered or unexported methods
}

AllocatedIDStore stores allocated id.

type IDGenerateClient

type IDGenerateClient struct {
	// contains filtered or unexported fields
}

IDGenerateClient requests new id to server.

func NewClient

func NewClient() *IDGenerateClient

NewClient is IDGenerateClient constructed.

func (*IDGenerateClient) Allocate

func (c *IDGenerateClient) Allocate(id int) error

Allocate a specified id.

func (*IDGenerateClient) Close

func (c *IDGenerateClient) Close() error

Close connection.

func (*IDGenerateClient) Connect

func (c *IDGenerateClient) Connect(addr *net.TCPAddr) error

Connect to server.

func (*IDGenerateClient) Free

func (c *IDGenerateClient) Free(id int) error

Free a allocated id.

func (*IDGenerateClient) FreeAll

func (c *IDGenerateClient) FreeAll() error

FreeAll free all allocated id.

func (*IDGenerateClient) Generate

func (c *IDGenerateClient) Generate() (int, error)

Generate a new id.

func (*IDGenerateClient) GetAllocatedIDCount

func (c *IDGenerateClient) GetAllocatedIDCount(id int) (int, error)

GetAllocatedIDCount is getter for allocatedIDCount.

func (*IDGenerateClient) IsAllocated

func (c *IDGenerateClient) IsAllocated(id int) (bool, error)

IsAllocated check if specified id is allocated.

func (*IDGenerateClient) Ping

func (c *IDGenerateClient) Ping() error

Ping verifies a connection to the server is still alive.

func (*IDGenerateClient) Reconnect

func (c *IDGenerateClient) Reconnect() error

Reconnect connects to the server if a connection to the server is not alive.

type IDGenerateServer

type IDGenerateServer struct {
	// contains filtered or unexported fields
}

IDGenerateServer generate id when requested by client.

func NewServer

func NewServer(store AllocatedIDStore, tcpAddr *net.TCPAddr) (*IDGenerateServer, error)

NewServer is IDGenerateServer constructed.

func (*IDGenerateServer) Pause

func (s *IDGenerateServer) Pause() error

Pause server while maintaining allocated id.

func (*IDGenerateServer) Run

func (s *IDGenerateServer) Run() error

Run server.

func (*IDGenerateServer) SetLogger

func (s *IDGenerateServer) SetLogger(logger *log.Logger)

SetLogger is setter for logger.

func (*IDGenerateServer) Stop

func (s *IDGenerateServer) Stop() error

Stop server and free all allocated id.

type IDGenerator

type IDGenerator struct {
	// contains filtered or unexported fields
}

IDGenerator generate a id.

func NewIDGenerator

func NewIDGenerator(store AllocatedIDStore) (*IDGenerator, error)

NewIDGenerator is IDGenerator constructed.

func (*IDGenerator) Allocate

func (g *IDGenerator) Allocate(id int) error

Allocate a specified id.

func (*IDGenerator) Free

func (g *IDGenerator) Free(id int) error

Free a allocated id.

func (*IDGenerator) FreeAll

func (g *IDGenerator) FreeAll() error

FreeAll free all allocated id.

func (*IDGenerator) Generate

func (g *IDGenerator) Generate() (int, error)

Generate a new id.

func (*IDGenerator) GetAllocatedIDCount

func (g *IDGenerator) GetAllocatedIDCount() (int, error)

GetAllocatedIDCount is getter for allocatedIDCount.

func (*IDGenerator) IsAllocated

func (g *IDGenerator) IsAllocated(id int) (bool, error)

IsAllocated check if specified id is allocated.

type LocalStore

type LocalStore struct {
	// contains filtered or unexported fields
}

LocalStore stores allocated id to byte slice.

func NewLocalStore

func NewLocalStore(maxSize int) (*LocalStore, error)

NewLocalStore is LocalStore constructed.

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore stores allocated id to redis.

func NewRedisStore

func NewRedisStore(host, key string, maxSize int) (*RedisStore, error)

NewRedisStore is RedisStore constructed.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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