one

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 2 Imported by: 0

README

one logo

(īdəmˌpōtənt) Idempotency Handler

Description

Easily check if you have recieved/processed an object before. Some examples may be:

  • Where PubSub services use "at least once delivery"
  • Cases of accepting requests to make payment
  • Deduping requests to your services

Getting Started

import the repo by running:

go get github.com/catmullet/one

import into your project

import github.com/catmullet/one

Creating Keys

The one.MakeKey() function takes in an array of any type and creates a key. This key is specific to the parameters you have passed in. If you pass in the exact same fields you will get the exact same key. Its how we tell if we are getting the same request. Choose Something that will give you a good indication that this is not the same object. you can be as strict or relaxed as you want with it.

Take for example this event from cloud storage

type Event struct {
  Bucket string
  Object string
  Version int
  UpdateTime time.Time
}

If you wanted to make sure that you never processed this storage object again you would use this:

key := one.MakeKey(event.Bucket, event.Object)

If you wanted to make sure you processed on every version update you would use this:

key := one.MakeKey(event.Bucket, event.Object, event.Version)

If you wanted to process based on any change to the storage object you could pass in the entire object like this:

key := one.MakeKey(event)
Redis
// import "gopkg.in/redis.v5" for redis.Options

options := &redis.Options{
		Network:            "tcp",
		Addr:               "localhost:6379",
		Dialer:             nil,
		Password:           "",
		DB:                 0,
	}
  
var oneStore OneStore
oneStore = redisstore.NewRedisOneStore(options, time.Second * 30)

Add Keys

ok, err := oneStore.Add(key)
if !ok {
  // Key already exists, so handle that here.
}

// Key doesn't exist and was added to the one store

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyExist Error for notifying of existing key.
	ErrKeyExist = fmt.Errorf("error: key exists")
)

Functions

func MakeKey

func MakeKey(fields ...interface{}) (key string)

MakeKey Creates key based on field values.

Types

type Store added in v1.2.0

type Store interface {
	AddKey(key string) (ok bool, err error)
}

OneStore interface for existing and custom key storage.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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