@@ -113,17 +113,36 @@ static int http_put(struct flb_out_doris *ctx,
113113 msgpack_object msg_key ;
114114 msgpack_object msg_val ;
115115
116+ char address [1024 ] = {0 };
117+ int len = 0 ;
118+
116119 /* Get upstream context and connection */
117- if (strcmp (host , ctx -> host ) == 0 && port == ctx -> port ) {
120+ if (strcmp (host , ctx -> host ) == 0 && port == ctx -> port ) { // address in config
118121 u = ctx -> u ;
119122 }
120- else {
121- // TODO cache
122- u = flb_upstream_create (ctx -> u -> base .config ,
123- host ,
124- port ,
125- ctx -> u -> base .flags ,
126- ctx -> u -> base .tls_context );
123+ else { // redirected address
124+ len = snprintf (address , sizeof (address ), "%s:%i" , host , port );
125+ u = flb_hash_table_get_ptr (ctx -> u_pool , address , len );
126+ if (!u ) { // first check
127+ pthread_mutex_lock (& ctx -> mutex ); // lock
128+ u = flb_hash_table_get_ptr (ctx -> u_pool , address , len );
129+ if (!u ) { // second check
130+ u = flb_upstream_create (ctx -> u -> base .config ,
131+ host ,
132+ port ,
133+ ctx -> u -> base .flags ,
134+ ctx -> u -> base .tls_context );
135+ if (u ) {
136+ flb_hash_table_add (ctx -> u_pool , address , len , u , 0 );
137+ }
138+ }
139+ pthread_mutex_unlock (& ctx -> mutex ); // unlock
140+ if (!u ) {
141+ flb_plg_error (ctx -> ins , "no doris be connections available to %s:%i" ,
142+ host , port );
143+ return FLB_RETRY ;
144+ }
145+ }
127146 }
128147 u_conn = flb_upstream_conn_get (u );
129148 if (!u_conn ) {
@@ -276,11 +295,6 @@ static int http_put(struct flb_out_doris *ctx,
276295 /* Release the TCP connection */
277296 flb_upstream_conn_release (u_conn );
278297
279- /* Release flb_upstream */
280- if (u != ctx -> u ) {
281- flb_upstream_destroy (u );
282- }
283-
284298 return out_ret ;
285299}
286300
@@ -341,7 +355,7 @@ static void cb_doris_flush(struct flb_event_chunk *event_chunk,
341355
342356 if (ctx -> add_label ) {
343357 /* {label_prefix}_{db}_{table}_{timestamp}_{uuid} */
344- len = snprintf (label , sizeof (label ) - 1 , "%s_%s_%s_%lu_" , ctx -> label_prefix , ctx -> database , ctx -> table , cfl_time_now () / 1000000000L );
358+ len = snprintf (label , sizeof (label ), "%s_%s_%s_%lu_" , ctx -> label_prefix , ctx -> database , ctx -> table , cfl_time_now () / 1000000000L );
345359 flb_utils_uuid_v4_gen (label + len );
346360 len += 36 ;
347361 }
0 commit comments