https://github.com/catmullet/one
🚥 Idempotency Handler, for making sure incoming requests are idempotent. Useful for payments, "at least once delivery" systems and more.
https://github.com/catmullet/one
delivery golang idempotence idempotency idempotency-handler idempotency-key idempotent idempotent-requests payment redis storage
Last synced: 5 months ago
JSON representation
🚥 Idempotency Handler, for making sure incoming requests are idempotent. Useful for payments, "at least once delivery" systems and more.
- Host: GitHub
- URL: https://github.com/catmullet/one
- Owner: catmullet
- License: mit
- Created: 2020-10-27T23:57:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T03:24:07.000Z (over 5 years ago)
- Last Synced: 2024-06-19T03:08:23.525Z (almost 2 years ago)
- Topics: delivery, golang, idempotence, idempotency, idempotency-handler, idempotency-key, idempotent, idempotent-requests, payment, redis, storage
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 21
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# _(ī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:
```sh
go get github.com/catmullet/one
```
import into your project
```go
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
```go
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:
```go
key := one.MakeKey(event.Bucket, event.Object)
```
If you wanted to make sure you processed on every version update you would use this:
```go
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:
```go
key := one.MakeKey(event)
```
#### Redis
```go
// 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
```go
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
```