1
- var net = require ( 'net' ) ;
2
1
var EventEmitter = require ( 'events' ) . EventEmitter ;
3
2
var _ = require ( 'underscore' ) ;
4
3
var Message = require ( './Message' ) ;
5
4
var ProduceRequest = require ( './ProduceRequest' ) ;
5
+ var Connection = require ( './Connection' ) ;
6
+ var ConnectionCache = require ( './ConnectionCache' ) ;
6
7
7
8
var Producer = function ( topic , options ) {
8
9
if ( ! topic || ( ! _ . isString ( topic ) ) ) {
@@ -14,67 +15,38 @@ var Producer = function(topic, options){
14
15
this . partition = options . partition || 0 ;
15
16
this . host = options . host || 'localhost' ;
16
17
this . port = options . port || 9092 ;
18
+ this . useConnectionCache = options . connectionCache ;
17
19
18
20
this . connection = null ;
19
- this . connecting = false ;
20
21
} ;
21
22
22
23
Producer . prototype = Object . create ( EventEmitter . prototype ) ;
23
24
24
25
Producer . prototype . connect = function ( ) {
25
26
var that = this ;
26
- this . connecting = true ;
27
- this . connection = net . createConnection ( this . port , this . host ) ;
28
- this . connection . setKeepAlive ( true , 1000 ) ;
27
+ if ( this . useConnectionCache ) {
28
+ this . connection = Producer . _connectionCache . getConnection ( this . port , this . host ) ;
29
+ } else {
30
+ this . connection = new Connection ( this . port , this . host ) ;
31
+ }
29
32
this . connection . once ( 'connect' , function ( ) {
30
- that . connecting = false ;
31
33
that . emit ( 'connect' ) ;
32
34
} ) ;
33
- this . connection . once ( 'error' , function ( err ) {
34
- if ( ! ! err . message && err . message === 'connect ECONNREFUSED' ) {
35
+ this . connection . on ( 'error' , function ( err ) {
36
+ if ( ! ! err . message && err . message === 'connect ECONNREFUSED' ) {
35
37
that . emit ( 'error' , err ) ;
36
- that . connecting = false ;
37
38
}
38
39
} ) ;
40
+ this . connection . connect ( ) ;
39
41
} ;
40
42
41
- Producer . prototype . _reconnect = function ( cb ) {
42
- var producer = this ;
43
-
44
- var onConnect = function ( ) {
45
- producer . removeListener ( 'brokerReconnectError' , onBrokerReconnectError ) ;
46
- return cb ( ) ;
47
- } ;
48
- producer . once ( 'connect' , onConnect ) ;
49
-
50
- var onBrokerReconnectError = function ( err ) {
51
- producer . removeListener ( 'connect' , onConnect ) ;
52
- return cb ( 'brokerReconnectError' ) ;
53
- } ;
54
- producer . once ( 'brokerReconnectError' , onBrokerReconnectError ) ;
55
-
56
- if ( ! producer . connecting ) {
57
- producer . connect ( ) ;
58
- producer . connection . on ( 'error' , function ( err ) {
59
- if ( ! ! err . message && err . message === 'connect ECONNREFUSED' ) {
60
- producer . emit ( "brokerReconnectError" , err ) ;
61
- } else {
62
- return cb ( err ) ;
63
- }
64
- } ) ;
65
- } else {
66
- // reconnect already in progress. wait.
67
- }
68
- } ;
69
-
70
-
71
43
Producer . prototype . send = function ( messages , options , cb ) {
72
44
var that = this ;
73
45
if ( arguments . length === 2 ) {
74
46
// "options" is not a required parameter, so handle the
75
47
// case when it's not set.
76
48
cb = options ;
77
- options = { } ;
49
+ options = { } ;
78
50
}
79
51
if ( ! cb || ( typeof cb != 'function' ) ) {
80
52
throw "A callback with an error parameter must be supplied" ;
@@ -83,20 +55,14 @@ Producer.prototype.send = function(messages, options, cb) {
83
55
options . topic = options . topic || this . topic ;
84
56
messages = toListOfMessages ( toArray ( messages ) ) ;
85
57
var request = new ProduceRequest ( options . topic , options . partition , messages ) ;
86
- this . connection . write ( request . toBytes ( ) , function ( err ) {
87
- if ( ! ! err && err . message === 'This socket is closed.' ) {
88
- that . _reconnect ( function ( err ) {
89
- if ( err ) {
90
- return cb ( err ) ;
91
- }
92
- that . connection . write ( request . toBytes ( ) , function ( err ) {
93
- return cb ( err ) ;
94
- } ) ;
95
- } ) ;
96
- } else {
97
- cb ( err ) ;
98
- }
99
- } ) ;
58
+
59
+ this . connection . write ( request . toBytes ( ) , cb ) ;
60
+ } ;
61
+
62
+ Producer . _connectionCache = new ConnectionCache ( ) ;
63
+
64
+ Producer . clearConnectionCache = function ( ) {
65
+ Producer . _connectionCache . clear ( ) ;
100
66
} ;
101
67
102
68
module . exports = Producer ;
0 commit comments