ax(node): rename local variable pc to connection in transport.go
AX Principle 1: predictable names over short names. The abbreviation `pc` (PeerConnection) in local variable scope is ambiguous — renamed to `connection` in Connect, Stop, Send, Broadcast, and handleWSUpgrade. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
45722a230c
commit
bfdf3be4fd
1 changed files with 27 additions and 27 deletions
|
|
@ -164,9 +164,9 @@ func (limiter *PeerRateLimiter) Allow() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// pc := transport.connections[peer.ID]
|
||||
// if err := pc.Send(msg); err != nil { logging.Error("send failed", ...) }
|
||||
// pc.GracefulClose("shutdown", DisconnectShutdown)
|
||||
// connection := transport.connections[peer.ID]
|
||||
// if err := connection.Send(msg); err != nil { logging.Error("send failed", ...) }
|
||||
// connection.GracefulClose("shutdown", DisconnectShutdown)
|
||||
type PeerConnection struct {
|
||||
Peer *Peer
|
||||
Conn *websocket.Conn
|
||||
|
|
@ -291,8 +291,8 @@ func (transport *Transport) Stop() error {
|
|||
|
||||
// Gracefully close all connections with shutdown message
|
||||
transport.mutex.Lock()
|
||||
for _, pc := range transport.connections {
|
||||
pc.GracefulClose("server shutdown", DisconnectShutdown)
|
||||
for _, connection := range transport.connections {
|
||||
connection.GracefulClose("server shutdown", DisconnectShutdown)
|
||||
}
|
||||
transport.mutex.Unlock()
|
||||
|
||||
|
|
@ -317,7 +317,7 @@ func (transport *Transport) OnMessage(handler MessageHandler) {
|
|||
transport.handler = handler
|
||||
}
|
||||
|
||||
// pc, err := transport.Connect(peer)
|
||||
// connection, err := transport.Connect(peer)
|
||||
// if err != nil { return fmt.Errorf("connect failed: %w", err) }
|
||||
func (transport *Transport) Connect(peer *Peer) (*PeerConnection, error) {
|
||||
// Build WebSocket URL
|
||||
|
|
@ -336,7 +336,7 @@ func (transport *Transport) Connect(peer *Peer) (*PeerConnection, error) {
|
|||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "failed to connect to peer: " + err.Error()}
|
||||
}
|
||||
|
||||
pc := &PeerConnection{
|
||||
connection := &PeerConnection{
|
||||
Peer: peer,
|
||||
Conn: conn,
|
||||
LastActivity: time.Now(),
|
||||
|
|
@ -345,46 +345,46 @@ func (transport *Transport) Connect(peer *Peer) (*PeerConnection, error) {
|
|||
}
|
||||
|
||||
// Perform handshake with challenge-response authentication
|
||||
// This also derives and stores the shared secret in pc.SharedSecret
|
||||
if err := transport.performHandshake(pc); err != nil {
|
||||
// This also derives and stores the shared secret in connection.SharedSecret
|
||||
if err := transport.performHandshake(connection); err != nil {
|
||||
conn.Close()
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "handshake failed: " + err.Error()}
|
||||
}
|
||||
|
||||
// Store connection using the real peer ID from handshake
|
||||
transport.mutex.Lock()
|
||||
transport.connections[pc.Peer.ID] = pc
|
||||
transport.connections[connection.Peer.ID] = connection
|
||||
transport.mutex.Unlock()
|
||||
|
||||
logging.Debug("connected to peer", logging.Fields{"peer_id": pc.Peer.ID, "secret_len": len(pc.SharedSecret)})
|
||||
logging.Debug("connected to peer", logging.Fields{"peer_id": connection.Peer.ID, "secret_len": len(connection.SharedSecret)})
|
||||
|
||||
// Update registry
|
||||
transport.registry.SetConnected(pc.Peer.ID, true)
|
||||
transport.registry.SetConnected(connection.Peer.ID, true)
|
||||
|
||||
// Start read loop
|
||||
transport.waitGroup.Add(1)
|
||||
go transport.readLoop(pc)
|
||||
go transport.readLoop(connection)
|
||||
|
||||
logging.Debug("started readLoop for peer", logging.Fields{"peer_id": pc.Peer.ID})
|
||||
logging.Debug("started readLoop for peer", logging.Fields{"peer_id": connection.Peer.ID})
|
||||
|
||||
// Start keepalive
|
||||
transport.waitGroup.Add(1)
|
||||
go transport.keepalive(pc)
|
||||
go transport.keepalive(connection)
|
||||
|
||||
return pc, nil
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
// if err := transport.Send(peer.ID, msg); err != nil { return err }
|
||||
func (transport *Transport) Send(peerID string, msg *Message) error {
|
||||
transport.mutex.RLock()
|
||||
pc, exists := transport.connections[peerID]
|
||||
connection, exists := transport.connections[peerID]
|
||||
transport.mutex.RUnlock()
|
||||
|
||||
if !exists {
|
||||
return &ProtocolError{Code: ErrCodeNotFound, Message: "peer " + peerID + " not connected"}
|
||||
}
|
||||
|
||||
return pc.Send(msg)
|
||||
return connection.Send(msg)
|
||||
}
|
||||
|
||||
// if err := transport.Broadcast(msg); err != nil { logging.Warn("broadcast partial failure", ...) }
|
||||
|
|
@ -392,18 +392,18 @@ func (transport *Transport) Send(peerID string, msg *Message) error {
|
|||
func (transport *Transport) Broadcast(msg *Message) error {
|
||||
transport.mutex.RLock()
|
||||
connections := make([]*PeerConnection, 0, len(transport.connections))
|
||||
for _, pc := range transport.connections {
|
||||
for _, connection := range transport.connections {
|
||||
// Exclude sender from broadcast to prevent echo (P2P-MED-6)
|
||||
if pc.Peer != nil && pc.Peer.ID == msg.From {
|
||||
if connection.Peer != nil && connection.Peer.ID == msg.From {
|
||||
continue
|
||||
}
|
||||
connections = append(connections, pc)
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
transport.mutex.RUnlock()
|
||||
|
||||
var lastErr error
|
||||
for _, pc := range connections {
|
||||
if err := pc.Send(msg); err != nil {
|
||||
for _, connection := range connections {
|
||||
if err := connection.Send(msg); err != nil {
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
|
|
@ -550,7 +550,7 @@ func (transport *Transport) handleWSUpgrade(responseWriter http.ResponseWriter,
|
|||
})
|
||||
}
|
||||
|
||||
pc := &PeerConnection{
|
||||
connection := &PeerConnection{
|
||||
Peer: peer,
|
||||
Conn: conn,
|
||||
SharedSecret: sharedSecret,
|
||||
|
|
@ -598,7 +598,7 @@ func (transport *Transport) handleWSUpgrade(responseWriter http.ResponseWriter,
|
|||
|
||||
// Store connection
|
||||
transport.mutex.Lock()
|
||||
transport.connections[peer.ID] = pc
|
||||
transport.connections[peer.ID] = connection
|
||||
transport.mutex.Unlock()
|
||||
|
||||
// Update registry
|
||||
|
|
@ -606,11 +606,11 @@ func (transport *Transport) handleWSUpgrade(responseWriter http.ResponseWriter,
|
|||
|
||||
// Start read loop
|
||||
transport.waitGroup.Add(1)
|
||||
go transport.readLoop(pc)
|
||||
go transport.readLoop(connection)
|
||||
|
||||
// Start keepalive
|
||||
transport.waitGroup.Add(1)
|
||||
go transport.keepalive(pc)
|
||||
go transport.keepalive(connection)
|
||||
}
|
||||
|
||||
// if err := transport.performHandshake(pc); err != nil { conn.Close(); return nil, err }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue