94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
|
|
package notify
|
||
|
|
|
||
|
|
import "sync/atomic"
|
||
|
|
|
||
|
|
type clientConnTransportState struct {
|
||
|
|
streamTransport atomic.Bool
|
||
|
|
transportGen atomic.Uint64
|
||
|
|
attachCount atomic.Uint64
|
||
|
|
detachCount atomic.Uint64
|
||
|
|
lastAttachAt atomic.Int64
|
||
|
|
transportDetach atomic.Pointer[clientConnTransportDetachState]
|
||
|
|
}
|
||
|
|
|
||
|
|
func cloneClientConnTransportDetachState(src *clientConnTransportDetachState) *clientConnTransportDetachState {
|
||
|
|
if src == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
cloned := *src
|
||
|
|
return &cloned
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *LogicalConn) ensureTransportState() *clientConnTransportState {
|
||
|
|
if c == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if state := c.transportState.Load(); state != nil {
|
||
|
|
if client := c.compatClientConn(); client != nil {
|
||
|
|
client.transportState.Store(state)
|
||
|
|
}
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
client := c.compatClientConn()
|
||
|
|
if client != nil {
|
||
|
|
if state := client.transportState.Load(); state != nil {
|
||
|
|
if c.transportState.CompareAndSwap(nil, state) {
|
||
|
|
client.transportState.Store(state)
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.ensureTransportState()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
state := &clientConnTransportState{}
|
||
|
|
if c.transportState.CompareAndSwap(nil, state) {
|
||
|
|
if client != nil {
|
||
|
|
client.transportState.Store(state)
|
||
|
|
}
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.ensureTransportState()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ClientConn) ensureClientConnTransportState() *clientConnTransportState {
|
||
|
|
if c == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if logical := c.logicalView.Load(); logical != nil {
|
||
|
|
return logical.ensureTransportState()
|
||
|
|
}
|
||
|
|
if state := c.transportState.Load(); state != nil {
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
state := &clientConnTransportState{}
|
||
|
|
if c.transportState.CompareAndSwap(nil, state) {
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.transportState.Load()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ClientConn) setClientConnTransportDetachState(state *clientConnTransportDetachState) {
|
||
|
|
if c == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if logical := c.logicalView.Load(); logical != nil {
|
||
|
|
logical.setTransportDetachState(state)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
transportState := c.ensureClientConnTransportState()
|
||
|
|
if transportState == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
transportState.transportDetach.Store(cloneClientConnTransportDetachState(state))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *LogicalConn) setTransportDetachState(state *clientConnTransportDetachState) {
|
||
|
|
transportState := c.ensureTransportState()
|
||
|
|
if transportState == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
transportState.transportDetach.Store(cloneClientConnTransportDetachState(state))
|
||
|
|
if client := c.compatClientConn(); client != nil {
|
||
|
|
client.transportState.Store(transportState)
|
||
|
|
}
|
||
|
|
}
|