@@ -127,6 +127,63 @@ int network_backends_remove(network_backends_t *bs, guint index) {
127127 return 0 ;
128128}
129129
130+ void copy_key (guint * key , guint * value , GHashTable * table ) {
131+ guint * new_key = g_new0 (guint , 1 );
132+ * new_key = * key ;
133+ g_hash_table_add (table , new_key );
134+ }
135+
136+ int network_backends_addclient (network_backends_t * bs , gchar * address ) {
137+ guint * sum = g_new0 (guint , 1 );
138+ char * token ;
139+ while ((token = strsep (& address , "." )) != NULL ) {
140+ * sum = (* sum << 8 ) + atoi (token );
141+ }
142+ * sum = htonl (* sum );
143+
144+ gint index = * (bs -> ip_table_index );
145+ GHashTable * old_table = bs -> ip_table [index ];
146+ GHashTable * new_table = bs -> ip_table [1 - index ];
147+ g_hash_table_remove_all (new_table );
148+ g_hash_table_foreach (old_table , copy_key , new_table );
149+ g_hash_table_add (new_table , sum );
150+ g_atomic_int_set (bs -> ip_table_index , 1 - index );
151+
152+ return 0 ;
153+ }
154+
155+ int network_backends_removeclient (network_backends_t * bs , gchar * address ) {
156+ guint sum ;
157+ char * token ;
158+ while ((token = strsep (& address , "." )) != NULL ) {
159+ sum = (sum << 8 ) + atoi (token );
160+ }
161+ sum = htonl (sum );
162+
163+ gint index = * (bs -> ip_table_index );
164+ GHashTable * old_table = bs -> ip_table [index ];
165+ GHashTable * new_table = bs -> ip_table [1 - index ];
166+ g_hash_table_remove_all (new_table );
167+ g_hash_table_foreach (old_table , copy_key , new_table );
168+ g_hash_table_remove (new_table , & sum );
169+ g_atomic_int_set (bs -> ip_table_index , 1 - index );
170+
171+ return 0 ;
172+ }
173+
174+ void append_key (guint * key , guint * value , GString * str ) {
175+ g_string_append_c (str , ',' );
176+ guint sum = * key ;
177+
178+ g_string_append_printf (str , "%u" , sum & 0x000000FF );
179+
180+ guint i ;
181+ for (i = 1 ; i <= 3 ; ++ i ) {
182+ sum >>= 8 ;
183+ g_string_append_printf (str , ".%u" , sum & 0x000000FF );
184+ }
185+ }
186+
130187int network_backends_save (network_backends_t * bs ) {
131188 GKeyFile * keyfile = g_key_file_new ();
132189 g_key_file_set_list_separator (keyfile , ',' );
@@ -172,6 +229,18 @@ int network_backends_save(network_backends_t *bs) {
172229 g_string_free (master , TRUE);
173230 g_string_free (slave , TRUE);
174231
232+ GString * client_ips = g_string_new (NULL );
233+ GHashTable * ip_table = bs -> ip_table [* (bs -> ip_table_index )];
234+ g_hash_table_foreach (ip_table , append_key , client_ips );
235+
236+ if (client_ips -> len != 0 ) {
237+ g_key_file_set_value (keyfile , "mysql-proxy" , "client-ips" , client_ips -> str + 1 );
238+ } else {
239+ g_key_file_set_value (keyfile , "mysql-proxy" , "client-ips" , "" );
240+ }
241+
242+ g_string_free (client_ips , TRUE);
243+
175244 gsize file_size = 0 ;
176245 gchar * file_buf = g_key_file_to_data (keyfile , & file_size , NULL );
177246 if (FALSE == g_file_set_contents (bs -> default_file , file_buf , file_size , & gerr )) {
@@ -199,9 +268,10 @@ int network_backends_add(network_backends_t *bs, /* const */ gchar *address, bac
199268 new_backend = network_backend_new (bs -> event_thread_count );
200269 new_backend -> type = type ;
201270
271+ gchar * p = NULL ;
202272 if (type == BACKEND_TYPE_RO ) {
203273 guint weight = 1 ;
204- gchar * p = strrchr (address , '@' );
274+ p = strrchr (address , '@' );
205275 if (p != NULL ) {
206276 * p = '\0' ;
207277 weight = atoi (p + 1 );
@@ -239,8 +309,9 @@ int network_backends_add(network_backends_t *bs, /* const */ gchar *address, bac
239309 }
240310 g_mutex_unlock (bs -> backends_mutex ); /*remove lock*/
241311
242- g_message ("added %s backend: %s" , (type == BACKEND_TYPE_RW ) ?
243- "read/write" : "read-only" , address );
312+ g_message ("added %s backend: %s" , (type == BACKEND_TYPE_RW ) ? "read/write" : "read-only" , address );
313+
314+ if (p != NULL ) * p = '@' ;
244315
245316 return 0 ;
246317}
0 commit comments