Here is an example of a card number generation algorithm in Golang and PostgreSQL with Redis:
-
Create a PostgreSQL database with a table to store the IIN and remaining card numbers for each BIN. The table should have columns for the BIN, the last card number generated, and the remaining card numbers.
-
Create a Redis instance and store a map that will be used to track which BINs are currently being generated and lock/unlock the card numbers in Redis.
-
Write a function that takes in a BIN and returns the next available card number. The function should first check the Redis map to see if the BIN is currently being generated. If it is, the function should wait until the BIN is available before proceeding.
-
Once the BIN is available, get the last card number generated for the BIN from the PostgreSQL database. If there are no remaining card numbers for the BIN, throw an error.
-
Increment the last card number generated and store it back in the database.
-
Release the BIN in the Redis map so that it can be used by another instance.
-
Store the generated card number in Redis under the BIN key.
-
Return the generated card number.
Here is an example code for the algorithm:
import (
"database/sql"
"fmt"
"github.com/go-redis/redis/v8"
"sync"
"time"
)
type cardNumberGenerator struct {
db *sql.DB
redis *redis.Client
mutex sync.Mutex
}
func NewCardNumberGenerator(db *sql.DB, redis *redis.Client) *cardNumberGenerator {
return &cardNumberGenerator{db: db, redis: redis}
}
func (c *cardNumberGenerator) getNextCardNumber(bin int) (int, error) {
binKey := fmt.Sprintf("bin:%d", bin)
// check if BIN is being generated, wait if it is
for {
c.mutex.Lock()
if c.redis.Get(binKey).Val() == "free" {
c.redis.Set(binKey, "generating", time.Minute).Err()
c.mutex.Unlock()
break
}
c.mutex.Unlock()
time.Sleep(100 * time.Millisecond)
}
// get last card number generated and remaining card numbers for BIN
var lastCardNumber int
var remainingCardNumbers int
err := c.db.QueryRow("SELECT last_card_number, remaining_card_numbers FROM bin_numbers WHERE bin=?", bin).
Scan(&lastCardNumber, &remainingCardNumbers)
if err != nil {
c.mutex.Lock()
c.redis.Set(binKey, "free", 0).Err()
c.mutex.Unlock()
return 0, err
}
// check if there are remaining card numbers for BIN
if remainingCardNumbers <= 0 {
c.mutex.Lock()
c.redis.Set(binKey, "free", 0).Err()
c.mutex.Unlock()
return 0, fmt.Errorf("no card numbers left to generate for BIN %d", bin)
}
// increment last card number generated and update remaining card numbers in database
lastCardNumber++
remainingCardNumbers--
_, err = c.db.Exec("UPDATE bin_numbers SET last_card_number=?, remaining_card_numbers=? WHERE bin=?", lastCardNumber, remainingCardNumbers, bin)
if err != nil {
c.mutex.Lock()
c.redis.Set(binKey, "free", 0).Err()
c.mutex.Unlock()
return 0, err
}
// release BIN and store generated card number in Redis
c.mutex.Lock()
c.redis.Set(binKey, "free", 0).Err()
c.redis.Set(fmt.Sprintf("card:%d:%d", bin, lastCardNumber), 1, 0).Err()
c.mutex.Unlock()
return lastCardNumber, nil
}
Note that this code assumes that the table in the PostgreSQL database is named bin_numbers and has columns bin, last_card_number, and remaining_card_numbers. It also assumes that the Redis map has keys of the format bin:<bin> and card numbers are stored under keys of the format card:<bin>:<card_number>.