<commit_before>package uk.gov.homeoffice.io

import java.net.{BindException, ServerSocket}
import grizzled.slf4j.Logging

trait Network extends Logging {
  type Port = Int

  def freeport[R](retries: Int = 5)(f: Port => R): R = {
    require(retries >= 0)

    def freeport(retries: Int = 5, retry: Int)(f: Port => R): R = try {
      f(port)
    } catch {
      case e: BindException if retry < retries => freeport(retries, retry + 1)(f)
    }

    freeport(retries, 0)(f)
  }

  def port: Int = {
    val port = new ServerSocket(0).getLocalPort
    info(s"Attempting to use free port $port")
    port
  }
}<commit_msg>Simple free port functionality with retry to handle race conditions.<commit_after>package uk.gov.homeoffice.io

import java.net.{BindException, ServerSocket}
import grizzled.slf4j.Logging

trait Network extends Logging {
  type Port = Int

  def freeport[R](retries: Int = 5)(f: Port => R): R = {
    require(retries >= 0)

    def port: Int = {
      val port = new ServerSocket(0).getLocalPort
      info(s"Attempting to use free port $port")
      port
    }

    def freeport(retries: Int = 5, retry: Int)(f: Port => R): R = try {
      f(port)
    } catch {
      case e: BindException if retry < retries => freeport(retries, retry + 1)(f)
    }

    freeport(retries, 0)(f)
  }
}
{'fraction_non_alphanumeric': 0.12643678160919541, 'fraction_numerical': 0.010727969348659003, 'mean_word_length': 3.068535825545171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5038179', 'n_tokens_mistral': 468, 'n_tokens_neox': 454, 'n_words': 129}