bank

package
v0.0.0-...-e20221d Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Id      int64
	Balance int64
}

Account 是一个线程不安全的结构体,实现了Accountable接口

func (*Account) GetBalance

func (a *Account) GetBalance() int64

func (*Account) GetId

func (a *Account) GetId() int64

func (*Account) SetBalance

func (a *Account) SetBalance(amount int64)

func (*Account) Transfer

func (a *Account) Transfer(to Accountable, amount int64)

type AccountV1

type AccountV1 struct {
	Account
	// contains filtered or unexported fields
}

AccountV1 组合了Account,通过锁,保证线程安全

func (*AccountV1) Transfer

func (a *AccountV1) Transfer(to Accountable, amount int64)

type AccountV2

type AccountV2 struct {
	Account
}

AccountV2 组合了Account,通过借助外部的Allocator,保证线程安全

func (*AccountV2) Transfer

func (a *AccountV2) Transfer(to Accountable, amount int64)

type AccountV3

type AccountV3 struct {
	Account
	// contains filtered or unexported fields
}

AccountV3 组合了Account,通过信号量,保证线程安全

func NewAccountV3

func NewAccountV3(id int64, balance int64) *AccountV3

func (*AccountV3) Transfer

func (a *AccountV3) Transfer(to Accountable, amount int64)

type Accountable

type Accountable interface {
	GetId() int64
	GetBalance() int64
	SetBalance(amount int64)
	Transfer(to Accountable, amount int64)
}

type Allocator

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

func (*Allocator) Apply

func (a *Allocator) Apply(from, to any)

func (*Allocator) Free

func (a *Allocator) Free(from, to any)

type Bank

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

func NewBank

func NewBank() *Bank

func (*Bank) Close

func (bank *Bank) Close()

func (*Bank) Transfer

func (bank *Bank) Transfer(from Accountable, to Accountable, amount int64)

Transfer 通过银行级别的锁,保证线程安全,不必考虑Accountable对象本身是否线程安全

func (*Bank) TransferAsync

func (bank *Bank) TransferAsync(from Accountable, to Accountable, amount int64)

TransferAsync 通过银行级别的channel,把转账业务由并行转串行,保证线程安全,不必考虑Accountable对象本身是否线程安全

type TransferTask

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

Jump to

Keyboard shortcuts

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