serial

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2021 License: MIT Imports: 4 Imported by: 0

README

Serial Enumerator and Number

Serial Enumerator and Number types

Serial Enumerator

Serial provides an Enumerator type generates a reuseable pool of numbers and returns a set of functions to request() and release() a SID (serialized numeric ID) from an available limited numeric id pool range which is given by the user as 0...{n} range.

A SID can be released in any order, but requests are always forward looking and will be the next sequential numeric ID available for use or re-use.

Size the Enumerator properly to avoid exhaustion and blocking while waiting for a SID to become available because with exhaustion of all SID the Enumerator will block waiting for a SID to be released for reuse.

For example, if you consume 1000 sid/sec and hold a SID for 3 seconds on average, an appropraite size might be found by taking your consumtion times average duration and adding in an additional padding factor to prevent blocking. (eg. 1000x3 x2 = 6000)


  request,release := serial.Enumerator(6000)
  
  for !exit {
    go func(id int) {
      defer release(id)
      // ... do stuff
      
    }(request())
  }

Number

The Serial Number type provides a sequential enumerator that generates serial numbers sequentially utilizing the first 7 bytes of a uint64 to generate a number in the range N00000000000001 to NFFFFFFFFFFFFFF. The type provides method to persist the current Number to disk.


  var persist = "./number.persist"
  func Example() {
    var n Number
    var exit bool
    n.Load(persist)
    defer n.Save(persist)
    for !exit {
      sn := n.Next()
      // ... do stuff

      fmt.Println(sn)
      // N00000000000001
    }
  }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enumerator

func Enumerator(size int) (request func() (sid int), release func(sid int))

Enumerator returns a set of functions to request() and release() a SID (serialized numeric ID) from an available limited numeric pool range. The SID can be released in any order, but requests are always forward looking and will be the next sequential one available.

Size the pool properly to avoid exhaustion and blocking while waiting for a SID to become available. For example, if you consume 1000 sid/sec and hold a SID for 3 seconds on average, an appropraite size might be found like this: (1000*3) *2 = 6000

Types

type Number added in v1.1.0

type Number uint64

Number provides a persistent sequential serial number type that can be persisted to disk; utilizes the first 7 bytes of uint64

func (*Number) Load added in v1.1.0

func (n *Number) Load(path string)

Load Number from disk

func (*Number) Next added in v1.1.0

func (n *Number) Next() string

Next generates the next serial Number in the range (N00000000000001 to NFFFFFFFFFFFFFF)

func (*Number) Save added in v1.1.0

func (n *Number) Save(path string)

Save Number to disk

Jump to

Keyboard shortcuts

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