127127 , inflight_calls := non_neg_integer ()
128128 , topic := topic ()
129129 , calls := ? EMPTY | calls ()
130+ , sender => pid ()
130131 }.
131132
132133% % @doc Start a per-partition producer worker.
@@ -231,7 +232,8 @@ send_sync(Pid, Batch0, Timeout) ->
231232init (#{client_id := ClientId , topic := Topic , partition := Partition } = St ) ->
232233 erlang :process_flag (trap_exit , true ),
233234 ok = set_process_label (ClientId , Topic , Partition ),
234- {ok , St , {continue , do_init }}.
235+ {ok , Sender } = wolff_sender :start_link (self (), ClientId , Topic , Partition ),
236+ {ok , St #{sender => Sender }, {continue , do_init }}.
235237
236238do_init (#{client_id := ClientId ,
237239 topic := Topic ,
@@ -423,10 +425,12 @@ use_defaults(Config, [{K, V} | Rest]) ->
423425 false -> use_defaults (Config #{K => V }, Rest )
424426 end .
425427
426- resend_requests (#{q_items := Items } = Sent , St , ? reconnect ) ->
428+ resend_requests (#{q_items := Items , req_ref := {alias , OldRef }} = Sent , St , ? reconnect ) ->
429+ erlang :unalias (OldRef ),
427430 {ok , Ref } = send_request (Items , St ),
428431 [Sent #{req_ref := Ref }];
429- resend_requests (#{q_items := Items } = Sent , St , ? message_too_large ) ->
432+ resend_requests (#{q_items := Items , req_ref := {alias , OldRef }} = Sent , St , ? message_too_large ) ->
433+ erlang :unalias (OldRef ),
430434 Refs = lists :map (fun (Item ) -> {ok , Ref } = send_request ([Item ], St ), Ref end , Items ),
431435 #{attempts := Attempts , q_ack_ref := Q_AckRef } = Sent ,
432436 make_sent_items_list (Items , Refs , Attempts , Q_AckRef ).
@@ -445,25 +449,25 @@ make_sent_items_list([Item | Items], [Ref | Refs], Attempts, Q_AckRef) ->
445449 attempts => Attempts
446450 } | make_sent_items_list (Items , Refs , Attempts , Q_AckRef )].
447451
448- send_request (QueueItems , #{conn := Conn } = St ) ->
449- # kpro_req {ref = Ref } = Req = make_request (QueueItems , St ),
450- ok = request_async (Conn , Req ),
451- {ok , Ref }.
452-
453- make_request (QueueItems ,
452+ send_request (QueueItems ,
454453 #{config := #{required_acks := RequiredAcks ,
455454 ack_timeout := AckTimeout ,
456455 compression := Compression
457456 },
458457 produce_api_vsn := Vsn ,
459458 topic := Topic ,
460- partition := Partition }) ->
459+ partition := Partition ,
460+ conn := Conn ,
461+ sender := Sender
462+ }) ->
461463 Batch = get_batch_from_queue_items (QueueItems ),
462- kpro_req_lib :produce (Vsn , Topic , Partition , Batch ,
463- #{ack_timeout => AckTimeout ,
464- required_acks => RequiredAcks ,
465- compression => Compression
466- }).
464+ Opts = #{ack_timeout => AckTimeout ,
465+ required_acks => RequiredAcks ,
466+ compression => Compression
467+ },
468+ Ref = {alias , erlang :alias ([reply ])},
469+ ok = wolff_sender :do (Sender , Conn , Ref , Vsn , Topic , Partition , Batch , Opts ),
470+ {ok , Ref }.
467471
468472resend_sent_reqs (#{sent_reqs := SentReqs ,
469473 config := Config
@@ -504,7 +508,6 @@ send_to_kafka(#{sent_reqs := SentReqs,
504508 replayq := Q ,
505509 config := #{required_acks := RequiredAcks ,
506510 max_batch_bytes := BytesLimit } = Config ,
507- conn := Conn ,
508511 pending_acks := PendingAcks
509512 } = St0 ) ->
510513 {NewQ , QAckRef , Items } =
@@ -518,7 +521,7 @@ send_to_kafka(#{sent_reqs := SentReqs,
518521 NewInflightCalls = InflightCalls + NrOfCalls ,
519522 _ = wolff_metrics :inflight_set (Config , NewInflightCalls ),
520523 NoAck = (RequiredAcks =:= none ),
521- # kpro_req { ref = Ref } = Req = make_request (Items , St0 ),
524+ { ok , Ref } = send_request (Items , St0 ),
522525 Sent = #{req_ref => Ref ,
523526 q_items => Items ,
524527 q_ack_ref => QAckRef ,
@@ -530,7 +533,6 @@ send_to_kafka(#{sent_reqs := SentReqs,
530533 inflight_calls := NewInflightCalls ,
531534 pending_acks := NewPendingAcks
532535 },
533- ok = request_async (Conn , Req ),
534536 St2 = maybe_fake_kafka_ack (NoAck , St1 ),
535537 maybe_send_to_kafka (St2 ).
536538
@@ -876,9 +878,6 @@ varint_bytes(N) -> vb(zz(N)).
876878-compile ({inline , zz / 1 }).
877879zz (I ) -> (I bsl 1 ) bxor (I bsr 63 ).
878880
879- request_async (Conn , Req ) when is_pid (Conn ) ->
880- ok = kpro :send (Conn , Req ).
881-
882881% % collect send calls which are already sent to mailbox,
883882% % the collection is size-limited by the max_linger_bytes config.
884883collect_send_calls (Call , Bytes , ? EMPTY , Max ) ->
0 commit comments