How to overwrite wp_unique_filename logic

This doesn’t answer the specific issue I was having but does resolve the problem. The cause of my issue was that wp_unique_filename was taking longer and longer as the month went on when we had more images in our media library. We were already namespacing them with year/month dir structure.

I ended up adding a filter to override the upload dir to be year/month/day with the following code:

function upload_dir_filter($uploads)
{
    $day = date('d');
    $uploads['path'] .= "https://wordpress.stackexchange.com/" . $day;
    $uploads['url']  .= "https://wordpress.stackexchange.com/" . $day;
    return $uploads;
}
add_filter('upload_dir', 'upload_dir_filter');

This fixed our problem.