Pārlūkot izejas kodu

H2O: A couple of small changes (#4745)

* Ensure that TLS is not used for database connections
* Make it possible to pass a precalculated key hash value to the
cache functions
Anton Kirilov 6 gadi atpakaļ
vecāks
revīzija
bdbaf049cd

+ 20 - 6
frameworks/C/h2o/src/cache.c

@@ -110,9 +110,14 @@ void cache_destroy(cache_t *cache)
 		assert(!cache->cache_lock);
 }
 
-h2o_cache_ref_t *cache_fetch(cache_t *cache, uint64_t now, h2o_iovec_t key)
+h2o_cache_ref_t *cache_fetch(cache_t *cache,
+                             uint64_t now,
+                             h2o_iovec_t key,
+                             h2o_cache_hashcode_t keyhash)
 {
-	const h2o_cache_hashcode_t keyhash = h2o_cache_calchash(key.base, key.len);
+	if (!keyhash)
+		keyhash = h2o_cache_calchash(key.base, key.len);
+
 	const size_t idx = get_index(cache->cache_num, keyhash);
 	pthread_mutex_t * const mutex = cache->cache_lock + idx;
 
@@ -124,16 +129,25 @@ h2o_cache_ref_t *cache_fetch(cache_t *cache, uint64_t now, h2o_iovec_t key)
 	return ret;
 }
 
-void cache_release(cache_t *cache, h2o_cache_ref_t *ref)
+void cache_release(cache_t *cache, h2o_cache_ref_t *ref, h2o_cache_hashcode_t keyhash)
 {
-	const size_t idx = get_index(cache->cache_num, h2o_cache_calchash(ref->key.base, ref->key.len));
+	if (!keyhash)
+		keyhash = h2o_cache_calchash(ref->key.base, ref->key.len);
+
+	const size_t idx = get_index(cache->cache_num, keyhash);
 
 	h2o_cache_release(cache->cache[idx], ref);
 }
 
-int cache_set(uint64_t now, h2o_iovec_t key, h2o_iovec_t value, cache_t *cache)
+int cache_set(uint64_t now,
+              h2o_iovec_t key,
+              h2o_cache_hashcode_t keyhash,
+              h2o_iovec_t value,
+              cache_t *cache)
 {
-	const h2o_cache_hashcode_t keyhash = h2o_cache_calchash(key.base, key.len);
+	if (!keyhash)
+		keyhash = h2o_cache_calchash(key.base, key.len);
+
 	const size_t idx = get_index(cache->cache_num, keyhash);
 	pthread_mutex_t * const mutex = cache->cache_lock + idx;
 

+ 10 - 3
frameworks/C/h2o/src/cache.h

@@ -37,8 +37,15 @@ int cache_create(size_t concurrency,
                  void (*destroy_cb)(h2o_iovec_t value),
                  cache_t *cache);
 void cache_destroy(cache_t *cache);
-h2o_cache_ref_t *cache_fetch(cache_t *cache, uint64_t now, h2o_iovec_t key);
-void cache_release(cache_t *cache, h2o_cache_ref_t *ref);
-int cache_set(uint64_t now, h2o_iovec_t key, h2o_iovec_t value, cache_t *cache);
+h2o_cache_ref_t *cache_fetch(cache_t *cache,
+                             uint64_t now,
+                             h2o_iovec_t key,
+                             h2o_cache_hashcode_t keyhash);
+void cache_release(cache_t *cache, h2o_cache_ref_t *ref, h2o_cache_hashcode_t keyhash);
+int cache_set(uint64_t now,
+              h2o_iovec_t key,
+              h2o_cache_hashcode_t keyhash,
+              h2o_iovec_t value,
+              cache_t *cache);
 
 #endif // CACHE_H_

+ 4 - 2
frameworks/C/h2o/src/handlers/world.c

@@ -373,14 +373,15 @@ static void fetch_from_cache(uint64_t now, cache_t *world_cache, multiple_query_
 	for (size_t i = 0; i < query_ctx->num_query; i++) {
 		key.base = (char *) &query_ctx->res[i].id;
 
-		h2o_cache_ref_t * const r = cache_fetch(world_cache, now, key);
+		const h2o_cache_hashcode_t keyhash = h2o_cache_calchash(key.base, key.len);
+		h2o_cache_ref_t * const r = cache_fetch(world_cache, now, key, keyhash);
 
 		if (r) {
 			query_ctx->res[i].id = query_ctx->res[query_ctx->num_result].id;
 			memcpy(query_ctx->res + query_ctx->num_result++,
 			       r->value.base,
 			       sizeof(*query_ctx->res));
-			cache_release(world_cache, r);
+			cache_release(world_cache, r, keyhash);
 		}
 	}
 }
@@ -477,6 +478,7 @@ static result_return_t on_multiple_query_result(db_query_param_t *param, PGresul
 				*r = query_ctx->res[query_ctx->num_result];
 				cache_set(h2o_now(query_ctx->ctx->event_loop.h2o_ctx.loop),
 				          key,
+				          0,
 				          value,
 				          &query_ctx->ctx->global_data->request_handler_data.world_cache);
 			}

+ 1 - 1
frameworks/C/h2o/start-servers.sh

@@ -39,7 +39,7 @@ run_curl()
 run_h2o_app()
 {
 	taskset -c "$1" "$2/h2o_app" -a20 -f "$3/template" -m "$DB_CONN" "$4" "$5" \
-	        -d "host=tfb-database dbname=hello_world user=benchmarkdbuser \
+	        -d "host=tfb-database dbname=hello_world user=benchmarkdbuser sslmode=disable \
 	            password=benchmarkdbpass" &
 }