Ivan Belyakov 4a1f751ced feat(wallet)_: Implemented RPC limiter interface
Split ClientInterface to aggregation of multiple interfaces
Added tags to RPC stats API
Use tagged RPC client for transfers commands
Implemented general interface for RPC limiting
2024-05-28 13:44:40 +02:00

79 lines
1.3 KiB
Go

package connection
import (
"sync"
"time"
)
type Connectable interface {
SetIsConnected(bool)
IsConnected() bool
}
type StateChangeCb func(State)
type Status struct {
stateChangeCb StateChangeCb
state State
stateLock sync.RWMutex
}
func NewStatus() *Status {
return &Status{
state: NewState(),
}
}
func (c *Status) SetStateChangeCb(stateChangeCb StateChangeCb) {
c.stateChangeCb = stateChangeCb
}
func (c *Status) SetIsConnected(value bool) {
now := time.Now().Unix()
state := c.GetState()
state.LastCheckedAt = now
if value {
state.LastSuccessAt = now
}
if value {
state.Value = StateValueConnected
} else {
state.Value = StateValueDisconnected
}
c.SetState(state)
}
func (c *Status) ResetStateValue() {
state := c.GetState()
state.Value = StateValueUnknown
c.SetState(state)
}
func (c *Status) GetState() State {
c.stateLock.RLock()
defer c.stateLock.RUnlock()
return c.state
}
func (c *Status) SetState(state State) {
c.stateLock.Lock()
isStateChange := c.state.Value != state.Value
c.state = state
c.stateLock.Unlock()
if isStateChange && c.stateChangeCb != nil {
c.stateChangeCb(state)
}
}
func (c *Status) GetStateValue() StateValue {
return c.GetState().Value
}
func (c *Status) IsConnected() bool {
return c.GetStateValue() == StateValueConnected
}