storj/pkg/piecestore/rpc/server/allocationManager.go
Alexander Leitner b0db33f919
Bandwidth accounting (#134)
* captplanet standalone farmer setup

* Bandwidth Allocation

* utils.Close method changed to utils.LogClose

* Get build temporarily working

* Get/Put for PSClient should take payer bandwidth allocations rather than the NewPSClient function

* Update example client to reflect changes in client API

* Update ecclient to use latest PSClient, Make NewPSClient return error also

* Updated pieceranger tests to check for errors; sign method should take byte array

* Handle defers in store.go better

* Fix defer functions in psdb.go

* fun times

* Protobuf bandwidthallocation data is now a byte array

* Remove psservice package and merge it into pstore server

* Write wrapper for database calls

* Change all expiration names in protobuf to be more informative; add defer in retrieve; remove old comment

* Make PSDB tests implementation independent rather than method independent

* get rid of payer, renter in ecclient

* add context monitoring in store and retrieve
2018-08-17 13:40:15 -04:00

53 lines
1.3 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import "github.com/zeebo/errs"
// AllocationManager manages allocations for file retrieval
type AllocationManager struct {
TotalAllocated, Used int64
}
// NewAllocationManager returns a new AllocationManager
func NewAllocationManager() *AllocationManager {
return &AllocationManager{}
}
// AllocationError is a type of error for failures in AllocationManager
var AllocationError = errs.Class("allocation error")
// NextReadSize returns the size to be read from file based on max that can be Used
func (am *AllocationManager) NextReadSize() int64 {
if am.TotalAllocated <= 0 {
return 0
}
remaining := am.TotalAllocated - am.Used
if remaining > 32*1024 {
return 32 * 1024
}
return remaining
}
// NewTotal adds another allcoation to the AllocationManager
func (am *AllocationManager) NewTotal(total int64) {
if total > am.TotalAllocated {
am.TotalAllocated = total
}
}
// UseAllocation indicates to the AllocationManager that an Amount was successfully Used for an allocation
func (am *AllocationManager) UseAllocation(amount int64) error {
if amount > am.TotalAllocated-am.Used {
return AllocationError.New("can't use unallocated bandwidth")
}
am.Used += amount
return nil
}