raus

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2022 License: MIT Imports: 15 Imported by: 4

README

raus

Ranged unique id supplier

GoDoc

LICENSE

MIT License

Copyright (c) 2017 FUJIWARA Shunichiro

Documentation

Index

Examples

Constants

View Source
const (
	DefaultNamespace = "raus"
)

Variables

View Source
var (
	MaxCandidate     = 10
	LockExpires      = 60 * time.Second
	SubscribeTimeout = time.Second * 3
	CleanupTimeout   = time.Second * 30
)

Functions

func SetLogger

func SetLogger(l Logger)

Types

type Logger

type Logger interface {
	Println(...interface{})
	Printf(string, ...interface{})
}

type Raus

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

func New

func New(redisURI string, min, max uint) (*Raus, error)

New creates *Raus object.

Example
package main

import (
	"context"
	"log"
	"sync"

	"github.com/fujiwara/raus"
)

var redisURL = "redis://localhost:26379"

func main() {
	// prepere context
	ctx, cancel := context.WithCancel(context.Background())

	r, _ := raus.New(redisURL, 0, 3)
	id, ch, _ := r.Get(ctx)
	log.Printf("Got id %d", id)

	// watch error
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		err, more := <-ch
		if !more {
			// raus shutdown successfully
			return
		} else {
			// fatal error
			panic(err)
		}
	}()

	// Run your application code

	// notify shutdown
	cancel()

	wg.Wait()
}
Output:

func (*Raus) Get

func (r *Raus) Get(ctx context.Context) (uint, chan error, error)

Get gets unique id ranged between min and max.

type RedisClient added in v0.1.0

type RedisClient interface {
	Close() error
	Subscribe(ctx context.Context, channels ...string) *redis.PubSub
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
	Get(ctx context.Context, key string) *redis.StringCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd
	TxPipeline() redis.Pipeliner
}

type RedisOptions added in v0.1.0

type RedisOptions struct {
	Cluster  bool
	Addrs    []string
	Username string
	Password string
	DB       int
}

func ParseRedisURI

func ParseRedisURI(s string) (*RedisOptions, string, error)

ParseRedisURI parses uri for redis (redis://host:port/db?ns=namespace)

func (*RedisOptions) NewClient added in v0.1.0

func (o *RedisOptions) NewClient() RedisClient

Jump to

Keyboard shortcuts

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