Skip to content

Commit

Permalink
Free the sampling buffer value on expiry (#72)
Browse files Browse the repository at this point in the history
* Free the sampling buffer value on expiry

This was leaking like a sieve, especially if churning a lot of timers.

* Fix for hashmap delete while iterate
  • Loading branch information
theatrus authored and Yash Kumaraswamy committed Aug 1, 2017
1 parent 504833c commit 8177c4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,15 @@ int hashmap_clear(hashmap *map) {
}

/**
* Iterates through the key/value pairs in the map,
* invoking a callback for each. The call back gets a
* key, value for each and returns an integer stop value.
* If the callback returns 1, then the iteration stops.
* Iterates through the key/value pairs in the map, invoking a
* callback for each. The call back gets a key, value for each and
* returns an integer stop value. If the callback returns 1, then the
* iteration stops.
*
* Calling hashmap_delete is safe (if not inefficient) from this
* function. Calling any insertion function will lead to undefined
* iteration order.
*
* @arg map The hashmap to iterate over
* @arg cb The callback function to invoke
* @arg data Opaque handle passed to the callback
Expand All @@ -377,9 +382,16 @@ int hashmap_iter(hashmap *map, hashmap_callback cb, void *data) {
for (int i = 0; i < map->table_size && !should_break; i++) {
entry = map->table + i;
while (entry && entry->key && !should_break) {
// Invoke the callback
/* Grab the next entry to avoid issues where the entry may
* be freed by the callback. This is pretty inefficient
* way to handle deletes but prevents a class of bugs from
* code that decides to do that anyway
*/
hashmap_entry *next_entry = entry->next;
/* Invoke the callback */
should_break = cb(data, entry->key, entry->value, entry->metadata);
entry = entry->next;

entry = next_entry;
}
}
return should_break;
Expand Down
2 changes: 2 additions & 0 deletions src/sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ static int expiry_callback(void* _s, const char* key, void* _value, void *metada
time_t now = timestamp();

if ((now - bucket->last_modified_at) > (*ttl)) {
if (_value)
free(_value);
hashmap_delete(sampler->map, key);
}

Expand Down

0 comments on commit 8177c4e

Please sign in to comment.