paygo

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 2 Imported by: 0

README

Pay as you Go

This is a small and simple library for Golang to scheduler deduct user's wallet balance every X seconds.

What is Pay As You Go ?

One way to get money from a user is to deduct some money from the user's wallet based on a specific time, and if the user's wallet does not have money, we will no longer provide the service.

Concept

  • DoFunc is a function for each X seconds when it comes time to pay them. why you need yo pass it? Because you want to reduce the amount of RAM consumed for each item.
  • UUID is a unique id for a new pay as you go item. You can set it any things but it must be unique like: user transaction id in your database.
  • Wallet is user wallet id and why you need to pass it? Because by passing it to DoFunc you can get your user's wallet balance and update it.

Example

In this example we use wallets that are in variable. We deduct wallet balance and print that every 10s.

package main

import (
	"fmt"

	payg "github.com/iamnonroot/pay-as-you-go"
)

type Wallets map[string]int

func main() {
    // [wallet id] => wallet balance balance
	var wallets Wallets = Wallets{
		"root": 1000,
		"user": 10000,
	}

	item := payg.New(&payg.Item{
		Every: 10, // every 10 seconds
		DoFunc: func(option payg.ItemOption) bool {
            // get wallet balance
			balance := wallets[option.Wallet]

            // if the wallet balance is low, delete scheduler by returning false
			if balance-option.Price < 0 {
				return false
			}

            // set wallet balance
			wallets[option.Wallet] = balance - option.Price

			fmt.Println(wallets[option.Wallet])

            // continue
			return true
		},
	})

	item.Add(payg.ItemOption{
		UUID:   "root-going", // a uuid for pay as you go
		Wallet: "root", // user's wallet id
		Price:  400, // product price
	})

	item.Add(payg.ItemOption{
		UUID:   "user-going",
		Wallet: "user",
		Price:  6000,
	})

	item.Start()
}

Documentation

Overview

Pay as you Go library to scheduler deduct user's wallet balance every X seconds

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Every  int
	DoFunc func(option ItemOption) bool
	// contains filtered or unexported fields
}

Create a new scheduler by setting how many every seconds jobs run and function to get user wallet balance and function to set user wallet balance (if return be false, job will removed)

func New

func New(item *Item) *Item

Create a new pay as you go with creating a new scheduler

Example
package main

import (
	"fmt"

	payg "github.com/iamnonroot/pay-as-you-go"
)

func main() {
	var wallets map[string]int = map[string]int{
		"root": 1000,
		"user": 10000,
	}

	item := payg.New(&payg.Item{
		Every: 3,
		DoFunc: func(option payg.ItemOption) bool {
			balance := wallets[option.Wallet]

			if balance-option.Price < 0 {
				return false
			}

			wallets[option.Wallet] = balance - option.Price

			fmt.Println(wallets[option.Wallet])

			return true
		},
	})

	item.Add(payg.ItemOption{
		UUID:   "root-going",
		Wallet: "root",
		Price:  400,
	})

	item.Add(payg.ItemOption{
		UUID:   "user-going",
		Wallet: "user",
		Price:  6000,
	})

	item.Start()
}
Output:

func (*Item) Add

func (item *Item) Add(option ItemOption)

Add a new scheduler job with a UUID and do it for every X seconds and a function to do

func (*Item) Remove

func (item *Item) Remove(uuid string)

Remove a scheduler job with UUID

func (*Item) Start

func (item *Item) Start()

Start scheduler

type ItemOption

type ItemOption struct {
	UUID   string
	Wallet string
	Price  int
}

Create new scheduler job by setting a unique name/id for job and user wallet id and price of product

Jump to

Keyboard shortcuts

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