github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/managed_node.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "sync" 25 "time" 26 ) 27 28 type _IManagedNode interface { 29 _GetKey() string 30 _SetVerifyCertificate(verify bool) 31 _GetVerifyCertificate() bool 32 _SetMinBackoff(waitTime time.Duration) 33 _GetMinBackoff() time.Duration 34 _SetMaxBackoff(waitTime time.Duration) 35 _GetMaxBackoff() time.Duration 36 _InUse() 37 _IsHealthy() bool 38 _IncreaseBackoff() 39 _DecreaseBackoff() 40 _Wait() time.Duration 41 _GetUseCount() int64 42 _GetLastUsed() time.Time 43 _GetAttempts() int64 44 _GetReadmitTime() *time.Time 45 _GetAddress() string 46 _ToSecure() _IManagedNode 47 _ToInsecure() _IManagedNode 48 _GetManagedNode() *_ManagedNode 49 _Close() error 50 } 51 52 type _ManagedNode struct { 53 address *_ManagedNodeAddress 54 currentBackoff time.Duration 55 lastUsed time.Time 56 useCount int64 57 minBackoff time.Duration 58 maxBackoff time.Duration 59 badGrpcStatusCount int64 60 readmitTime *time.Time 61 mutex sync.RWMutex 62 } 63 64 func (node *_ManagedNode) _GetAttempts() int64 { 65 node.mutex.RLock() 66 defer node.mutex.RUnlock() 67 return node.badGrpcStatusCount 68 } 69 70 func (node *_ManagedNode) _GetAddress() string { 71 if node.address != nil { 72 return node.address._String() 73 } 74 75 return "" 76 } 77 78 func (node *_ManagedNode) _GetReadmitTime() *time.Time { 79 node.mutex.RLock() 80 defer node.mutex.RUnlock() 81 return node.readmitTime 82 } 83 84 func _NewManagedNode(address string, minBackoff time.Duration) (node *_ManagedNode, err error) { 85 node = &_ManagedNode{ 86 currentBackoff: minBackoff, 87 lastUsed: time.Now(), 88 useCount: 0, 89 minBackoff: minBackoff, 90 maxBackoff: 1 * time.Hour, 91 badGrpcStatusCount: 0, 92 } 93 node.address, err = _ManagedNodeAddressFromString(address) 94 return node, err 95 } 96 97 func (node *_ManagedNode) _SetMinBackoff(minBackoff time.Duration) { 98 if node.currentBackoff == node.minBackoff { 99 node.currentBackoff = node.minBackoff 100 } 101 102 node.minBackoff = minBackoff 103 } 104 105 func (node *_ManagedNode) _GetMinBackoff() time.Duration { 106 return node.minBackoff 107 } 108 109 func (node *_ManagedNode) _SetMaxBackoff(waitTime time.Duration) { 110 node.maxBackoff = waitTime 111 } 112 113 func (node *_ManagedNode) _GetMaxBackoff() time.Duration { 114 return node.maxBackoff 115 } 116 117 func (node *_ManagedNode) _InUse() { 118 node.mutex.Lock() 119 defer node.mutex.Unlock() 120 121 node.useCount++ 122 node.lastUsed = time.Now() 123 } 124 125 func (node *_ManagedNode) _IsHealthy() bool { 126 node.mutex.RLock() 127 defer node.mutex.RUnlock() 128 129 if node.readmitTime == nil { 130 return true 131 } 132 133 return node.readmitTime.Before(time.Now()) 134 } 135 136 func (node *_ManagedNode) _IncreaseBackoff() { 137 node.mutex.Lock() 138 defer node.mutex.Unlock() 139 140 node.badGrpcStatusCount++ 141 node.currentBackoff *= 2 142 if node.currentBackoff > node.maxBackoff { 143 node.currentBackoff = node.maxBackoff 144 } 145 readmitTime := time.Now().Add(node.currentBackoff) 146 node.readmitTime = &readmitTime 147 } 148 149 func (node *_ManagedNode) _DecreaseBackoff() { 150 node.mutex.Lock() 151 defer node.mutex.Unlock() 152 153 node.currentBackoff /= 2 154 if node.currentBackoff < node.minBackoff { 155 node.currentBackoff = node.minBackoff 156 } 157 } 158 159 func (node *_ManagedNode) _Wait() time.Duration { 160 node.mutex.RLock() 161 defer node.mutex.RUnlock() 162 return node.readmitTime.Sub(node.lastUsed) 163 } 164 165 func (node *_ManagedNode) _GetUseCount() int64 { 166 return node.useCount 167 } 168 169 func (node *_ManagedNode) _GetLastUsed() time.Time { 170 return node.lastUsed 171 }