44import functools
55import hashlib
66import hmac
7+ import io
78import logging
89import random
910import socket
2425 SaslAuthenticateRequest ,
2526 SaslHandShakeRequest ,
2627)
27- from aiokafka .protocol .api import RequestHeader
2828from aiokafka .protocol .commit import (
2929 GroupCoordinatorResponse_v0 as GroupCoordinatorResponse ,
3030)
@@ -459,10 +459,8 @@ def send(self, request, expect_response=True):
459459 )
460460
461461 correlation_id = self ._next_correlation_id ()
462- header = RequestHeader (
463- request ,
464- correlation_id = correlation_id ,
465- client_id = self ._client_id ,
462+ header = request .build_request_header (
463+ correlation_id = correlation_id , client_id = self ._client_id
466464 )
467465 message = header .encode () + request .encode ()
468466 size = struct .pack (">i" , len (message ))
@@ -480,7 +478,7 @@ def send(self, request, expect_response=True):
480478 return self ._writer .drain ()
481479 fut = self ._loop .create_future ()
482480 self ._requests .append (
483- (correlation_id , request . RESPONSE_TYPE , fut ),
481+ (correlation_id , request , fut ),
484482 )
485483 return wait_for (fut , self ._request_timeout )
486484
@@ -569,39 +567,41 @@ async def _read(self_ref):
569567 del self
570568
571569 def _handle_frame (self , resp ):
572- correlation_id , resp_type , fut = self ._requests [0 ]
570+ correlation_id , request , fut = self ._requests [0 ]
573571
574572 if correlation_id is None : # Is a SASL packet, just pass it though
575573 if not fut .done ():
576574 fut .set_result (resp )
577575 else :
578- (recv_correlation_id ,) = struct .unpack_from (">i" , resp , 0 )
576+ resp = io .BytesIO (resp )
577+ response_header = request .parse_response_header (resp )
578+ resp_type = request .RESPONSE_TYPE
579579
580580 if (
581581 self ._api_version == (0 , 8 , 2 )
582582 and resp_type is GroupCoordinatorResponse
583583 and correlation_id != 0
584- and recv_correlation_id == 0
584+ and response_header . correlation_id == 0
585585 ):
586586 log .warning (
587587 "Kafka 0.8.2 quirk -- GroupCoordinatorResponse"
588- " coorelation id does not match request. This"
588+ " correlation id does not match request. This"
589589 " should go away once at least one topic has been"
590590 " initialized on the broker"
591591 )
592592
593- elif correlation_id != recv_correlation_id :
593+ elif response_header . correlation_id != correlation_id :
594594 error = Errors .CorrelationIdError (
595595 f"Correlation ids do not match: sent { correlation_id } ,"
596- f" recv { recv_correlation_id } "
596+ f" recv { response_header . correlation_id } "
597597 )
598598 if not fut .done ():
599599 fut .set_exception (error )
600600 self .close (reason = CloseReason .OUT_OF_SYNC )
601601 return
602602
603603 if not fut .done ():
604- response = resp_type .decode (resp [ 4 :] )
604+ response = resp_type .decode (resp )
605605 log .debug ("%s Response %d: %s" , self , correlation_id , response )
606606 fut .set_result (response )
607607
0 commit comments