refactor: streamline request handling logic by consolidating type checks for requested_at and request_payload, and rename max function for clarity
This commit is contained in:
@@ -1016,11 +1016,11 @@ func (l *Link) handleRequest(plaintext []byte, pkt *packet.Packet) error {
|
|||||||
|
|
||||||
requestedAtFloat, ok := requestData[common.ZERO].(float64)
|
requestedAtFloat, ok := requestData[common.ZERO].(float64)
|
||||||
if !ok {
|
if !ok {
|
||||||
if requestedAtInt, ok := requestData[common.ZERO].(int64); ok {
|
requestedAtInt, ok := requestData[common.ZERO].(int64)
|
||||||
requestedAtFloat = float64(requestedAtInt)
|
if !ok {
|
||||||
} else {
|
|
||||||
return fmt.Errorf("invalid requested_at type: %T", requestData[common.ZERO])
|
return fmt.Errorf("invalid requested_at type: %T", requestData[common.ZERO])
|
||||||
}
|
}
|
||||||
|
requestedAtFloat = float64(requestedAtInt)
|
||||||
}
|
}
|
||||||
requestedAt := time.Unix(int64(requestedAtFloat), 0)
|
requestedAt := time.Unix(int64(requestedAtFloat), 0)
|
||||||
|
|
||||||
@@ -1031,11 +1031,11 @@ func (l *Link) handleRequest(plaintext []byte, pkt *packet.Packet) error {
|
|||||||
|
|
||||||
var requestPayload []byte
|
var requestPayload []byte
|
||||||
if requestData[common.TWO] != nil {
|
if requestData[common.TWO] != nil {
|
||||||
if payload, ok := requestData[common.TWO].([]byte); ok {
|
payload, ok := requestData[common.TWO].([]byte)
|
||||||
requestPayload = payload
|
if !ok {
|
||||||
} else {
|
|
||||||
return fmt.Errorf("invalid request_payload type: %T", requestData[common.TWO])
|
return fmt.Errorf("invalid request_payload type: %T", requestData[common.TWO])
|
||||||
}
|
}
|
||||||
|
requestPayload = payload
|
||||||
}
|
}
|
||||||
|
|
||||||
requestID := pkt.TruncatedHash()
|
requestID := pkt.TruncatedHash()
|
||||||
@@ -1143,7 +1143,7 @@ func (l *Link) handleRTTPacket(pkt *packet.Packet) error {
|
|||||||
rtt = float64(binary.BigEndian.Uint64(plaintext[:common.EIGHT])) / common.FLOAT_1E9
|
rtt = float64(binary.BigEndian.Uint64(plaintext[:common.EIGHT])) / common.FLOAT_1E9
|
||||||
}
|
}
|
||||||
|
|
||||||
l.rtt = max(measuredRTT, rtt)
|
l.rtt = maxFloat(measuredRTT, rtt)
|
||||||
l.status = STATUS_ACTIVE
|
l.status = STATUS_ACTIVE
|
||||||
l.establishedAt = time.Now()
|
l.establishedAt = time.Now()
|
||||||
|
|
||||||
@@ -1207,7 +1207,7 @@ func (l *Link) handleTeardown(plaintext []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b float64) float64 {
|
func maxFloat(a, b float64) float64 {
|
||||||
if a > b {
|
if a > b {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
@@ -1364,10 +1364,7 @@ func (l *Link) Resend(pkt interface{}) error {
|
|||||||
return errors.New("invalid packet type")
|
return errors.New("invalid packet type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := l.transport.SendPacket(packetObj); err != nil {
|
return l.transport.SendPacket(packetObj)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) GetLinkID() []byte {
|
func (l *Link) GetLinkID() []byte {
|
||||||
|
|||||||
Reference in New Issue
Block a user