130
130
-type consumer () :: # consumer {}.
131
131
132
132
-record (state ,
133
+ <<<<<<< HEAD
133
134
{ client :: brod :client ()
134
135
, client_mref :: reference ()
135
136
, topic :: brod :topic ()
136
137
, consumers = [] :: [consumer ()]
137
138
, cb_module :: module ()
138
139
, cb_state :: cb_state ()
139
140
, message_type :: message | message_set
141
+ =======
142
+ { client :: brod :client ()
143
+ , client_mref :: reference ()
144
+ , topic :: brod :topic ()
145
+ , consumers = [] :: [consumer ()]
146
+ , cb_fun :: cb_fun ()
147
+ , cb_state :: cb_state ()
148
+ , message_type :: message | message_set
149
+ , consumer_config :: list ()
150
+ >>>>>>> topic_subscriber owns its consumers
140
151
}).
141
152
142
153
- type state () :: # state {}.
143
154
144
155
% % delay 2 seconds retry the failed subscription to partiton consumer process
145
156
- define (RESUBSCRIBE_DELAY , 2000 ).
146
157
147
- -define (LO_CMD_START_CONSUMER (ConsumerConfig , CommittedOffsets , Partitions ),
158
+ - define (LO_CMD_START_CONSUMER (CommittedOffsets , Partitions ),
148
159
{'$start_consumer' , ConsumerConfig , CommittedOffsets , Partitions }).
149
160
- define (LO_CMD_SUBSCRIBE_PARTITIONS , '$subscribe_partitions' ).
150
161
@@ -274,6 +285,7 @@ ack(Pid, Partition, Offset) ->
274
285
% %%_* gen_server callbacks =====================================================
275
286
276
287
% % @private
288
+ <<<<<<< HEAD
277
289
- spec init (topic_subscriber_config ()) -> {ok , state ()}.
278
290
init (Config ) ->
279
291
Defaults = #{ message_type => message_set
@@ -290,29 +302,50 @@ init(Config) ->
290
302
, partitions := Partitions
291
303
} = maps :merge (Defaults , Config ),
292
304
{ok , CommittedOffsets , CbState } = CbModule :init (Topic , InitData ),
305
+ =======
306
+ init ({Client , Topic , Partitions , ConsumerConfig ,
307
+ MessageType , CbModule , CbInitArg }) ->
308
+ {ok , CommittedOffsets , CbState } = CbModule :init (Topic , CbInitArg ),
309
+ CbFun = fun (Partition , Msg , CbStateIn ) ->
310
+ CbModule :handle_message (Partition , Msg , CbStateIn )
311
+ end ,
312
+ init ({Client , Topic , Partitions , ConsumerConfig ,
313
+ CommittedOffsets , MessageType , CbFun , CbState });
314
+ init ({Client , Topic , Partitions , ConsumerConfig0 ,
315
+ CommittedOffsets , MessageType , CbFun , CbState }) ->
316
+ >>>>>>> topic_subscriber owns its consumers
293
317
ok = brod_utils :assert_client (Client ),
294
318
ok = brod_utils :assert_topic (Topic ),
295
- self () ! ? LO_CMD_START_CONSUMER (ConsumerConfig , CommittedOffsets , Partitions ),
319
+ ConsumerConfig = [{register_self , false } | ConsumerConfig0 ],
320
+ self () ! ? LO_CMD_START_CONSUMER (CommittedOffsets , Partitions ),
296
321
State =
322
+ <<<<<<< HEAD
297
323
# state { client = Client
298
324
, client_mref = erlang :monitor (process , Client )
299
325
, topic = Topic
300
326
, cb_module = CbModule
301
327
, cb_state = CbState
302
328
, message_type = MessageType
329
+ =======
330
+ # state { client = Client
331
+ , client_mref = erlang :monitor (process , Client )
332
+ , topic = Topic
333
+ , cb_fun = CbFun
334
+ , cb_state = CbState
335
+ , message_type = MessageType
336
+ , consumer_config = ConsumerConfig
337
+ >>>>>>> topic_subscriber owns its consumers
303
338
},
304
339
{ok , State }.
305
340
306
341
% % @private
307
342
handle_info ({_ConsumerPid , # kafka_message_set {} = MsgSet }, State0 ) ->
308
343
State = handle_consumer_delivery (MsgSet , State0 ),
309
344
{noreply , State };
310
- handle_info (? LO_CMD_START_CONSUMER (ConsumerConfig , CommittedOffsets ,
311
- Partitions0 ),
345
+ handle_info (? LO_CMD_START_CONSUMER (CommittedOffsets , Partitions0 ),
312
346
# state { client = Client
313
347
, topic = Topic
314
348
} = State ) ->
315
- ok = brod :start_consumer (Client , Topic , ConsumerConfig ),
316
349
{ok , PartitionsCount } = brod :get_partitions_count (Client , Topic ),
317
350
AllPartitions = lists :seq (0 , PartitionsCount - 1 ),
318
351
Partitions =
@@ -413,15 +446,17 @@ update_last_offset(Partition, Messages,
413
446
Consumer = C # consumer {last_offset = LastOffset },
414
447
State # state {consumers = put_consumer (Consumer , Consumers )}.
415
448
416
- subscribe_partitions (# state { client = Client
417
- , topic = Topic
418
- , consumers = Consumers0
449
+ subscribe_partitions (# state { client = Client
450
+ , topic = Topic
451
+ , consumers = Consumers0
452
+ , consumer_config = ConsumerConfig
419
453
} = State ) ->
420
454
Consumers =
421
- lists :map (fun (C ) -> subscribe_partition (Client , Topic , C ) end , Consumers0 ),
455
+ lists :map ( fun (C ) -> subscribe_partition (Client , Topic , ConsumerConfig , C ) end
456
+ , Consumers0 ),
422
457
{ok , State # state {consumers = Consumers }}.
423
458
424
- subscribe_partition (Client , Topic , Consumer ) ->
459
+ subscribe_partition (Client , Topic , ConsumerConfig , Consumer ) ->
425
460
# consumer { partition = Partition
426
461
, consumer_pid = Pid
427
462
, acked_offset = AckedOffset
@@ -448,9 +483,15 @@ subscribe_partition(Client, Topic, Consumer) ->
448
483
StartOffset >= 0 orelse erlang :error ({invalid_offset , AckedOffset }),
449
484
[{begin_offset , StartOffset }]
450
485
end ,
451
- case brod :subscribe (Client , self (), Topic , Partition , Options ) of
486
+ ClientPid = if is_atom (Client ) -> whereis (Client );
487
+ is_pid (Client ) -> Client
488
+ end ,
489
+ case brod_consumer :start_link (ClientPid , Topic , Partition ,
490
+ ConsumerConfig ) of
452
491
{ok , ConsumerPid } ->
453
492
Mref = erlang :monitor (process , ConsumerPid ),
493
+ unlink (ConsumerPid ),
494
+ ok = brod_consumer :subscribe (ConsumerPid , self (), Options ),
454
495
Consumer # consumer { consumer_pid = ConsumerPid
455
496
, consumer_mref = Mref
456
497
};
0 commit comments