mirror of
https://github.com/openziti/ziti.git
synced 2026-10-05 12:22:25 +00:00
77bce93e85
Previously we often had two sets of events, one in the network package, and then another in the events package. We now only have one, in the events package. Events now have a public API, with minimal deps. There's an implementation package which has the dependencies. This allows the network package to generate events, without have a circular dependency on the events implementation package.
177 lines
3.7 KiB
Go
177 lines
3.7 KiB
Go
/*
|
|
Copyright NetFoundry Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package network
|
|
|
|
import (
|
|
"github.com/openziti/foundation/v2/concurrenz"
|
|
"github.com/openziti/foundation/v2/info"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type Link struct {
|
|
Id string
|
|
Src *Router
|
|
Dst *Router
|
|
Protocol string
|
|
DialAddress string
|
|
state []*LinkState
|
|
down bool
|
|
StaticCost int32
|
|
SrcLatency int64
|
|
DstLatency int64
|
|
Cost int64
|
|
usable concurrenz.AtomicBoolean
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func newLink(id string, linkProtocol string, dialAddress string, initialLatency time.Duration) *Link {
|
|
l := &Link{
|
|
Id: id,
|
|
Protocol: linkProtocol,
|
|
DialAddress: dialAddress,
|
|
state: make([]*LinkState, 0),
|
|
down: false,
|
|
StaticCost: 1,
|
|
SrcLatency: initialLatency.Nanoseconds(),
|
|
DstLatency: initialLatency.Nanoseconds(),
|
|
}
|
|
l.recalculateCost()
|
|
l.addState(&LinkState{Mode: Pending, Timestamp: info.NowInMilliseconds()})
|
|
return l
|
|
}
|
|
|
|
func (link *Link) GetId() string {
|
|
return link.Id
|
|
}
|
|
|
|
func (link *Link) CurrentState() *LinkState {
|
|
link.lock.Lock()
|
|
defer link.lock.Unlock()
|
|
if link.state == nil || len(link.state) < 1 {
|
|
return nil
|
|
}
|
|
return link.state[0]
|
|
}
|
|
|
|
func (link *Link) addState(s *LinkState) {
|
|
link.lock.Lock()
|
|
defer link.lock.Unlock()
|
|
|
|
if link.state == nil {
|
|
link.state = make([]*LinkState, 0)
|
|
}
|
|
link.state = append([]*LinkState{s}, link.state...)
|
|
link.recalculateUsable()
|
|
}
|
|
|
|
func (link *Link) SetDown(down bool) {
|
|
link.lock.Lock()
|
|
defer link.lock.Unlock()
|
|
link.down = down
|
|
link.recalculateUsable()
|
|
}
|
|
|
|
func (link *Link) IsDown() bool {
|
|
link.lock.Lock()
|
|
defer link.lock.Unlock()
|
|
return link.down
|
|
}
|
|
|
|
func (link *Link) recalculateUsable() {
|
|
if link.down {
|
|
link.usable.Set(false)
|
|
} else if len(link.state) < 1 || link.state[0].Mode != Connected {
|
|
link.usable.Set(false)
|
|
} else {
|
|
link.usable.Set(true)
|
|
}
|
|
}
|
|
|
|
func (link *Link) IsUsable() bool {
|
|
return link.usable.Get()
|
|
}
|
|
|
|
func (link *Link) GetStaticCost() int32 {
|
|
return atomic.LoadInt32(&link.StaticCost)
|
|
}
|
|
|
|
func (link *Link) SetStaticCost(cost int32) {
|
|
atomic.StoreInt32(&link.StaticCost, cost)
|
|
link.recalculateCost()
|
|
}
|
|
|
|
func (link *Link) GetSrcLatency() int64 {
|
|
return atomic.LoadInt64(&link.SrcLatency)
|
|
}
|
|
|
|
func (link *Link) SetSrcLatency(latency int64) {
|
|
atomic.StoreInt64(&link.SrcLatency, latency)
|
|
link.recalculateCost()
|
|
}
|
|
|
|
func (link *Link) GetDstLatency() int64 {
|
|
return atomic.LoadInt64(&link.DstLatency)
|
|
}
|
|
|
|
func (link *Link) SetDstLatency(latency int64) {
|
|
atomic.StoreInt64(&link.DstLatency, latency)
|
|
link.recalculateCost()
|
|
}
|
|
|
|
func (link *Link) recalculateCost() {
|
|
cost := int64(link.GetStaticCost()) + link.GetSrcLatency()/1_000_000 + link.GetDstLatency()/1_000_000
|
|
atomic.StoreInt64(&link.Cost, cost)
|
|
}
|
|
|
|
func (link *Link) GetCost() int64 {
|
|
return atomic.LoadInt64(&link.Cost)
|
|
}
|
|
|
|
type LinkMode byte
|
|
|
|
const (
|
|
Pending LinkMode = iota
|
|
Connected
|
|
Failed
|
|
)
|
|
|
|
func (t LinkMode) String() string {
|
|
if t == Pending {
|
|
return "Pending"
|
|
} else if t == Connected {
|
|
return "Connected"
|
|
} else if t == Failed {
|
|
return "Failed"
|
|
} else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
type LinkState struct {
|
|
Mode LinkMode
|
|
Timestamp int64
|
|
}
|
|
|
|
func newLinkState(mode LinkMode) *LinkState {
|
|
return &LinkState{
|
|
Mode: mode,
|
|
Timestamp: info.NowInMilliseconds(),
|
|
}
|
|
}
|