99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
|
|
package notify
|
||
|
|
|
||
|
|
import "sync/atomic"
|
||
|
|
|
||
|
|
type logicalConnRuntimeState struct {
|
||
|
|
sessionRuntime atomic.Pointer[clientConnSessionRuntime]
|
||
|
|
}
|
||
|
|
|
||
|
|
func cloneClientConnSessionRuntime(src *clientConnSessionRuntime) *clientConnSessionRuntime {
|
||
|
|
if src == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
cloned := *src
|
||
|
|
return &cloned
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *logicalConnRuntimeState) sessionRuntimeSnapshot() *clientConnSessionRuntime {
|
||
|
|
if s == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return s.sessionRuntime.Load()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *logicalConnRuntimeState) setSessionRuntime(rt *clientConnSessionRuntime) {
|
||
|
|
if s == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.sessionRuntime.Store(cloneClientConnSessionRuntime(rt))
|
||
|
|
}
|
||
|
|
|
||
|
|
func newLogicalConnRuntimeStateFromClient(c *ClientConn) *logicalConnRuntimeState {
|
||
|
|
if c == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
state := &logicalConnRuntimeState{}
|
||
|
|
state.setSessionRuntime(c.sessionRuntime.Load())
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *LogicalConn) ensureRuntimeState() *logicalConnRuntimeState {
|
||
|
|
if c == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if state := c.runtime.Load(); state != nil {
|
||
|
|
if client := c.compatClientConn(); client != nil {
|
||
|
|
client.runtimeState.Store(state)
|
||
|
|
}
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
client := c.compatClientConn()
|
||
|
|
if client != nil {
|
||
|
|
if state := client.runtimeState.Load(); state != nil {
|
||
|
|
if c.runtime.CompareAndSwap(nil, state) {
|
||
|
|
client.runtimeState.Store(state)
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.ensureRuntimeState()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
state := newLogicalConnRuntimeStateFromClient(client)
|
||
|
|
if state == nil {
|
||
|
|
state = &logicalConnRuntimeState{}
|
||
|
|
}
|
||
|
|
if c.runtime.CompareAndSwap(nil, state) {
|
||
|
|
if client != nil {
|
||
|
|
client.runtimeState.Store(state)
|
||
|
|
}
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.ensureRuntimeState()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ClientConn) ensureLogicalConnRuntimeState() *logicalConnRuntimeState {
|
||
|
|
if c == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if logical := c.logicalView.Load(); logical != nil {
|
||
|
|
return logical.ensureRuntimeState()
|
||
|
|
}
|
||
|
|
if state := c.runtimeState.Load(); state != nil {
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
state := newLogicalConnRuntimeStateFromClient(c)
|
||
|
|
if c.runtimeState.CompareAndSwap(nil, state) {
|
||
|
|
if logical := c.logicalView.Load(); logical != nil {
|
||
|
|
logical.runtime.CompareAndSwap(nil, state)
|
||
|
|
}
|
||
|
|
return state
|
||
|
|
}
|
||
|
|
return c.runtimeState.Load()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ClientConn) syncLegacySessionRuntimeFromState(state *logicalConnRuntimeState) {
|
||
|
|
if c == nil || state == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.sessionRuntime.Store(state.sessionRuntimeSnapshot())
|
||
|
|
}
|