[
  {
    "path": ".github/FUNDING.yml",
    "content": "# These are supported funding model platforms\n\ngithub: [panjf2000]\npatreon: panjf2000\nopen_collective: panjf2000\nko_fi: # Replace with a single Ko-fi username\ntidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel\ncommunity_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry\nliberapay: # Replace with a single Liberapay username\nissuehunt: # Replace with a single IssueHunt username\notechie: # Replace with a single Otechie username\ncustom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug-report.yaml",
    "content": "name: Bug Report\ndescription: Oops!..., it's a bug.\ntitle: \"[Bug]: \"\nlabels: [\"bug\"]\nassignees:\n  - panjf2000\nbody:\n  - type: markdown\n    id: tips\n    attributes:\n      value: |\n        ## Before you go any further\n        - Please read [<u>*How To Ask Questions The Smart Way*</u>](http://www.catb.org/~esr/faqs/smart-questions.html) ( Chinese translation: [《提问的智慧》](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md)) before you file an issue formally.\n        - Keep in mind that there is always welcome to ask questions on [Discord](https://discord.gg/Cuy7KPaWQc).\n  - type: checkboxes\n    id: checklist\n    attributes:\n      label: Actions I've taken before I'm here\n      description: Make sure you have tried the following ways but still the problem has not been solved.\n      options:\n        - label: I've thoroughly read the documentations on this issue but still have no clue.\n          required: true\n        - label: I've searched the current list of Github issues but didn't find any duplicate issues that have been solved.\n          required: true\n        - label: I've searched the internet with this issue, but haven't found anything helpful.\n          required: true\n    validations:\n      required: true\n  - type: textarea\n    id: bug-report\n    attributes:\n      label: What happened?\n      description: Describe (and illustrate) the bug that you encountered precisely.\n      placeholder: Please describe what happened and how it happened, the more details you provide, the faster the bug gets fixed.\n    validations:\n      required: true\n  - type: dropdown\n    id: major-version\n    attributes:\n      label: Major version of ants\n      description: What major version of ants are you running?\n      options:\n        - v2\n        - v1\n    validations:\n      required: true\n  - type: input\n    id: specific-version\n    attributes:\n      label: Specific version of ants\n      description: What's the specific version of ants?\n      placeholder: \"For example: v2.6.0\"\n    validations:\n      required: true\n  - type: dropdown\n    id: os\n    attributes:\n      label: Operating system\n      multiple: true\n      options:\n        - Linux\n        - macOS\n        - Windows\n        - BSD\n    validations:\n      required: true\n  - type: input\n    id: os-version\n    attributes:\n      label: OS version\n      description: What's the specific version of OS?\n      placeholder: \"Run `uname -srm` command to get the info, for example: Darwin 21.5.0 arm64, Linux 5.4.0-137-generic x86_64\"\n    validations:\n      required: true\n  - type: input\n    id: go-version\n    attributes:\n      label: Go version\n      description: What's the specific version of Go?\n      placeholder: \"Run `go version` command to get the info, for example: go1.20.5 linux/amd64\"\n    validations:\n      required: true\n  - type: textarea\n    id: logs\n    attributes:\n      label: Relevant log output\n      description: Please copy and paste any relevant log output.\n      render: shell\n    validations:\n      required: true\n  - type: textarea\n    id: code\n    attributes:\n      label: Reproducer\n      description: Please provide the minimal code to reproduce the bug.\n      render: go\n  - type: textarea\n    id: how-to-reproduce\n    attributes:\n      label: How to Reproduce\n      description: Steps to reproduce the result.\n      placeholder: Tell us step by step how we can replicate this bug and what we should see in the end.\n      value: |\n        Steps to reproduce the behavior:\n        1. Go to '....'\n        2. Click on '....'\n        3. Do '....'\n        4. See '....'\n    validations:\n      required: true\n  - type: dropdown\n    id: bug-in-latest-code\n    attributes:\n      label: Does this issue reproduce with the latest release?\n      description: Is this bug still present in the latest version?\n      options:\n        - It can reproduce with the latest release\n        - It gets fixed in the latest release\n        - I haven't verified it with the latest release\n    validations:\n      required: true\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature-request.yaml",
    "content": "name: Feature Request\ndescription: Propose an idea to make ants even better.\ntitle: \"[Feature]: \"\nlabels: [\"proposal\", \"enhancement\"]\nassignees:\n  - panjf2000\nbody:\n  - type: markdown\n    id: tips\n    attributes:\n      value: |\n        ## Before you go any further\n        - Please read [<u>*How To Ask Questions The Smart Way*</u>](http://www.catb.org/~esr/faqs/smart-questions.html) ( Chinese translation: [《提问的智慧》](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md)) before you file an issue formally.\n        - Keep in mind that there is always welcome to ask questions on [Discord](https://discord.gg/Cuy7KPaWQc).\n  - type: textarea\n    id: feature-request\n    attributes:\n      label: Description of new feature\n      description: Make a concise but clear description about this new feature.\n      placeholder: Describe this new feature with critical details.\n    validations:\n      required: true\n  - type: textarea\n    id: feature-scenario\n    attributes:\n      label: Scenarios for new feature\n      description: Explain why you need this feature and what scenarios can benefit from it.\n      placeholder: Please try to convince us that this new feature makes sense, also it will improve ants.\n    validations:\n      required: true\n  - type: dropdown\n    id: breaking-changes\n    attributes:\n      label: Breaking changes or not?\n      description: Is this new feature going to break the backward compatibility of the existing APIs?\n      options:\n        - \"Yes\"\n        - \"No\"\n    validations:\n      required: true\n  - type: textarea\n    id: code\n    attributes:\n      label: Code snippets (optional)\n      description: Illustrate this new feature with source code, what it looks like in code.\n      render: go\n  - type: textarea\n    id: feature-alternative\n    attributes:\n      label: Alternatives for new feature\n      description: Alternatives in your mind in case that the feature can't be done for some reasons.\n      placeholder: A concise but clear description of any alternative solutions or features you had in mind.\n      value: None.\n  - type: textarea\n    id: additional-context\n    attributes:\n      label: Additional context (optional)\n      description: Any additional context about this new feature we should know.\n      placeholder: What else should we know before we start discussing this new feature?\n      value: None.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/question.yaml",
    "content": "name: Question\ndescription: Ask questions about ants.\ntitle: \"[Question]: \"\nlabels: [\"question\", \"help wanted\"]\nbody:\n  - type: markdown\n    id: tips\n    attributes:\n      value: |\n        ## Before you go any further\n        - Please read [<u>*How To Ask Questions The Smart Way*</u>](http://www.catb.org/~esr/faqs/smart-questions.html) ( Chinese translation: [《提问的智慧》](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md)) before you file an issue formally.\n        - Keep in mind that there is always welcome to ask questions on [Discord](https://discord.gg/Cuy7KPaWQc).\n  - type: textarea\n    id: question\n    attributes:\n      label: Questions with details\n      description: What do you want to know?\n      placeholder: Describe your question with critical details here.\n    validations:\n      required: true\n  - type: textarea\n    id: code\n    attributes:\n      label: Code snippets (optional)\n      description: Illustrate your question with source code if needed.\n      render: go\n"
  },
  {
    "path": ".github/pull_request_template.md",
    "content": "<!--\nThank you for contributing to `ants`! Please fill this out to help us review your pull request more efficiently.\n\nWas this change discussed in an issue first? That can help save time in case the change is not a good fit for the project. Not all pull requests get merged.\n\nIt is not uncommon for pull requests to go through several, iterative reviews. Please be patient with us! Every reviewer is a volunteer, and each has their own style.\n-->\n\n## 1. Are you opening this pull request for bug-fixs, optimizations or new feature?\n\n\n\n## 2. Please describe how these code changes achieve your intention.\n<!-- Please be specific. Motivate the problem, and justify why this is the best solution. -->\n\n\n\n## 3. Please link to the relevant issues (if any).\n<!-- This adds crucial context to your change. -->\n\n\n\n## 4. Which documentation changes (if any) need to be made/updated because of this PR?\n<!-- Reviewers will often reference this first in order to know what to expect from the change. Please be specific enough so that they can paste your wording into the documentation directly. -->\n\n\n\n## 4. Checklist\n\n- [ ] I have squashed all insignificant commits.\n- [ ] I have commented my code for explaining package types, values, functions, and non-obvious lines.\n- [ ] I have written unit tests and verified that all tests passes (if needed).\n- [ ] I have documented feature info on the README (only when this PR is adding a new feature).\n- [ ] (optional) I am willing to help maintain this change if there are issues with it later.\n"
  },
  {
    "path": ".github/release-drafter.yml",
    "content": "name-template: Ants v$RESOLVED_VERSION\ntag-template: v$RESOLVED_VERSION\ncategories:\n    - title: 🧨 Breaking changes\n      labels:\n          - breaking changes\n    - title: 🚀 Features\n      labels:\n          - proposal\n          - new feature\n    - title: 🛩 Enhancements\n      labels:\n          - enhancement\n          - optimization\n    - title: 🐛 Bugfixes\n      labels:\n          - bug\n    - title: 📚 Documentation\n      labels:\n          - doc\n          - docs\n    - title: 🗃 Misc\n      labels:\n          - chores\nchange-template: '- $TITLE (#$NUMBER)'\nchange-title-escapes: '\\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.\nversion-resolver:\n  major:\n    labels:\n      - major\n  minor:\n    labels:\n      - minor\n      - new feature\n      - proposal\n  patch:\n    labels:\n      - patch\n      - bug\n      - dependencies\n  default: patch\nautolabeler:\n  - label: bug\n    title:\n      - /fix/i\n      - /bug/i\n      - /resolve/i\n  - label: docs\n    files:\n      - '*.md'\n    title:\n      - /doc/i\n      - /README/i\n  - label: enhancement\n    title:\n      - /opt:/i\n      - /refactor/i\n      - /optimize/i\n      - /improve/i\n      - /update/i\n      - /remove/i\n      - /delete/i\n  - label: optimization\n    title:\n      - /opt:/i\n      - /refactor/i\n      - /optimize/i\n      - /improve/i\n      - /update/i\n      - /remove/i\n      - /delete/i\n  - label: new feature\n    title:\n      - /feat:/i\n      - /feature/i\n      - /implement/i\n      - /add/i\n      - /minor/i\n  - label: dependencies\n    title:\n      - /dependencies/i\n      - /upgrade/i\n      - /bump up/i\n  - label: chores\n    title:\n      - /chore/i\n      - /misc/i\n      - /cleanup/i\n      - /clean up/i\n  - label: major\n    title:\n      - /major:/i\n  - label: minor\n    title:\n      - /minor:/i\n  - label: patch\n    title:\n      - /patch:/i\ntemplate: |\n    ## Changelogs\n    \n    $CHANGES\n\n    **Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION\n  \n    Thanks to all these contributors: $CONTRIBUTORS for making this release possible.\n"
  },
  {
    "path": ".github/workflows/codeql.yml",
    "content": "name: \"Code scanning\"\n\non:\n  push:\n    branches:\n      - master\n      - dev\n    paths-ignore:\n      - '**.md'\n      - '**.yml'\n      - '**.yaml'\n      - '!.github/workflows/codeql.yml'\n  pull_request:\n    branches:\n      - master\n      - dev\n    paths-ignore:\n      - '**.md'\n      - '**.yml'\n      - '**.yaml'\n      - '!.github/workflows/codeql.yml'\n  schedule:\n    #        ┌───────────── minute (0 - 59)\n    #        │  ┌───────────── hour (0 - 23)\n    #        │  │ ┌───────────── day of the month (1 - 31)\n    #        │  │ │ ┌───────────── month (1 - 12 or JAN-DEC)\n    #        │  │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)\n    #        │  │ │ │ │\n    #        │  │ │ │ │\n    #        │  │ │ │ │\n    #        *  * * * *\n    - cron: '30 4 * * 6'\n\njobs:\n  CodeQL-Build:\n    # CodeQL runs on ubuntu-latest, windows-latest, and macos-latest\n    runs-on: ubuntu-latest\n\n    permissions:\n      # required for all workflows\n      security-events: write\n\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n        with:\n          fetch-depth: 2\n\n      # Initializes the CodeQL tools for scanning.\n      - name: Initialize CodeQL\n        uses: github/codeql-action/init@v3\n        with:\n          languages: go\n\n      # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).\n      # If this step fails, then you should remove it and run the build manually (see below).\n      - name: Autobuild\n        uses: github/codeql-action/autobuild@v3\n\n      # ℹ️ Command-line programs to run using the OS shell.\n      # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun\n\n      # ✏️ If the Autobuild fails above, remove it and uncomment the following\n      #    three lines and modify them (or add more) to build your code if your\n      #    project uses a compiled language\n\n      #- run: |\n      #     make bootstrap\n      #     make release\n\n      - name: Perform CodeQL Analysis\n        uses: github/codeql-action/analyze@v3"
  },
  {
    "path": ".github/workflows/pull-request.yml",
    "content": "name: Check pull request target\non:\n  pull_request:\n    types:\n      - opened\n      - reopened\n      - synchronize\n    branches:\n      - master\njobs:\n  check-branches:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Check target branch\n        run: |\n          if [ ${{ github.head_ref }} != \"dev\" ]; then\n            echo \"Only pull requests from dev branch are only allowed to be merged into master branch.\"\n            exit 1\n          fi\n"
  },
  {
    "path": ".github/workflows/release-drafter.yml",
    "content": "name: Release Drafter\n\non:\n  push:\n    # branches to consider in the event; optional, defaults to all\n    branches:\n      - master\n  # pull_request event is required only for autolabeler\n  pull_request:\n    # Only following types are handled by the action, but one can default to all as well\n    types: [opened, reopened, synchronize]\n  # pull_request_target event is required for autolabeler to support PRs from forks\n  # pull_request_target:\n  #   types: [opened, reopened, synchronize]\n\npermissions:\n  contents: read\n\njobs:\n  update_release_draft:\n    permissions:\n      # write permission is required to create a github release\n      contents: write\n      # write permission is required for autolabeler\n      # otherwise, read permission is required at least\n      pull-requests: write\n    runs-on: ubuntu-latest\n    steps:\n      # (Optional) GitHub Enterprise requires GHE_HOST variable set\n      #- name: Set GHE_HOST\n      #  run: |\n      #    echo \"GHE_HOST=${GITHUB_SERVER_URL##https:\\/\\/}\" >> $GITHUB_ENV\n\n      # Drafts your next Release notes as Pull Requests are merged into \"master\"\n      - uses: release-drafter/release-drafter@v6\n        # (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml\n        # with:\n        #   config-name: my-config.yml\n        #   disable-autolabeler: true\n        env:\n          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}"
  },
  {
    "path": ".github/workflows/stale-bot.yml",
    "content": "name: Monitor inactive issues and PRs\non:\n  schedule:\n    - cron: '0 0 * * *'\n  workflow_dispatch:\n\njobs:\n  stale-issues:\n    runs-on: ubuntu-latest\n    permissions:\n      actions: write\n      issues: write\n      pull-requests: write\n    steps:\n      - uses: actions/stale@v9\n        with:\n          operations-per-run: 50\n          days-before-issue-stale: 30\n          days-before-issue-close: 7\n          stale-issue-label: 'stale'\n          stale-issue-message: |\n            This issue is marked as stale because it has been open for 30 days with no activity.\n            \n            You should take one of the following actions:\n            - Manually close this issue if it is no longer relevant\n            - Comment if you have more information to share\n            \n            This issue will be automatically closed in 7 days if no further activity occurs.\n          close-issue-message: |\n            This issue was closed because it has been inactive for 7 days since being marked as stale.\n\n            If you believe this is a false alarm, please leave a comment for it or open a new issue, you can also reopen this issue directly if you have permission.\n          days-before-pr-stale: 21\n          days-before-pr-close: 7\n          stale-pr-label: 'stale'\n          stale-pr-message: |\n            This PR is marked as stale because it has been open for 21 days with no activity.\n            \n            You should take one of the following actions:\n            - Manually close this PR if it is no longer relevant\n            - Push new commits or comment if you have more information to share\n            \n            This PR will be automatically closed in 7 days if no further activity occurs.\n          close-pr-message: |\n            This PR was closed because it has been inactive for 7 days since being marked as stale.\n\n            If you believe this is a false alarm, feel free to reopen this PR or create a new one.\n          repo-token: ${{ secrets.GITHUB_TOKEN }}"
  },
  {
    "path": ".github/workflows/test.yml",
    "content": "name: Run tests\n\non:\n  push:\n    branches:\n      - master\n      - dev\n    paths-ignore:\n      - '**.md'\n      - '**.yml'\n      - '**.yaml'\n      - 'examples/*'\n      - '!.github/workflows/test.yml'\n  pull_request:\n    branches:\n      - master\n      - dev\n    paths-ignore:\n      - '**.md'\n      - '**.yml'\n      - '**.yaml'\n      - 'examples/*'\n      - '!.github/workflows/test.yml'\n\nenv:\n  GO111MODULE: on\n  GOPROXY: \"https://proxy.golang.org\"\n\njobs:\n  lint:\n    strategy:\n      matrix:\n        os:\n          - ubuntu-latest\n          - macos-latest\n    name: Run golangci-lint\n    runs-on: ${{ matrix.os }}\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: '^1.20'\n          cache: false\n\n      - name: Setup and run golangci-lint\n        uses: golangci/golangci-lint-action@v6\n        with:\n          version: v1.62.2\n          args: --timeout 5m -v -E gofumpt -E gocritic -E misspell -E revive -E godot\n  test:\n    needs: lint\n    strategy:\n      fail-fast: false\n      matrix:\n        go: [1.19, 1.26]\n        os: [ubuntu-latest, macos-latest, windows-latest]\n    name: Go ${{ matrix.go }} @ ${{ matrix.os }}\n    runs-on: ${{ matrix.os}}\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n        with:\n          ref: ${{ github.ref }}\n\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: ${{ matrix.go }}\n\n      - name: Print Go environment\n        id: go-env\n        run: |\n          printf \"Using go at: $(which go)\\n\"\n          printf \"Go version: $(go version)\\n\"\n          printf \"\\n\\nGo environment:\\n\\n\"\n          go env\n          printf \"\\n\\nSystem environment:\\n\\n\"\n          env\n          # Calculate the short SHA1 hash of the git commit\n          echo \"SHORT_SHA=$(git rev-parse --short HEAD)\" >> $GITHUB_OUTPUT\n          echo \"GO_CACHE=$(go env GOCACHE)\" >> $GITHUB_OUTPUT\n\n      - name: Run unit tests and integrated tests\n        run: go test -v -race -coverprofile=\"codecov.report\" -covermode=atomic\n\n      - name: Upload code coverage report to Codecov\n        uses: codecov/codecov-action@v5\n        with:\n          files: ./codecov.report\n          flags: unittests\n          name: codecov-ants\n          fail_ci_if_error: true\n          verbose: true\n        env:\n          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}\n"
  },
  {
    "path": ".gitignore",
    "content": "# Binaries for programs and plugins\n*.exe\n*.exe~\n*.dll\n*.so\n*.dylib\n\n# Test binary, built with `go test -c`\n*.test\n\n# Output of the go coverage tool, specifically when used with LiteIDE\n*.out\n\n# Dependency directories (remove the comment below to include it)\n# vendor/\n\n.idea\n\n.DS_Store\n"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nIn the interest of fostering an open and welcoming environment, we as\ncontributors and maintainers pledge to making participation in our project and\nour community a harassment-free experience for everyone, regardless of age, body\nsize, disability, ethnicity, sex characteristics, gender identity and expression,\nlevel of experience, education, socio-economic status, nationality, personal\nappearance, race, religion, or sexual identity and orientation.\n\n## Our Standards\n\nExamples of behavior that contributes to creating a positive environment\ninclude:\n\n* Using welcoming and inclusive language\n* Being respectful of differing viewpoints and experiences\n* Gracefully accepting constructive criticism\n* Focusing on what is best for the community\n* Showing empathy towards other community members\n\nExamples of unacceptable behavior by participants include:\n\n* The use of sexualized language or imagery and unwelcome sexual attention or\n advances\n* Trolling, insulting/derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others' private information, such as a physical or electronic\n address, without explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\n professional setting\n\n## Our Responsibilities\n\nProject maintainers are responsible for clarifying the standards of acceptable\nbehavior and are expected to take appropriate and fair corrective action in\nresponse to any instances of unacceptable behavior.\n\nProject maintainers have the right and responsibility to remove, edit, or\nreject comments, commits, code, wiki edits, issues, and other contributions\nthat are not aligned to this Code of Conduct, or to ban temporarily or\npermanently any contributor for other behaviors that they deem inappropriate,\nthreatening, offensive, or harmful.\n\n## Scope\n\nThis Code of Conduct applies both within project spaces and in public spaces\nwhen an individual is representing the project or its community. Examples of\nrepresenting a project or community include using an official project e-mail\naddress, posting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event. Representation of a project may be\nfurther defined and clarified by project maintainers.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported by contacting the project team at panjf2000@gmail.com. All\ncomplaints will be reviewed and investigated and will result in a response that\nis deemed necessary and appropriate to the circumstances. The project team is\nobligated to maintain confidentiality with regard to the reporter of an incident.\nFurther details of specific enforcement policies may be posted separately.\n\nProject maintainers who do not follow or enforce the Code of Conduct in good\nfaith may face temporary or permanent repercussions as determined by other\nmembers of the project's leadership.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,\navailable at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html\n\n[homepage]: https://www.contributor-covenant.org\n\nFor answers to common questions about this code of conduct, see\nhttps://www.contributor-covenant.org/faq\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# Contributing \n\n## With issues:\n  - Use the search tool before opening a new issue.\n  - Please provide source code and commit sha if you found a bug.\n  - Review existing issues and provide feedback or react to them.\n\n## With pull requests:\n  - Open your pull request against `dev`.\n  - Open one pull request for only one feature/proposal, if you have several those, please put them into different PRs, whereas you are allowed to open one pull request with several bug-fixs.\n  - Your pull request should have no more than two commits, if not, you should squash them.\n  - It should pass all tests in the available continuous integrations systems such as TravisCI.\n  - You should add/modify tests to cover your proposed code changes.\n  - If your pull request contains a new feature, please document it on the README.\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2018 Andy Pan\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "<p align=\"center\">\n<img src=\"https://raw.githubusercontent.com/panjf2000/logos/master/ants/logo.png\" />\n<b>A goroutine pool for Go</b>\n<br/><br/>\n<a title=\"Build Status\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/actions?query=workflow%3ATests\"><img src=\"https://img.shields.io/github/actions/workflow/status/panjf2000/ants/test.yml?branch=master&style=flat-square&logo=github-actions\" /></a>\n<a title=\"Codecov\" target=\"_blank\" href=\"https://codecov.io/gh/panjf2000/ants\"><img src=\"https://img.shields.io/codecov/c/github/panjf2000/ants?style=flat-square&logo=codecov\" /></a>\n<a title=\"Release\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/releases\"><img src=\"https://img.shields.io/github/v/release/panjf2000/ants.svg?color=161823&style=flat-square&logo=smartthings\" /></a>\n<a title=\"Tag\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/tags\"><img src=\"https://img.shields.io/github/v/tag/panjf2000/ants?color=%23ff8936&logo=fitbit&style=flat-square\" /></a>\n<br/>\n<a title=\"Minimum Go Version\" target=\"_blank\" href=\"https://github.com/panjf2000/gnet\"><img src=\"https://img.shields.io/badge/go-%3E%3D1.19-30dff3?style=flat-square&logo=go\" /></a>\n<a title=\"Go Report Card\" target=\"_blank\" href=\"https://goreportcard.com/report/github.com/panjf2000/ants\"><img src=\"https://goreportcard.com/badge/github.com/panjf2000/ants?style=flat-square\" /></a>\n<a title=\"Doc for ants\" target=\"_blank\" href=\"https://pkg.go.dev/github.com/panjf2000/ants/v2?tab=doc\"><img src=\"https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs\" /></a>\n<a title=\"Mentioned in Awesome Go\" target=\"_blank\" href=\"https://github.com/avelino/awesome-go#goroutines\"><img src=\"https://awesome.re/mentioned-badge-flat.svg\" /></a>\n</p>\n\nEnglish | [中文](README_ZH.md)\n\n## 📖 Introduction\n\nLibrary `ants` implements a goroutine pool with fixed capacity, managing and recycling a massive number of goroutines, allowing developers to limit the number of goroutines in your concurrent programs.\n\n## 🚀 Features:\n\n- Managing and recycling a massive number of goroutines automatically\n- Purging overdue goroutines periodically\n- Abundant APIs: submitting tasks, getting the number of running goroutines, tuning the capacity of the pool dynamically, releasing the pool, rebooting the pool, etc.\n- Handle panic gracefully to prevent programs from crash\n- Efficient in memory usage and it may even achieve ***higher performance*** than unlimited goroutines in Go\n- Nonblocking mechanism\n- Preallocated memory (ring buffer, optional)\n\n## 💡 How `ants` works\n\n### Flow Diagram\n\n<p align=\"center\">\n<img width=\"1011\" alt=\"ants-flowchart-en\" src=\"https://user-images.githubusercontent.com/7496278/66396509-7b42e700-ea0c-11e9-8612-b71a4b734683.png\">\n</p>\n\n### Activity Diagrams\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-1.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-2.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-3.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-4.png)\n\n## 🧰 How to install\n\n### For `ants` v1\n\n``` powershell\ngo get -u github.com/panjf2000/ants\n```\n\n### For `ants` v2 (with GO111MODULE=on)\n\n```powershell\ngo get -u github.com/panjf2000/ants/v2\n```\n\n## 🛠 How to use\nCheck out [the examples](https://pkg.go.dev/github.com/panjf2000/ants/v2#pkg-examples) for basic usage.\n\n### Functional options for pool\n\n`ants.Options`contains all optional configurations of the ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`/`NewPoolWithFuncGeneric` method.\n\nCheck out [ants.Options](https://pkg.go.dev/github.com/panjf2000/ants/v2#Options) and [ants.Option](https://pkg.go.dev/github.com/panjf2000/ants/v2#Option) for more details.\n\n### Customize pool capacity\n\n`ants` supports customizing the capacity of the pool. You can call the `NewPool` method to instantiate a `Pool` with a given capacity, as follows:\n\n``` go\np, _ := ants.NewPool(10000)\n```\n\n### Submit tasks\nTasks can be submitted by calling `ants.Submit`\n```go\nants.Submit(func(){})\n```\n\n### Tune pool capacity at runtime\nYou can tune the capacity of `ants` pool at runtime with `ants.Tune`:\n\n``` go\npool.Tune(1000) // Tune its capacity to 1000\npool.Tune(100000) // Tune its capacity to 100000\n```\n\nDon't worry about the contention problems in this case, the method here is thread-safe (or should be called goroutine-safe).\n\n### Pre-malloc goroutine queue in pool\n\n`ants` allows you to pre-allocate the memory of the goroutine queue in the pool, which may get a performance enhancement under some special certain circumstances such as the scenario that requires a pool with ultra-large capacity, meanwhile, each task in goroutine lasts for a long time, in this case, pre-mallocing will reduce a lot of memory allocation in goroutine queue.\n\n```go\n// ants will pre-malloc the whole capacity of pool when calling ants.NewPool.\np, _ := ants.NewPool(100000, ants.WithPreAlloc(true))\n```\n\n### Release pool\n\n```go\npool.Release()\n```\n\nor\n\n```go\npool.ReleaseTimeout(time.Second * 3)\n```\n\n### Reboot pool\n\n```go\n// A pool that has been released can be still used after calling the Reboot().\npool.Reboot()\n```\n\n## ⚙️ About sequence\n\nAll tasks submitted to `ants` pool will not be guaranteed to be addressed in order, because those tasks scatter among a series of concurrent workers, thus those tasks would be executed concurrently.\n\n## 👏 Contributors\n\nPlease read our [Contributing Guidelines](CONTRIBUTING.md) before opening a PR and thank you to all the developers who already made contributions to `ants`!\n\n<a href=\"https://github.com/panjf2000/ants/graphs/contributors\">\n  <img src=\"https://contrib.rocks/image?repo=panjf2000/ants\" />\n</a>\n\n## 📄 License\n\nThe source code in `ants` is available under the [MIT License](/LICENSE).\n\n## 📚 Relevant Articles\n\n-  [Goroutine 并发调度模型深度解析之手撸一个高性能 goroutine 池](https://taohuawu.club/high-performance-implementation-of-goroutine-pool)\n-  [Visually Understanding Worker Pool](https://medium.com/coinmonks/visually-understanding-worker-pool-48a83b7fc1f5)\n-  [The Case For A Go Worker Pool](https://brandur.org/go-worker-pool)\n-  [Go Concurrency - GoRoutines, Worker Pools and Throttling Made Simple](https://twin.sh/articles/39/go-concurrency-goroutines-worker-pools-and-throttling-made-simple)\n\n## 🖥 Use cases\n\n### business corporations & open-source organizations\n\nTrusted by the following corporations/organizations.\n\n<table>\n  <tbody>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.tencent.com/\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/tencent_logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.bytedance.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/ByteDance_Logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://tieba.baidu.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/baidu-tieba-logo.png\" width=\"300\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://weibo.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/weibo-logo.png\" width=\"300\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.tencentmusic.com/en-us/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/tencent-music-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.futuhk.com/en/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/futu-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.shopify.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/shopify-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.wechat.com/en/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/wechat-logo.png\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.baidu.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/baidu-mobile-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.360.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/360-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.huaweicloud.com/intl/en-us/\" target=\"_blank\">\n          <img src=\"https://res-static.hc-cdn.cn/cloudbu-site/china/zh-cn/%E7%BB%84%E4%BB%B6%E9%AA%8C%E8%AF%81/pep-common-header/logo-en.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.matrixorigin.io/\" target=\"_blank\">\n          <img src=\"https://www.matrixorigin.io/_next/static/media/logo-light-en.b8e29d17.svg\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://adguard-dns.io/\" target=\"_blank\">\n          <img src=\"https://cdn.adtidy.org/website/images/AdGuardDNS_black.svg\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://bk.tencent.com/\" target=\"_blank\">\n          <img src=\"https://static.apiseven.com/2022/11/14/6371adab14119.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.alibabacloud.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/aliyun-intl-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.zuoyebang.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/zuoyebang-logo.jpeg\" width=\"300\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.antgroup.com/en/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/ant-group-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://zilliz.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/zilliz-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://amap.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/amap-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.apache.org/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/asf-estd-1999-logo.jpg\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.coze.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/coze-logo.png\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n  </tbody>\n</table>\n\nIf you're also using `ants` in production, please help us enrich this list by opening a pull request.\n\n### open-source software\n\nThe open-source projects below do concurrent programming with the help of `ants`.\n\n- [gnet](https://github.com/panjf2000/gnet):  A high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.\n- [milvus](https://github.com/milvus-io/milvus): An open-source vector database for scalable similarity search and AI applications.\n- [nps](https://github.com/ehang-io/nps): A lightweight, high-performance, powerful intranet penetration proxy server, with a powerful web management terminal.\n- [TDengine](https://github.com/taosdata/TDengine): TDengine is an open source, high-performance, cloud native time-series database optimized for Internet of Things (IoT), Connected Cars, and Industrial IoT.\n- [siyuan](https://github.com/siyuan-note/siyuan): SiYuan is a local-first personal knowledge management system that supports complete offline use, as well as end-to-end encrypted synchronization.\n- [BillionMail](https://github.com/aaPanel/BillionMail): A future open-source Mail server, Email marketing platform designed to help businesses and individuals manage their email campaigns with ease.\n- [WeKnora](https://github.com/Tencent/WeKnora): An LLM-powered framework designed for deep document understanding and semantic retrieval, especially for handling complex, heterogeneous documents.\n- [coze-loop](https://github.com/coze-dev/coze-loop): A developer-oriented, platform-level solution focused on the development and operation of AI agents.\n- [osmedeus](https://github.com/j3ssie/osmedeus): A Workflow Engine for Offensive Security.\n- [jitsu](https://github.com/jitsucom/jitsu/tree/master): An open-source Segment alternative. Fully-scriptable data ingestion engine for modern data teams. Set-up a real-time data pipeline in minutes, not days.\n- [triangula](https://github.com/RH12503/triangula): Generate high-quality triangulated and polygonal art from images.\n- [teler](https://github.com/kitabisa/teler): Real-time HTTP Intrusion Detection.\n- [bsc](https://github.com/binance-chain/bsc): A Binance Smart Chain client based on the go-ethereum fork.\n- [jaeles](https://github.com/jaeles-project/jaeles): The Swiss Army knife for automated Web Application Testing.\n- [devlake](https://github.com/apache/incubator-devlake): The open-source dev data platform & dashboard for your DevOps tools.\n- [matrixone](https://github.com/matrixorigin/matrixone): MatrixOne is a future-oriented hyper-converged cloud and edge native DBMS that supports transactional, analytical, and streaming workloads with a simplified and distributed database engine, across multiple data centers, clouds, edges and other heterogeneous infrastructures.\n- [bk-bcs](https://github.com/TencentBlueKing/bk-bcs): BlueKing Container Service (BCS, same below) is a container management and orchestration platform for the micro-services under the BlueKing ecosystem.\n- [trueblocks-core](https://github.com/TrueBlocks/trueblocks-core): TrueBlocks improves access to blockchain data for any EVM-compatible chain (particularly Ethereum mainnet) while remaining entirely local.\n- [openGemini](https://github.com/openGemini/openGemini): openGemini is an open-source,cloud-native time-series database(TSDB) that can be widely used in IoT, Internet of Vehicles(IoV), O&M monitoring, and industrial Internet scenarios.\n- [AdGuardDNS](https://github.com/AdguardTeam/AdGuardDNS): AdGuard DNS is an alternative solution for tracker blocking, privacy protection, and parental control.\n- [WatchAD2.0](https://github.com/Qihoo360/WatchAD2.0): WatchAD2.0 是 360 信息安全中心开发的一款针对域安全的日志分析与监控系统，它可以收集所有域控上的事件日志、网络流量，通过特征匹配、协议分析、历史行为、敏感操作和蜜罐账户等方式来检测各种已知与未知威胁，功能覆盖了大部分目前的常见内网域渗透手法。\n- [vanus](https://github.com/vanus-labs/vanus): Vanus is a Serverless, event streaming system with processing capabilities. It easily connects SaaS, Cloud Services, and Databases to help users build next-gen Event-driven Applications.\n- [trpc-go](https://github.com/trpc-group/trpc-go): A pluggable, high-performance RPC framework written in Golang.\n- [motan-go](https://github.com/weibocom/motan-go): Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services. motan-go is the golang implementation of Motan.\n\n#### All use cases:\n\n- [Repositories that depend on ants/v2](https://github.com/panjf2000/ants/network/dependents?package_id=UGFja2FnZS0yMjY2ODgxMjg2)\n\n- [Repositories that depend on ants/v1](https://github.com/panjf2000/ants/network/dependents?package_id=UGFja2FnZS0yMjY0ODMzNjEw)\n\nIf you have `ants` integrated into projects, feel free to open a pull request refreshing this list of use cases.\n\n## 🔋 JetBrains OS licenses\n\n`ants` has been being developed with GoLand under the **free JetBrains Open Source license(s)** granted by JetBrains s.r.o., hence I would like to express my thanks here.\n\n<a href=\"https://www.jetbrains.com/?from=ants\" target=\"_blank\"><img src=\"https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.svg\" alt=\"JetBrains logo.\"></a>\n\n## ☕️ Buy me a coffee\n\n> Please be sure to leave your name, GitHub account, or other social media accounts when you donate by the following means so that I can add it to the list of donors as a token of my appreciation.\n\n<table>\n  <tbody>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://buymeacoffee.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/bmc_qr.png\" width=\"250\" alt=\"Buy me a coffee\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://www.patreon.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/patreon_logo.png\" width=\"250\" alt=\"Patreon\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://opencollective.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/open-collective-logo.png\" width=\"250\" alt=\"OpenCollective\" />\n        </a>\n      </td>\n    </tr>\n  </tbody>\n</table>\n\n## 🔋 Sponsorship\n\n<p>\n  <a href=\"https://www.digitalocean.com/\">\n    <img src=\"https://opensource.nyc3.cdn.digitaloceanspaces.com/attribution/assets/PoweredByDO/DO_Powered_by_Badge_blue.svg\" width=\"201px\">\n  </a>\n</p>\n"
  },
  {
    "path": "README_ZH.md",
    "content": "<p align=\"center\">\n<img src=\"https://raw.githubusercontent.com/panjf2000/logos/master/ants/logo.png\" />\n<b>Go 语言的 goroutine 池</b>\n<br/><br/>\n<a title=\"Build Status\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/actions?query=workflow%3ATests\"><img src=\"https://img.shields.io/github/actions/workflow/status/panjf2000/ants/test.yml?branch=master&style=flat-square&logo=github-actions\" /></a>\n<a title=\"Codecov\" target=\"_blank\" href=\"https://codecov.io/gh/panjf2000/ants\"><img src=\"https://img.shields.io/codecov/c/github/panjf2000/ants?style=flat-square&logo=codecov\" /></a>\n<a title=\"Release\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/releases\"><img src=\"https://img.shields.io/github/v/release/panjf2000/ants.svg?color=161823&style=flat-square&logo=smartthings\" /></a>\n<a title=\"Tag\" target=\"_blank\" href=\"https://github.com/panjf2000/ants/tags\"><img src=\"https://img.shields.io/github/v/tag/panjf2000/ants?color=%23ff8936&logo=fitbit&style=flat-square\" /></a>\n<br/>\n<a title=\"Minimum Go Version\" target=\"_blank\" href=\"https://github.com/panjf2000/gnet\"><img src=\"https://img.shields.io/badge/go-%3E%3D1.19-30dff3?style=flat-square&logo=go\" /></a>\n<a title=\"Go Report Card\" target=\"_blank\" href=\"https://goreportcard.com/report/github.com/panjf2000/ants\"><img src=\"https://goreportcard.com/badge/github.com/panjf2000/ants?style=flat-square\" /></a>\n<a title=\"Doc for ants\" target=\"_blank\" href=\"https://pkg.go.dev/github.com/panjf2000/ants/v2?tab=doc\"><img src=\"https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs\" /></a>\n<a title=\"Mentioned in Awesome Go\" target=\"_blank\" href=\"https://github.com/avelino/awesome-go#goroutines\"><img src=\"https://awesome.re/mentioned-badge-flat.svg\" /></a>\n</p>\n\n[英文](README.md) | 中文\n\n## 📖 简介\n\n`ants` 是一个高性能的 goroutine 池，实现了对大规模 goroutine 的调度管理、goroutine 复用，允许使用者在开发并发程序的时候限制 goroutine 数量，复用资源，达到更高效执行任务的效果。\n\n## 🚀 功能：\n\n- 自动调度海量的 goroutines，复用 goroutines\n- 定期清理过期的 goroutines，进一步节省资源\n- 提供了大量实用的接口：任务提交、获取运行中的 goroutine 数量、动态调整 Pool 大小、释放 Pool、重启 Pool 等\n- 优雅处理 panic，防止程序崩溃\n- 资源复用，极大节省内存使用量；在大规模批量并发任务场景下甚至可能比 Go 语言的无限制 goroutine 并发具有***更高的性能***\n- 非阻塞机制\n- 预分配内存 (环形队列，可选)\n\n## 💡 `ants` 是如何运行的\n\n### 流程图\n\n<p align=\"center\">\n<img width=\"845\" alt=\"ants-flowchart-cn\" src=\"https://user-images.githubusercontent.com/7496278/66396519-7ed66e00-ea0c-11e9-9c1a-5ca54bbd61eb.png\">\n</p>\n\n### 动态图\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-1.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-2.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-3.png)\n\n![](https://raw.githubusercontent.com/panjf2000/illustrations/master/go/ants-pool-4.png)\n\n## 🧰 安装\n\n### 使用 `ants` v1 版本:\n\n``` powershell\ngo get -u github.com/panjf2000/ants\n```\n\n### 使用 `ants` v2 版本 (开启 GO111MODULE=on):\n\n```powershell\ngo get -u github.com/panjf2000/ants/v2\n```\n\n## 🛠 使用\n基本的使用请查看[示例](https://pkg.go.dev/github.com/panjf2000/ants/v2#pkg-examples).\n\n### Pool 配置\n\n通过在调用 `NewPool`/`NewPoolWithFunc`/`NewPoolWithFuncGeneric` 之时使用各种 optional function，可以设置 `ants.Options` 中各个配置项的值，然后用它来定制化 goroutine pool。\n\n更多细节请查看 [ants.Options](https://pkg.go.dev/github.com/panjf2000/ants/v2#Options) 和 [ants.Option](https://pkg.go.dev/github.com/panjf2000/ants/v2#Option)\n\n\n### 自定义 pool 容量\n`ants` 支持实例化使用者自己的一个 Pool，指定具体的 pool 容量；通过调用 `NewPool` 方法可以实例化一个新的带有指定容量的 `Pool`，如下：\n\n``` go\np, _ := ants.NewPool(10000)\n```\n\n### 任务提交\n\n提交任务通过调用 `ants.Submit` 方法：\n```go\nants.Submit(func(){})\n```\n\n### 动态调整 goroutine 池容量\n需要动态调整 pool 容量可以通过调用 `ants.Tune`：\n\n``` go\npool.Tune(1000) // Tune its capacity to 1000\npool.Tune(100000) // Tune its capacity to 100000\n```\n\n该方法是线程安全的。\n\n### 预先分配 goroutine 队列内存\n\n`ants` 支持预先为 pool 分配容量的内存， 这个功能可以在某些特定的场景下提高 goroutine 池的性能。比如， 有一个场景需要一个超大容量的池，而且每个 goroutine 里面的任务都是耗时任务，这种情况下，预先分配 goroutine 队列内存将会减少不必要的内存重新分配。\n\n```go\n// 提前分配的 pool 容量的内存空间\np, _ := ants.NewPool(100000, ants.WithPreAlloc(true))\n```\n\n### 释放 Pool\n\n```go\npool.Release()\n```\n\n或者\n\n```go\npool.ReleaseTimeout(time.Second * 3)\n```\n\n### 重启 Pool\n\n```go\n// 只要调用 Reboot() 方法，就可以重新激活一个之前已经被销毁掉的池，并且投入使用。\npool.Reboot()\n```\n\n## ⚙️ 关于任务执行顺序\n\n`ants` 并不保证提交的任务被执行的顺序，执行的顺序也不是和提交的顺序保持一致，因为在 `ants` 是并发地处理所有提交的任务，提交的任务会被分派到正在并发运行的 workers 上去，因此那些任务将会被并发且无序地被执行。\n\n## 👏 贡献者\n\n请在提 PR 之前仔细阅读 [Contributing Guidelines](CONTRIBUTING.md)，感谢那些为 `ants` 贡献过代码的开发者！\n\n<a href=\"https://github.com/panjf2000/ants/graphs/contributors\">\n  <img src=\"https://contrib.rocks/image?repo=panjf2000/ants\" />\n</a>\n\n## 📄 证书\n\n`ants` 的源码允许用户在遵循 [MIT 开源证书](/LICENSE) 规则的前提下使用。\n\n## 📚 相关文章\n\n-  [Goroutine 并发调度模型深度解析之手撸一个高性能 goroutine 池](https://taohuawu.club/high-performance-implementation-of-goroutine-pool)\n-  [Visually Understanding Worker Pool](https://medium.com/coinmonks/visually-understanding-worker-pool-48a83b7fc1f5)\n-  [The Case For A Go Worker Pool](https://brandur.org/go-worker-pool)\n-  [Go Concurrency - GoRoutines, Worker Pools and Throttling Made Simple](https://twin.sh/articles/39/go-concurrency-goroutines-worker-pools-and-throttling-made-simple)\n\n## 🖥 用户案例\n\n### 商业公司和开源组织\n\n以下公司/组织在生产环境上使用了 `ants`。\n\n<table>\n  <tbody>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.tencent.com/\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/tencent_logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.bytedance.com/zh/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/ByteDance_Logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://tieba.baidu.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/baidu-tieba-logo.png\" width=\"300\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://weibo.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/weibo-logo.png\" width=\"300\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.tencentmusic.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/tencent-music-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.futuhk.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/futu-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.shopify.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/shopify-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://weixin.qq.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/wechat-logo.png\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.baidu.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/baidu-mobile-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.360.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/360-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.huaweicloud.com/\" target=\"_blank\">\n          <img src=\"https://res-static.hc-cdn.cn/cloudbu-site/china/zh-cn/wangxue/header/logo.svg\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://matrixorigin.cn/\" target=\"_blank\">\n          <img src=\"https://www.matrixorigin.cn/_next/static/media/logo-light-zh.16ed7ea0.svg\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://adguard-dns.io/\" target=\"_blank\">\n          <img src=\"https://cdn.adtidy.org/website/images/AdGuardDNS_black.svg\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://bk.tencent.com/\" target=\"_blank\">\n          <img src=\"https://static.apiseven.com/2022/11/14/6371adab14119.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://cn.aliyun.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/aliyun-cn-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.zuoyebang.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/zuoyebang-logo.jpeg\" width=\"300\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.antgroup.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/ant-group-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://zilliz.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/zilliz-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://amap.com/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/amap-logo.png\" width=\"250\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.apache.org/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/asf-estd-1999-logo.jpg\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a href=\"https://www.coze.cn/\" target=\"_blank\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/coze-logo-cn.png\" width=\"250\" />\n        </a>\n      </td>\n    </tr>\n  </tbody>\n</table>\n如果你也正在生产环境上使用 `ants`，欢迎提 PR 来丰富这份列表。\n\n### 开源软件\n\n这些开源项目借助 `ants` 进行并发编程。\n\n- [gnet](https://github.com/panjf2000/gnet):  gnet 是一个高性能、轻量级、非阻塞的事件驱动 Go 网络框架。\n- [milvus](https://github.com/milvus-io/milvus): 一个高度灵活、可靠且速度极快的云原生开源向量数据库。\n- [nps](https://github.com/ehang-io/nps): 一款轻量级、高性能、功能强大的内网穿透代理服务器。\n- [TDengine](https://github.com/taosdata/TDengine): TDengine 是一款开源、高性能、云原生的时序数据库 (Time-Series Database, TSDB)。TDengine 能被广泛运用于物联网、工业互联网、车联网、IT 运维、金融等领域。\n- [siyuan](https://github.com/siyuan-note/siyuan): 思源笔记是一款本地优先的个人知识管理系统，支持完全离线使用，同时也支持端到端加密同步。\n- [BillionMail](https://github.com/aaPanel/BillionMail): BillionMail 是一个未来的开源邮件服务器和电子邮件营销平台，旨在帮助企业和个人轻松管理他们的电子邮件营销活动。\n- [WeKnora](https://github.com/Tencent/WeKnora): 一款基于大语言模型（LLM）的文档理解与语义检索框架，专为结构复杂、内容异构的文档场景而打造。\n- [coze-loop](https://github.com/coze-dev/coze-loop): Coze Loop 是一个面向开发者，专注于 AI Agent 开发与运维的平台级解决方案。\n- [osmedeus](https://github.com/j3ssie/osmedeus): A Workflow Engine for Offensive Security.\n- [jitsu](https://github.com/jitsucom/jitsu/tree/master): An open-source Segment alternative. Fully-scriptable data ingestion engine for modern data teams. Set-up a real-time data pipeline in minutes, not days.\n- [triangula](https://github.com/RH12503/triangula): Generate high-quality triangulated and polygonal art from images.\n- [teler](https://github.com/kitabisa/teler): Real-time HTTP Intrusion Detection.\n- [bsc](https://github.com/binance-chain/bsc): A Binance Smart Chain client based on the go-ethereum fork.\n- [jaeles](https://github.com/jaeles-project/jaeles): The Swiss Army knife for automated Web Application Testing.\n- [devlake](https://github.com/apache/incubator-devlake): The open-source dev data platform & dashboard for your DevOps tools.\n- [matrixone](https://github.com/matrixorigin/matrixone): MatrixOne 是一款面向未来的超融合异构云原生数据库，通过超融合数据引擎支持事务/分析/流处理等混合工作负载，通过异构云原生架构支持跨机房协同/多地协同/云边协同。简化开发运维，消简数据碎片，打破数据的系统、位置和创新边界。\n- [bk-bcs](https://github.com/TencentBlueKing/bk-bcs): 蓝鲸容器管理平台（Blueking Container Service）定位于打造云原生技术和业务实际应用场景之间的桥梁；聚焦于复杂应用场景的容器化部署技术方案的研发、整合和产品化；致力于为游戏等复杂应用提供一站式、低门槛的容器编排和服务治理服务。\n- [trueblocks-core](https://github.com/TrueBlocks/trueblocks-core): TrueBlocks improves access to blockchain data for any EVM-compatible chain (particularly Ethereum mainnet) while remaining entirely local.\n- [openGemini](https://github.com/openGemini/openGemini): openGemini 是华为云开源的一款云原生分布式时序数据库，可广泛应用于物联网、车联网、运维监控、工业互联网等业务场景，具备卓越的读写性能和高效的数据分析能力，采用类SQL查询语言，无第三方软件依赖、安装简单、部署灵活、运维便捷。\n- [AdGuardDNS](https://github.com/AdguardTeam/AdGuardDNS): AdGuard DNS is an alternative solution for tracker blocking, privacy protection, and parental control.\n- [WatchAD2.0](https://github.com/Qihoo360/WatchAD2.0): WatchAD2.0 是 360 信息安全中心开发的一款针对域安全的日志分析与监控系统，它可以收集所有域控上的事件日志、网络流量，通过特征匹配、协议分析、历史行为、敏感操作和蜜罐账户等方式来检测各种已知与未知威胁，功能覆盖了大部分目前的常见内网域渗透手法。\n- [vanus](https://github.com/vanus-labs/vanus): Vanus is a Serverless, event streaming system with processing capabilities. It easily connects SaaS, Cloud Services, and Databases to help users build next-gen Event-driven Applications.\n- [trpc-go](https://github.com/trpc-group/trpc-go): 一个 Go 实现的可插拔的高性能 RPC 框架。\n- [motan-go](https://github.com/weibocom/motan-go): Motan 是一套高性能、易于使用的分布式远程服务调用 (RPC) 框架。motan-go 是 motan 的 Go 语言实现。\n\n#### 所有案例:\n\n- [Repositories that depend on ants/v2](https://github.com/panjf2000/ants/network/dependents?package_id=UGFja2FnZS0yMjY2ODgxMjg2)\n\n- [Repositories that depend on ants/v1](https://github.com/panjf2000/ants/network/dependents?package_id=UGFja2FnZS0yMjY0ODMzNjEw)\n\n如果你的项目也在使用 `ants`，欢迎给我提 Pull Request 来更新这份用户案例列表。\n\n## 🔋 JetBrains 开源证书支持\n\n`ants` 项目一直以来都是在 JetBrains 公司旗下的 GoLand 集成开发环境中进行开发，基于 **free JetBrains Open Source license(s)** 正版免费授权，在此表达我的谢意。\n\n<a href=\"https://www.jetbrains.com/?from=ants\" target=\"_blank\"><img src=\"https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.svg\" alt=\"JetBrains logo.\"></a>\n\n## ☕️ 打赏\n\n> 当您通过以下方式进行捐赠时，请务必留下姓名、GitHub 账号或其他社交媒体账号，以便我将其添加到捐赠者名单中，以表谢意。\n\n<table>\n  <tbody>\n    <tr>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://buymeacoffee.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/bmc_qr.png\" width=\"250\" alt=\"Buy me coffee\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://www.patreon.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/patreon_logo.png\" width=\"250\" alt=\"Patreon\" />\n        </a>\n      </td>\n      <td align=\"center\" valign=\"middle\">\n        <a target=\"_blank\" href=\"https://opencollective.com/panjf2000\">\n          <img src=\"https://res.strikefreedom.top/static_res/logos/open-collective-logo.png\" width=\"250\" alt=\"OpenCollective\" />\n        </a>\n      </td>\n    </tr>\n  </tbody>\n</table>\n\n## 🔋 赞助商\n\n<p>\n  <a href=\"https://www.digitalocean.com/\">\n    <img src=\"https://opensource.nyc3.cdn.digitaloceanspaces.com/attribution/assets/PoweredByDO/DO_Powered_by_Badge_blue.svg\" width=\"201px\">\n  </a>\n</p>\n"
  },
  {
    "path": "ants.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n// Package ants implements an efficient and reliable goroutine pool for Go.\n//\n// With ants, Go applications are able to limit the number of active goroutines,\n// recycle goroutines efficiently, and reduce the memory footprint significantly.\n// Package ants is extremely useful in the scenarios where a massive number of\n// goroutines are created and destroyed frequently, such as highly-concurrent\n// batch processing systems, HTTP servers, services of asynchronous tasks, etc.\npackage ants\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"log\"\n\t\"math\"\n\t\"os\"\n\t\"runtime\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"time\"\n\n\tsyncx \"github.com/panjf2000/ants/v2/pkg/sync\"\n)\n\nconst (\n\t// DefaultAntsPoolSize is the default capacity for a default goroutine pool.\n\tDefaultAntsPoolSize = math.MaxInt32\n\n\t// DefaultCleanIntervalTime is the interval time to clean up goroutines.\n\tDefaultCleanIntervalTime = time.Second\n)\n\nconst (\n\t// OPENED represents that the pool is opened.\n\tOPENED = iota\n\n\t// CLOSED represents that the pool is closed.\n\tCLOSED\n)\n\nvar (\n\t// ErrLackPoolFunc will be returned when invokers don't provide function for pool.\n\tErrLackPoolFunc = errors.New(\"must provide function for pool\")\n\n\t// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.\n\tErrInvalidPoolExpiry = errors.New(\"invalid expiry for pool\")\n\n\t// ErrPoolClosed will be returned when submitting task to a closed pool.\n\tErrPoolClosed = errors.New(\"this pool has been closed\")\n\n\t// ErrPoolOverload will be returned when the pool is full and no workers available.\n\tErrPoolOverload = errors.New(\"too many goroutines blocked on submit or Nonblocking is set\")\n\n\t// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.\n\tErrInvalidPreAllocSize = errors.New(\"can not set up a negative capacity under PreAlloc mode\")\n\n\t// ErrTimeout will be returned after the operations timed out.\n\tErrTimeout = errors.New(\"operation timed out\")\n\n\t// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.\n\tErrInvalidPoolIndex = errors.New(\"invalid pool index\")\n\n\t// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.\n\tErrInvalidLoadBalancingStrategy = errors.New(\"invalid load-balancing strategy\")\n\n\t// ErrInvalidMultiPoolSize  will be returned when trying to create a MultiPool with an invalid size.\n\tErrInvalidMultiPoolSize = errors.New(\"invalid size for multiple pool\")\n\n\t// workerChanCap determines whether the channel of a worker should be a buffered channel\n\t// to get the best performance. Inspired by fasthttp at\n\t// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139\n\tworkerChanCap = func() int {\n\t\t// Use blocking channel if GOMAXPROCS=1.\n\t\t// This switches context from sender to receiver immediately,\n\t\t// which results in higher performance (under go1.5 at least).\n\t\tif runtime.GOMAXPROCS(0) == 1 {\n\t\t\treturn 0\n\t\t}\n\n\t\t// Use non-blocking workerChan if GOMAXPROCS>1,\n\t\t// since otherwise the sender might be dragged down if the receiver is CPU-bound.\n\t\treturn 1\n\t}()\n\n\tdefaultLogger = Logger(log.New(os.Stderr, \"[ants]: \", log.LstdFlags|log.Lmsgprefix|log.Lmicroseconds))\n\n\t// Init an instance pool when importing ants.\n\tdefaultAntsPool, _ = NewPool(DefaultAntsPoolSize)\n)\n\n// Submit submits a task to pool.\nfunc Submit(task func()) error {\n\treturn defaultAntsPool.Submit(task)\n}\n\n// Running returns the number of the currently running goroutines.\nfunc Running() int {\n\treturn defaultAntsPool.Running()\n}\n\n// Cap returns the capacity of this default pool.\nfunc Cap() int {\n\treturn defaultAntsPool.Cap()\n}\n\n// Free returns the available goroutines to work.\nfunc Free() int {\n\treturn defaultAntsPool.Free()\n}\n\n// Release Closes the default pool.\nfunc Release() {\n\tdefaultAntsPool.Release()\n}\n\n// ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.\nfunc ReleaseTimeout(timeout time.Duration) error {\n\treturn defaultAntsPool.ReleaseTimeout(timeout)\n}\n\n// ReleaseContext is like Release but with a context, it waits all workers to exit before the context is done.\n//\n// Note that if the context is nil, it is the same as Release,\n// just return immediately without waiting for all workers to exit.\nfunc ReleaseContext(ctx context.Context) error {\n\treturn defaultAntsPool.ReleaseContext(ctx)\n}\n\n// Reboot reboots the default pool.\nfunc Reboot() {\n\tdefaultAntsPool.Reboot()\n}\n\n// Logger is used for logging formatted messages.\ntype Logger interface {\n\t// Printf must have the same semantics as log.Printf.\n\tPrintf(format string, args ...any)\n}\n\n// poolCommon contains all common fields for other sophisticated pools.\ntype poolCommon struct {\n\t// capacity of the pool, a negative value means that the capacity of pool is limitless, an infinite pool is used to\n\t// avoid potential issue of endless blocking caused by nested usage of a pool: submitting a task to pool\n\t// which submits a new task to the same pool.\n\tcapacity int32\n\n\t// running is the number of the currently running goroutines.\n\trunning int32\n\n\t// lock for protecting the worker queue.\n\tlock sync.Locker\n\n\t// workers is a slice that store the available workers.\n\tworkers workerQueue\n\n\t// state is used to notice the pool to closed itself.\n\tstate int32\n\n\t// cond for waiting to get an idle worker.\n\tcond *sync.Cond\n\n\t// done is used to indicate that all workers are done.\n\tallDone chan struct{}\n\t// once is used to make sure the pool is closed just once.\n\tonce *sync.Once\n\n\t// workerCache speeds up the obtainment of a usable worker in function:retrieveWorker.\n\tworkerCache sync.Pool\n\n\t// waiting is the number of goroutines already been blocked on pool.Submit(), protected by pool.lock\n\twaiting int32\n\n\tpurgeDone int32\n\tpurgeCtx  context.Context\n\tstopPurge context.CancelFunc\n\n\tticktockDone int32\n\tticktockCtx  context.Context\n\tstopTicktock context.CancelFunc\n\n\tnow int64\n\n\toptions *Options\n}\n\nfunc newPool(size int, options ...Option) (*poolCommon, error) {\n\tif size <= 0 {\n\t\tsize = -1\n\t}\n\n\topts := loadOptions(options...)\n\n\tif !opts.DisablePurge {\n\t\tif expiry := opts.ExpiryDuration; expiry < 0 {\n\t\t\treturn nil, ErrInvalidPoolExpiry\n\t\t} else if expiry == 0 {\n\t\t\topts.ExpiryDuration = DefaultCleanIntervalTime\n\t\t}\n\t}\n\n\tif opts.Logger == nil {\n\t\topts.Logger = defaultLogger\n\t}\n\n\tp := &poolCommon{\n\t\tcapacity: int32(size),\n\t\tallDone:  make(chan struct{}),\n\t\tlock:     syncx.NewSpinLock(),\n\t\tonce:     &sync.Once{},\n\t\toptions:  opts,\n\t}\n\tif p.options.PreAlloc {\n\t\tif size == -1 {\n\t\t\treturn nil, ErrInvalidPreAllocSize\n\t\t}\n\t\tp.workers = newWorkerQueue(queueTypeLoopQueue, size)\n\t} else {\n\t\tp.workers = newWorkerQueue(queueTypeStack, 0)\n\t}\n\n\tp.cond = sync.NewCond(p.lock)\n\n\tp.goPurge()\n\tp.goTicktock()\n\n\treturn p, nil\n}\n\n// purgeStaleWorkers clears stale workers periodically, it runs in an individual goroutine, as a scavenger.\nfunc (p *poolCommon) purgeStaleWorkers() {\n\tticker := time.NewTicker(p.options.ExpiryDuration)\n\n\tdefer func() {\n\t\tticker.Stop()\n\t\tatomic.StoreInt32(&p.purgeDone, 1)\n\t}()\n\n\tpurgeCtx := p.purgeCtx // copy to the local variable to avoid race from Reboot()\n\tfor {\n\t\tselect {\n\t\tcase <-purgeCtx.Done():\n\t\t\treturn\n\t\tcase <-ticker.C:\n\t\t}\n\n\t\tif p.IsClosed() {\n\t\t\tbreak\n\t\t}\n\n\t\tvar isDormant bool\n\t\tp.lock.Lock()\n\t\tstaleWorkers := p.workers.refresh(p.options.ExpiryDuration)\n\t\tn := p.Running()\n\t\tisDormant = n == 0 || n == len(staleWorkers)\n\t\tp.lock.Unlock()\n\n\t\t// Clean up the stale workers.\n\t\tfor i := range staleWorkers {\n\t\t\tstaleWorkers[i].finish()\n\t\t\tstaleWorkers[i] = nil\n\t\t}\n\n\t\t// There might be a situation where all workers have been cleaned up (no worker is running),\n\t\t// while some invokers still are stuck in p.cond.Wait(), then we need to awake those invokers.\n\t\tif isDormant && p.Waiting() > 0 {\n\t\t\tp.cond.Broadcast()\n\t\t}\n\t}\n}\n\nconst nowTimeUpdateInterval = 500 * time.Millisecond\n\n// ticktock is a goroutine that updates the current time in the pool regularly.\nfunc (p *poolCommon) ticktock() {\n\tticker := time.NewTicker(nowTimeUpdateInterval)\n\tdefer func() {\n\t\tticker.Stop()\n\t\tatomic.StoreInt32(&p.ticktockDone, 1)\n\t}()\n\n\tticktockCtx := p.ticktockCtx // copy to the local variable to avoid race from Reboot()\n\tfor {\n\t\tselect {\n\t\tcase <-ticktockCtx.Done():\n\t\t\treturn\n\t\tcase <-ticker.C:\n\t\t}\n\n\t\tif p.IsClosed() {\n\t\t\tbreak\n\t\t}\n\n\t\tatomic.StoreInt64(&p.now, time.Now().UnixNano())\n\t}\n}\n\nfunc (p *poolCommon) goPurge() {\n\tif p.options.DisablePurge {\n\t\treturn\n\t}\n\n\t// Start a goroutine to clean up expired workers periodically.\n\tp.purgeCtx, p.stopPurge = context.WithCancel(context.Background())\n\tgo p.purgeStaleWorkers()\n}\n\nfunc (p *poolCommon) goTicktock() {\n\tatomic.StoreInt64(&p.now, time.Now().UnixNano())\n\tp.ticktockCtx, p.stopTicktock = context.WithCancel(context.Background())\n\tgo p.ticktock()\n}\n\nfunc (p *poolCommon) nowTime() int64 {\n\treturn atomic.LoadInt64(&p.now)\n}\n\n// Running returns the number of workers currently running.\nfunc (p *poolCommon) Running() int {\n\treturn int(atomic.LoadInt32(&p.running))\n}\n\n// Free returns the number of available workers, -1 indicates this pool is unlimited.\nfunc (p *poolCommon) Free() int {\n\tc := p.Cap()\n\tif c < 0 {\n\t\treturn -1\n\t}\n\treturn c - p.Running()\n}\n\n// Waiting returns the number of tasks waiting to be executed.\nfunc (p *poolCommon) Waiting() int {\n\treturn int(atomic.LoadInt32(&p.waiting))\n}\n\n// Cap returns the capacity of this pool.\nfunc (p *poolCommon) Cap() int {\n\treturn int(atomic.LoadInt32(&p.capacity))\n}\n\n// Tune changes the capacity of this pool, note that it is noneffective to the infinite or pre-allocation pool.\nfunc (p *poolCommon) Tune(size int) {\n\tcapacity := p.Cap()\n\tif capacity == -1 || size <= 0 || size == capacity || p.options.PreAlloc {\n\t\treturn\n\t}\n\tatomic.StoreInt32(&p.capacity, int32(size))\n\tif size > capacity {\n\t\tif size-capacity == 1 {\n\t\t\tp.cond.Signal()\n\t\t\treturn\n\t\t}\n\t\tp.cond.Broadcast()\n\t}\n}\n\n// IsClosed indicates whether the pool is closed.\nfunc (p *poolCommon) IsClosed() bool {\n\treturn atomic.LoadInt32(&p.state) == CLOSED\n}\n\n// Release closes this pool and releases the worker queue.\nfunc (p *poolCommon) Release() {\n\tif !atomic.CompareAndSwapInt32(&p.state, OPENED, CLOSED) {\n\t\treturn\n\t}\n\n\tif p.stopPurge != nil {\n\t\tp.stopPurge()\n\t\tp.stopPurge = nil\n\t}\n\tif p.stopTicktock != nil {\n\t\tp.stopTicktock()\n\t\tp.stopTicktock = nil\n\t}\n\n\tp.lock.Lock()\n\tp.workers.reset()\n\tp.lock.Unlock()\n\n\t// There might be some callers waiting in retrieveWorker(), so we need to wake them up to prevent\n\t// those callers blocking infinitely.\n\tp.cond.Broadcast()\n}\n\n// ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.\nfunc (p *poolCommon) ReleaseTimeout(timeout time.Duration) error {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\terr := p.ReleaseContext(ctx)\n\tif errors.Is(err, context.DeadlineExceeded) {\n\t\treturn ErrTimeout\n\t}\n\treturn err\n}\n\n// ReleaseContext is like Release but with a context, it waits all workers to exit before the context is done.\n//\n// Note that if the context is nil, it is the same as Release,\n// just return immediately without waiting for all workers to exit.\nfunc (p *poolCommon) ReleaseContext(ctx context.Context) error {\n\tif p.IsClosed() || (!p.options.DisablePurge && p.stopPurge == nil) || p.stopTicktock == nil {\n\t\treturn ErrPoolClosed\n\t}\n\n\tp.Release()\n\n\t// Don't wait for all workers to exit, just return immediately if the context is nil.\n\tif ctx == nil {\n\t\treturn nil\n\t}\n\n\tvar purgeCh <-chan struct{}\n\tif !p.options.DisablePurge {\n\t\tpurgeCh = p.purgeCtx.Done()\n\t} else {\n\t\tpurgeCh = p.allDone\n\t}\n\n\tif p.Running() == 0 {\n\t\tp.once.Do(func() {\n\t\t\tclose(p.allDone)\n\t\t})\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\treturn ctx.Err()\n\t\tcase <-p.allDone:\n\t\t\t<-purgeCh\n\t\t\t<-p.ticktockCtx.Done()\n\t\t\tif p.Running() == 0 &&\n\t\t\t\t(p.options.DisablePurge || atomic.LoadInt32(&p.purgeDone) == 1) &&\n\t\t\t\tatomic.LoadInt32(&p.ticktockDone) == 1 {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\t}\n}\n\n// Reboot reboots a closed pool, it does nothing if the pool is not closed.\n// If you intend to reboot a closed pool, use ReleaseTimeout() instead of\n// Release() to ensure that all workers are stopped and resource are released\n// before rebooting, otherwise you may run into data race.\nfunc (p *poolCommon) Reboot() {\n\tif atomic.CompareAndSwapInt32(&p.state, CLOSED, OPENED) {\n\t\tatomic.StoreInt32(&p.purgeDone, 0)\n\t\tp.goPurge()\n\t\tatomic.StoreInt32(&p.ticktockDone, 0)\n\t\tp.goTicktock()\n\t\tp.allDone = make(chan struct{})\n\t\tp.once = &sync.Once{}\n\t}\n}\n\nfunc (p *poolCommon) addRunning(delta int) int {\n\treturn int(atomic.AddInt32(&p.running, int32(delta)))\n}\n\nfunc (p *poolCommon) addWaiting(delta int) {\n\tatomic.AddInt32(&p.waiting, int32(delta))\n}\n\n// retrieveWorker returns an available worker to run the tasks.\nfunc (p *poolCommon) retrieveWorker() (w worker, err error) {\n\tp.lock.Lock()\n\nretry:\n\t// First try to fetch the worker from the queue.\n\tif w = p.workers.detach(); w != nil {\n\t\tp.lock.Unlock()\n\t\treturn\n\t}\n\n\t// If the worker queue is empty, and we don't run out of the pool capacity,\n\t// then just spawn a new worker goroutine.\n\tif capacity := p.Cap(); capacity == -1 || capacity > p.Running() {\n\t\tw = p.workerCache.Get().(worker)\n\t\tw.run()\n\t\tp.lock.Unlock()\n\t\treturn\n\t}\n\n\t// Bail out early if it's in nonblocking mode or the number of pending callers reaches the maximum limit value.\n\tif p.options.Nonblocking || (p.options.MaxBlockingTasks != 0 && p.Waiting() >= p.options.MaxBlockingTasks) {\n\t\tp.lock.Unlock()\n\t\treturn nil, ErrPoolOverload\n\t}\n\n\t// Otherwise, we'll have to keep them blocked and wait for at least one worker to be put back into pool.\n\tp.addWaiting(1)\n\tp.cond.Wait() // block and wait for an available worker\n\tp.addWaiting(-1)\n\n\tif p.IsClosed() {\n\t\tp.lock.Unlock()\n\t\treturn nil, ErrPoolClosed\n\t}\n\n\tgoto retry\n}\n\n// revertWorker puts a worker back into free pool, recycling the goroutines.\nfunc (p *poolCommon) revertWorker(worker worker) bool {\n\tif capacity := p.Cap(); (capacity > 0 && p.Running() > capacity) || p.IsClosed() {\n\t\tp.cond.Broadcast()\n\t\treturn false\n\t}\n\n\tworker.setLastUsedTime(p.nowTime())\n\n\tp.lock.Lock()\n\t// To avoid memory leaks, add a double check in the lock scope.\n\t// Issue: https://github.com/panjf2000/ants/issues/113\n\tif p.IsClosed() {\n\t\tp.lock.Unlock()\n\t\treturn false\n\t}\n\tif err := p.workers.insert(worker); err != nil {\n\t\tp.lock.Unlock()\n\t\treturn false\n\t}\n\t// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.\n\tp.cond.Signal()\n\tp.lock.Unlock()\n\n\treturn true\n}\n"
  },
  {
    "path": "ants_benchmark_test.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants_test\n\nimport (\n\t\"runtime\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"testing\"\n\t\"time\"\n\n\t\"golang.org/x/sync/errgroup\"\n\n\t\"github.com/panjf2000/ants/v2\"\n)\n\nconst (\n\tRunTimes           = 1e6\n\tPoolCap            = 5e4\n\tBenchParam         = 10\n\tDefaultExpiredTime = 10 * time.Second\n)\n\nfunc demoFunc() {\n\ttime.Sleep(time.Duration(BenchParam) * time.Millisecond)\n}\n\nfunc demoPoolFunc(args any) {\n\tn := args.(int)\n\ttime.Sleep(time.Duration(n) * time.Millisecond)\n}\n\nfunc demoPoolFuncInt(n int) {\n\ttime.Sleep(time.Duration(n) * time.Millisecond)\n}\n\nvar stopLongRunningFunc int32\n\nfunc longRunningFunc() {\n\tfor atomic.LoadInt32(&stopLongRunningFunc) == 0 {\n\t\truntime.Gosched()\n\t}\n}\n\nfunc longRunningPoolFunc(arg any) {\n\t<-arg.(chan struct{})\n}\n\nfunc longRunningPoolFuncCh(ch chan struct{}) {\n\t<-ch\n}\n\nfunc BenchmarkGoroutines(b *testing.B) {\n\tvar wg sync.WaitGroup\n\tfor i := 0; i < b.N; i++ {\n\t\twg.Add(RunTimes)\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\tgo func() {\n\t\t\t\tdemoFunc()\n\t\t\t\twg.Done()\n\t\t\t}()\n\t\t}\n\t\twg.Wait()\n\t}\n}\n\nfunc BenchmarkChannel(b *testing.B) {\n\tvar wg sync.WaitGroup\n\tsema := make(chan struct{}, PoolCap)\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\twg.Add(RunTimes)\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\tsema <- struct{}{}\n\t\t\tgo func() {\n\t\t\t\tdemoFunc()\n\t\t\t\t<-sema\n\t\t\t\twg.Done()\n\t\t\t}()\n\t\t}\n\t\twg.Wait()\n\t}\n}\n\nfunc BenchmarkErrGroup(b *testing.B) {\n\tvar wg sync.WaitGroup\n\tvar pool errgroup.Group\n\tpool.SetLimit(PoolCap)\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\twg.Add(RunTimes)\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\tpool.Go(func() error {\n\t\t\t\tdemoFunc()\n\t\t\t\twg.Done()\n\t\t\t\treturn nil\n\t\t\t})\n\t\t}\n\t\twg.Wait()\n\t}\n}\n\nfunc BenchmarkAntsPool(b *testing.B) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPool(PoolCap, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.Release()\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\twg.Add(RunTimes)\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\t_ = p.Submit(func() {\n\t\t\t\tdemoFunc()\n\t\t\t\twg.Done()\n\t\t\t})\n\t\t}\n\t\twg.Wait()\n\t}\n}\n\nfunc BenchmarkAntsMultiPool(b *testing.B) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewMultiPool(10, PoolCap/10, ants.RoundRobin, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.ReleaseTimeout(DefaultExpiredTime) //nolint:errcheck\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\twg.Add(RunTimes)\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\t_ = p.Submit(func() {\n\t\t\t\tdemoFunc()\n\t\t\t\twg.Done()\n\t\t\t})\n\t\t}\n\t\twg.Wait()\n\t}\n}\n\nfunc BenchmarkGoroutinesThroughput(b *testing.B) {\n\tfor i := 0; i < b.N; i++ {\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\tgo demoFunc()\n\t\t}\n\t}\n}\n\nfunc BenchmarkSemaphoreThroughput(b *testing.B) {\n\tsema := make(chan struct{}, PoolCap)\n\tfor i := 0; i < b.N; i++ {\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\tsema <- struct{}{}\n\t\t\tgo func() {\n\t\t\t\tdemoFunc()\n\t\t\t\t<-sema\n\t\t\t}()\n\t\t}\n\t}\n}\n\nfunc BenchmarkAntsPoolThroughput(b *testing.B) {\n\tp, _ := ants.NewPool(PoolCap, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.Release()\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\t_ = p.Submit(demoFunc)\n\t\t}\n\t}\n}\n\nfunc BenchmarkAntsMultiPoolThroughput(b *testing.B) {\n\tp, _ := ants.NewMultiPool(10, PoolCap/10, ants.RoundRobin, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.ReleaseTimeout(DefaultExpiredTime) //nolint:errcheck\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tfor j := 0; j < RunTimes; j++ {\n\t\t\t_ = p.Submit(demoFunc)\n\t\t}\n\t}\n}\n\nfunc BenchmarkParallelAntsPoolThroughput(b *testing.B) {\n\tp, _ := ants.NewPool(PoolCap, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.Release()\n\n\tb.ResetTimer()\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tfor pb.Next() {\n\t\t\t_ = p.Submit(demoFunc)\n\t\t}\n\t})\n}\n\nfunc BenchmarkParallelAntsMultiPoolThroughput(b *testing.B) {\n\tp, _ := ants.NewMultiPool(10, PoolCap/10, ants.RoundRobin, ants.WithExpiryDuration(DefaultExpiredTime))\n\tdefer p.ReleaseTimeout(DefaultExpiredTime) //nolint:errcheck\n\n\tb.ResetTimer()\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tfor pb.Next() {\n\t\t\t_ = p.Submit(demoFunc)\n\t\t}\n\t})\n}\n"
  },
  {
    "path": "ants_test.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants_test\n\nimport (\n\t\"context\"\n\t\"log\"\n\t\"os\"\n\t\"runtime\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"github.com/panjf2000/ants/v2\"\n)\n\nconst (\n\t_   = 1 << (10 * iota)\n\tKiB // 1024\n\tMiB // 1048576\n)\n\nconst (\n\tParam    = 100\n\tAntsSize = 1000\n\tTestSize = 10000\n\tn        = 100000\n)\n\nvar curMem uint64\n\n// TestAntsPoolWaitToGetWorker is used to test waiting to get worker.\nfunc TestAntsPoolWaitToGetWorker(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPool(AntsSize)\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Submit(func() {\n\t\t\tdemoPoolFunc(Param)\n\t\t\twg.Done()\n\t\t})\n\t}\n\twg.Wait()\n\tt.Logf(\"pool, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPoolWaitToGetWorkerPreMalloc(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPool(AntsSize, ants.WithPreAlloc(true))\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Submit(func() {\n\t\t\tdemoPoolFunc(Param)\n\t\t\twg.Done()\n\t\t})\n\t}\n\twg.Wait()\n\tt.Logf(\"pool, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// TestAntsPoolWithFuncWaitToGetWorker is used to test waiting to get worker.\nfunc TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPoolWithFunc(AntsSize, func(i any) {\n\t\tdemoPoolFunc(i)\n\t\twg.Done()\n\t})\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Invoke(Param)\n\t}\n\twg.Wait()\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// TestAntsPoolWithFuncGenericWaitToGetWorker is used to test waiting to get worker.\nfunc TestAntsPoolWithFuncGenericWaitToGetWorker(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPoolWithFuncGeneric(AntsSize, func(i int) {\n\t\tdemoPoolFuncInt(i)\n\t\twg.Done()\n\t})\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Invoke(Param)\n\t}\n\twg.Wait()\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPoolWithFuncWaitToGetWorkerPreMalloc(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPoolWithFunc(AntsSize, func(i any) {\n\t\tdemoPoolFunc(i)\n\t\twg.Done()\n\t}, ants.WithPreAlloc(true))\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Invoke(Param)\n\t}\n\twg.Wait()\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPoolWithFuncGenericWaitToGetWorkerPreMalloc(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, _ := ants.NewPoolWithFuncGeneric(AntsSize, func(i int) {\n\t\tdemoPoolFuncInt(i)\n\t\twg.Done()\n\t}, ants.WithPreAlloc(true))\n\tdefer p.Release()\n\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = p.Invoke(Param)\n\t}\n\twg.Wait()\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool.\nfunc TestAntsPoolGetWorkerFromCache(t *testing.T) {\n\tp, _ := ants.NewPool(TestSize)\n\tdefer p.Release()\n\n\tfor i := 0; i < AntsSize; i++ {\n\t\t_ = p.Submit(demoFunc)\n\t}\n\ttime.Sleep(2 * ants.DefaultCleanIntervalTime)\n\t_ = p.Submit(demoFunc)\n\tt.Logf(\"pool, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool.\nfunc TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) {\n\tdur := 10\n\tp, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc)\n\tdefer p.Release()\n\n\tfor i := 0; i < AntsSize; i++ {\n\t\t_ = p.Invoke(dur)\n\t}\n\ttime.Sleep(2 * ants.DefaultCleanIntervalTime)\n\t_ = p.Invoke(dur)\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// TestAntsPoolWithFuncGenericGetWorkerFromCache is used to test getting worker from sync.Pool.\nfunc TestAntsPoolWithFuncGenericGetWorkerFromCache(t *testing.T) {\n\tdur := 10\n\tp, _ := ants.NewPoolWithFuncGeneric(TestSize, demoPoolFuncInt)\n\tdefer p.Release()\n\n\tfor i := 0; i < AntsSize; i++ {\n\t\t_ = p.Invoke(dur)\n\t}\n\ttime.Sleep(2 * ants.DefaultCleanIntervalTime)\n\t_ = p.Invoke(dur)\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPoolWithFuncGetWorkerFromCachePreMalloc(t *testing.T) {\n\tdur := 10\n\tp, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc, ants.WithPreAlloc(true))\n\tdefer p.Release()\n\n\tfor i := 0; i < AntsSize; i++ {\n\t\t_ = p.Invoke(dur)\n\t}\n\ttime.Sleep(2 * ants.DefaultCleanIntervalTime)\n\t_ = p.Invoke(dur)\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPoolWithFuncGenericGetWorkerFromCachePreMalloc(t *testing.T) {\n\tdur := 10\n\tp, _ := ants.NewPoolWithFuncGeneric(TestSize, demoPoolFuncInt, ants.WithPreAlloc(true))\n\tdefer p.Release()\n\n\tfor i := 0; i < AntsSize; i++ {\n\t\t_ = p.Invoke(dur)\n\t}\n\ttime.Sleep(2 * ants.DefaultCleanIntervalTime)\n\t_ = p.Invoke(dur)\n\tt.Logf(\"pool with func, running workers number:%d\", p.Running())\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\n// Contrast between goroutines without a pool and goroutines with ants pool.\n\nfunc TestNoPool(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdemoFunc()\n\t\t\twg.Done()\n\t\t}()\n\t}\n\n\twg.Wait()\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestAntsPool(t *testing.T) {\n\tdefer ants.Release()\n\tvar wg sync.WaitGroup\n\tfor i := 0; i < n; i++ {\n\t\twg.Add(1)\n\t\t_ = ants.Submit(func() {\n\t\t\tdemoFunc()\n\t\t\twg.Done()\n\t\t})\n\t}\n\twg.Wait()\n\n\tt.Logf(\"pool, capacity:%d\", ants.Cap())\n\tt.Logf(\"pool, running workers number:%d\", ants.Running())\n\tt.Logf(\"pool, free workers number:%d\", ants.Free())\n\n\tmem := runtime.MemStats{}\n\truntime.ReadMemStats(&mem)\n\tcurMem = mem.TotalAlloc/MiB - curMem\n\tt.Logf(\"memory usage:%d MB\", curMem)\n}\n\nfunc TestPanicHandler(t *testing.T) {\n\tvar panicCounter int64\n\tvar wg sync.WaitGroup\n\tp0, err := ants.NewPool(10, ants.WithPanicHandler(func(p any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t\tt.Logf(\"catch panic with PanicHandler: %v\", p)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool failed: %v\", err)\n\tdefer p0.Release()\n\twg.Add(1)\n\t_ = p0.Submit(func() {\n\t\tpanic(\"Oops!\")\n\t})\n\twg.Wait()\n\tc := atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 1, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p0.Running(), \"pool should be empty after panic\")\n\n\tp1, err := ants.NewPoolWithFunc(10, func(p any) { panic(p) }, ants.WithPanicHandler(func(_ any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p1.Release()\n\twg.Add(1)\n\t_ = p1.Invoke(\"Oops!\")\n\twg.Wait()\n\tc = atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 2, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p1.Running(), \"pool should be empty after panic\")\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, func(s string) { panic(s) }, ants.WithPanicHandler(func(_ any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p2.Release()\n\twg.Add(1)\n\t_ = p2.Invoke(\"Oops!\")\n\twg.Wait()\n\tc = atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 3, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p2.Running(), \"pool should be empty after panic\")\n}\n\nfunc TestPanicHandlerPreMalloc(t *testing.T) {\n\tvar panicCounter int64\n\tvar wg sync.WaitGroup\n\tp0, err := ants.NewPool(10, ants.WithPreAlloc(true), ants.WithPanicHandler(func(p any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t\tt.Logf(\"catch panic with PanicHandler: %v\", p)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool failed: %v\", err)\n\tdefer p0.Release()\n\twg.Add(1)\n\t_ = p0.Submit(func() {\n\t\tpanic(\"Oops!\")\n\t})\n\twg.Wait()\n\tc := atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 1, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p0.Running(), \"pool should be empty after panic\")\n\n\tp1, err := ants.NewPoolWithFunc(10, func(p any) { panic(p) }, ants.WithPreAlloc(true), ants.WithPanicHandler(func(_ any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p1.Release()\n\twg.Add(1)\n\t_ = p1.Invoke(\"Oops!\")\n\twg.Wait()\n\tc = atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 2, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p1.Running(), \"pool should be empty after panic\")\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, func(p string) { panic(p) }, ants.WithPreAlloc(true), ants.WithPanicHandler(func(_ any) {\n\t\tdefer wg.Done()\n\t\tatomic.AddInt64(&panicCounter, 1)\n\t}))\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p2.Release()\n\twg.Add(1)\n\t_ = p2.Invoke(\"Oops!\")\n\twg.Wait()\n\tc = atomic.LoadInt64(&panicCounter)\n\trequire.EqualValuesf(t, 3, c, \"panic handler didn't work, panicCounter: %d\", c)\n\trequire.EqualValues(t, 0, p1.Running(), \"pool should be empty after panic\")\n}\n\nfunc TestPoolPanicWithoutHandler(t *testing.T) {\n\tp0, err := ants.NewPool(10)\n\trequire.NoErrorf(t, err, \"create new pool failed: %v\", err)\n\tdefer p0.Release()\n\t_ = p0.Submit(func() {\n\t\tpanic(\"Oops!\")\n\t})\n\n\tp1, err := ants.NewPoolWithFunc(10, func(p any) { panic(p) })\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p1.Release()\n\t_ = p1.Invoke(\"Oops!\")\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, func(p string) { panic(p) })\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p2.Release()\n\t_ = p2.Invoke(\"Oops!\")\n}\n\nfunc TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) {\n\tp0, err := ants.NewPool(10, ants.WithPreAlloc(true))\n\trequire.NoErrorf(t, err, \"create new pool failed: %v\", err)\n\tdefer p0.Release()\n\t_ = p0.Submit(func() {\n\t\tpanic(\"Oops!\")\n\t})\n\n\tp1, err := ants.NewPoolWithFunc(10, func(p any) {\n\t\tpanic(p)\n\t})\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p1.Release()\n\t_ = p1.Invoke(\"Oops!\")\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, func(p any) {\n\t\tpanic(p)\n\t})\n\trequire.NoErrorf(t, err, \"create new pool with func failed: %v\", err)\n\tdefer p2.Release()\n\t_ = p2.Invoke(\"Oops!\")\n}\n\nfunc TestPurgePool(t *testing.T) {\n\tsize := 500\n\tch := make(chan struct{})\n\n\tp, err := ants.NewPool(size)\n\trequire.NoErrorf(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\n\tfor i := 0; i < size; i++ {\n\t\tj := i + 1\n\t\t_ = p.Submit(func() {\n\t\t\t<-ch\n\t\t\td := j % 100\n\t\t\ttime.Sleep(time.Duration(d) * time.Millisecond)\n\t\t})\n\t}\n\trequire.EqualValuesf(t, size, p.Running(), \"pool should be full, expected: %d, but got: %d\", size, p.Running())\n\n\tclose(ch)\n\ttime.Sleep(5 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValuesf(t, 0, p.Running(), \"pool should be empty after purge, but got %d\", p.Running())\n\n\tch = make(chan struct{})\n\tf := func(i any) {\n\t\t<-ch\n\t\td := i.(int) % 100\n\t\ttime.Sleep(time.Duration(d) * time.Millisecond)\n\t}\n\n\tp1, err := ants.NewPoolWithFunc(size, f)\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p1.Release()\n\n\tfor i := 0; i < size; i++ {\n\t\t_ = p1.Invoke(i)\n\t}\n\trequire.EqualValuesf(t, size, p1.Running(), \"pool should be full, expected: %d, but got: %d\", size, p1.Running())\n\n\tclose(ch)\n\ttime.Sleep(5 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValuesf(t, 0, p1.Running(), \"pool should be empty after purge, but got %d\", p1.Running())\n\n\tch = make(chan struct{})\n\tf1 := func(i int) {\n\t\t<-ch\n\t\td := i % 100\n\t\ttime.Sleep(time.Duration(d) * time.Millisecond)\n\t}\n\n\tp2, err := ants.NewPoolWithFuncGeneric(size, f1)\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p2.Release()\n\n\tfor i := 0; i < size; i++ {\n\t\t_ = p2.Invoke(i)\n\t}\n\trequire.EqualValuesf(t, size, p2.Running(), \"pool should be full, expected: %d, but got: %d\", size, p2.Running())\n\n\tclose(ch)\n\ttime.Sleep(5 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValuesf(t, 0, p2.Running(), \"pool should be empty after purge, but got %d\", p2.Running())\n}\n\nfunc TestPurgePreMallocPool(t *testing.T) {\n\tp, err := ants.NewPool(10, ants.WithPreAlloc(true))\n\trequire.NoErrorf(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\t_ = p.Submit(demoFunc)\n\ttime.Sleep(3 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValues(t, 0, p.Running(), \"all p should be purged\")\n\n\tp1, err := ants.NewPoolWithFunc(10, demoPoolFunc)\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p1.Release()\n\t_ = p1.Invoke(1)\n\ttime.Sleep(3 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValues(t, 0, p1.Running(), \"all p should be purged\")\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, demoPoolFuncInt)\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p2.Release()\n\t_ = p2.Invoke(1)\n\ttime.Sleep(3 * ants.DefaultCleanIntervalTime)\n\trequire.EqualValues(t, 0, p2.Running(), \"all p should be purged\")\n}\n\nfunc TestNonblockingSubmit(t *testing.T) {\n\tpoolSize := 10\n\tp, err := ants.NewPool(poolSize, ants.WithNonblocking(true))\n\trequire.NoErrorf(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Submit(longRunningFunc), \"nonblocking submit when pool is not full shouldn't return error\")\n\t}\n\tch := make(chan struct{})\n\tch1 := make(chan struct{})\n\tf := func() {\n\t\t<-ch\n\t\tclose(ch1)\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Submit(f), \"nonblocking submit when pool is not full shouldn't return error\")\n\trequire.ErrorIsf(t, p.Submit(demoFunc), ants.ErrPoolOverload,\n\t\t\"nonblocking submit when pool is full should get an ants.ErrPoolOverload\")\n\t// interrupt f to get an available worker\n\tclose(ch)\n\t<-ch1\n\trequire.NoError(t, p.Submit(demoFunc), \"nonblocking submit when pool is not full shouldn't return error\")\n}\n\nfunc TestMaxBlockingSubmit(t *testing.T) {\n\tpoolSize := 10\n\tp, err := ants.NewPool(poolSize, ants.WithMaxBlockingTasks(1))\n\trequire.NoErrorf(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Submit(longRunningFunc), \"submit when pool is not full shouldn't return error\")\n\t}\n\tch := make(chan struct{})\n\tf := func() {\n\t\t<-ch\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Submit(f), \"submit when pool is not full shouldn't return error\")\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\terrCh := make(chan error, 1)\n\tgo func() {\n\t\t// should be blocked. blocking num == 1\n\t\tif err := p.Submit(demoFunc); err != nil {\n\t\t\terrCh <- err\n\t\t}\n\t\twg.Done()\n\t}()\n\ttime.Sleep(1 * time.Second)\n\t// already reached max blocking limit\n\trequire.ErrorIsf(t, p.Submit(demoFunc), ants.ErrPoolOverload,\n\t\t\"blocking submit when pool reach max blocking submit should return ants.ErrPoolOverload\")\n\t// interrupt f to make blocking submit successful.\n\tclose(ch)\n\twg.Wait()\n\tselect {\n\tcase <-errCh:\n\t\tt.Fatalf(\"blocking submit when pool is full should not return error\")\n\tdefault:\n\t}\n}\n\nfunc TestNonblockingSubmitWithFunc(t *testing.T) {\n\tpoolSize := 10\n\tch := make(chan struct{})\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPoolWithFunc(poolSize, func(i any) {\n\t\tlongRunningPoolFunc(i)\n\t\twg.Done()\n\t}, ants.WithNonblocking(true))\n\trequire.NoError(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\twg.Add(poolSize)\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\trequire.ErrorIsf(t, p.Invoke(nil), ants.ErrPoolOverload,\n\t\t\"nonblocking submit when pool is full should get an ants.ErrPoolOverload\")\n\t// interrupt f to get an available worker\n\tclose(ch)\n\twg.Wait()\n\twg.Add(1)\n\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\twg.Wait()\n}\n\nfunc TestNonblockingSubmitWithFuncGeneric(t *testing.T) {\n\tpoolSize := 10\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPoolWithFuncGeneric(poolSize, func(ch chan struct{}) {\n\t\tlongRunningPoolFuncCh(ch)\n\t\twg.Done()\n\t}, ants.WithNonblocking(true))\n\trequire.NoError(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\tch := make(chan struct{})\n\twg.Add(poolSize)\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\trequire.ErrorIsf(t, p.Invoke(nil), ants.ErrPoolOverload,\n\t\t\"nonblocking submit when pool is full should get an ants.ErrPoolOverload\")\n\t// interrupt f to get an available worker\n\tclose(ch)\n\twg.Wait()\n\twg.Add(1)\n\trequire.NoError(t, p.Invoke(ch), \"nonblocking submit when pool is not full shouldn't return error\")\n\twg.Wait()\n}\n\nfunc TestMaxBlockingSubmitWithFunc(t *testing.T) {\n\tch := make(chan struct{})\n\tpoolSize := 10\n\tp, err := ants.NewPoolWithFunc(poolSize, longRunningPoolFunc, ants.WithMaxBlockingTasks(1))\n\trequire.NoError(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Invoke(ch), \"submit when pool is not full shouldn't return error\")\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Invoke(ch), \"submit when pool is not full shouldn't return error\")\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\terrCh := make(chan error, 1)\n\tgo func() {\n\t\t// should be blocked. blocking num == 1\n\t\tif err := p.Invoke(ch); err != nil {\n\t\t\terrCh <- err\n\t\t}\n\t\twg.Done()\n\t}()\n\ttime.Sleep(1 * time.Second)\n\t// already reached max blocking limit\n\trequire.ErrorIsf(t, p.Invoke(ch), ants.ErrPoolOverload,\n\t\t\"blocking submit when pool reach max blocking submit should return ants.ErrPoolOverload: %v\", err)\n\t// interrupt one func to make blocking submit successful.\n\tclose(ch)\n\twg.Wait()\n\tselect {\n\tcase <-errCh:\n\t\tt.Fatalf(\"blocking submit when pool is full should not return error\")\n\tdefault:\n\t}\n}\n\nfunc TestMaxBlockingSubmitWithFuncGeneric(t *testing.T) {\n\tpoolSize := 10\n\tp, err := ants.NewPoolWithFuncGeneric(poolSize, longRunningPoolFuncCh, ants.WithMaxBlockingTasks(1))\n\trequire.NoError(t, err, \"create TimingPool failed: %v\", err)\n\tdefer p.Release()\n\tch := make(chan struct{})\n\tfor i := 0; i < poolSize-1; i++ {\n\t\trequire.NoError(t, p.Invoke(ch), \"submit when pool is not full shouldn't return error\")\n\t}\n\t// p is full now.\n\trequire.NoError(t, p.Invoke(ch), \"submit when pool is not full shouldn't return error\")\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\terrCh := make(chan error, 1)\n\tgo func() {\n\t\t// should be blocked. blocking num == 1\n\t\tif err := p.Invoke(ch); err != nil {\n\t\t\terrCh <- err\n\t\t}\n\t\twg.Done()\n\t}()\n\ttime.Sleep(1 * time.Second)\n\t// already reached max blocking limit\n\trequire.ErrorIsf(t, p.Invoke(ch), ants.ErrPoolOverload,\n\t\t\"blocking submit when pool reach max blocking submit should return ants.ErrPoolOverload: %v\", err)\n\t// interrupt one func to make blocking submit successful.\n\tclose(ch)\n\twg.Wait()\n\tselect {\n\tcase <-errCh:\n\t\tt.Fatalf(\"blocking submit when pool is full should not return error\")\n\tdefault:\n\t}\n}\n\nfunc TestRebootDefaultPool(t *testing.T) {\n\tdefer ants.Release()\n\tants.Reboot() // should do nothing inside\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\t_ = ants.Submit(func() {\n\t\tdemoFunc()\n\t\twg.Done()\n\t})\n\twg.Wait()\n\trequire.NoError(t, ants.ReleaseTimeout(time.Second))\n\trequire.ErrorIsf(t, ants.Submit(nil), ants.ErrPoolClosed, \"pool should be closed\")\n\tants.Reboot()\n\twg.Add(1)\n\trequire.NoError(t, ants.Submit(func() { wg.Done() }), \"pool should be rebooted\")\n\twg.Wait()\n}\n\nfunc TestRebootNewPool(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPool(10)\n\trequire.NoErrorf(t, err, \"create Pool failed: %v\", err)\n\tdefer p.Release()\n\twg.Add(1)\n\t_ = p.Submit(func() {\n\t\tdemoFunc()\n\t\twg.Done()\n\t})\n\twg.Wait()\n\trequire.NoError(t, p.ReleaseTimeout(time.Second))\n\trequire.ErrorIsf(t, p.Submit(nil), ants.ErrPoolClosed, \"pool should be closed\")\n\tp.Reboot()\n\twg.Add(1)\n\trequire.NoError(t, p.Submit(func() { wg.Done() }), \"pool should be rebooted\")\n\twg.Wait()\n\n\tp1, err := ants.NewPoolWithFunc(10, func(i any) {\n\t\tdemoPoolFunc(i)\n\t\twg.Done()\n\t})\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p1.Release()\n\twg.Add(1)\n\t_ = p1.Invoke(1)\n\twg.Wait()\n\trequire.NoError(t, p1.ReleaseTimeout(time.Second))\n\trequire.ErrorIsf(t, p1.Invoke(nil), ants.ErrPoolClosed, \"pool should be closed\")\n\tp1.Reboot()\n\twg.Add(1)\n\trequire.NoError(t, p1.Invoke(1), \"pool should be rebooted\")\n\twg.Wait()\n\n\tp2, err := ants.NewPoolWithFuncGeneric(10, func(i int) {\n\t\tdemoPoolFuncInt(i)\n\t\twg.Done()\n\t})\n\trequire.NoErrorf(t, err, \"create TimingPoolWithFunc failed: %v\", err)\n\tdefer p2.Release()\n\twg.Add(1)\n\t_ = p2.Invoke(1)\n\twg.Wait()\n\trequire.NoError(t, p2.ReleaseTimeout(time.Second))\n\trequire.ErrorIsf(t, p2.Invoke(1), ants.ErrPoolClosed, \"pool should be closed\")\n\tp2.Reboot()\n\twg.Add(1)\n\trequire.NoError(t, p2.Invoke(1), \"pool should be rebooted\")\n\twg.Wait()\n}\n\nfunc TestInfinitePool(t *testing.T) {\n\tc := make(chan struct{})\n\tp, _ := ants.NewPool(-1)\n\t_ = p.Submit(func() {\n\t\t_ = p.Submit(func() {\n\t\t\t<-c\n\t\t})\n\t})\n\tc <- struct{}{}\n\tif n := p.Running(); n != 2 {\n\t\tt.Errorf(\"expect 2 workers running, but got %d\", n)\n\t}\n\tif n := p.Free(); n != -1 {\n\t\tt.Errorf(\"expect -1 of free workers by unlimited pool, but got %d\", n)\n\t}\n\tp.Tune(10)\n\tif capacity := p.Cap(); capacity != -1 {\n\t\tt.Fatalf(\"expect capacity: -1 but got %d\", capacity)\n\t}\n\tvar err error\n\t_, err = ants.NewPool(-1, ants.WithPreAlloc(true))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPreAllocSize)\n}\n\nfunc testPoolWithDisablePurge(t *testing.T, p *ants.Pool, numWorker int, waitForPurge time.Duration) {\n\tsig := make(chan struct{})\n\tvar wg1, wg2 sync.WaitGroup\n\twg1.Add(numWorker)\n\twg2.Add(numWorker)\n\tfor i := 0; i < numWorker; i++ {\n\t\t_ = p.Submit(func() {\n\t\t\twg1.Done()\n\t\t\t<-sig\n\t\t\twg2.Done()\n\t\t})\n\t}\n\twg1.Wait()\n\n\trunningCnt := p.Running()\n\trequire.EqualValuesf(t, numWorker, runningCnt, \"expect %d workers running, but got %d\", numWorker, runningCnt)\n\tfreeCnt := p.Free()\n\trequire.EqualValuesf(t, 0, freeCnt, \"expect %d free workers, but got %d\", 0, freeCnt)\n\n\t// Finish all tasks and sleep for a while to wait for purging, since we've disabled purge mechanism,\n\t// we should see that all workers are still running after the sleep.\n\tclose(sig)\n\twg2.Wait()\n\ttime.Sleep(waitForPurge + waitForPurge/2)\n\n\trunningCnt = p.Running()\n\trequire.EqualValuesf(t, numWorker, runningCnt, \"expect %d workers running, but got %d\", numWorker, runningCnt)\n\tfreeCnt = p.Free()\n\trequire.EqualValuesf(t, 0, freeCnt, \"expect %d free workers, but got %d\", 0, freeCnt)\n\n\terr := p.ReleaseTimeout(waitForPurge + waitForPurge/2)\n\trequire.NoErrorf(t, err, \"release pool failed: %v\", err)\n\n\trunningCnt = p.Running()\n\trequire.EqualValuesf(t, 0, runningCnt, \"expect %d workers running, but got %d\", 0, runningCnt)\n\tfreeCnt = p.Free()\n\trequire.EqualValuesf(t, numWorker, freeCnt, \"expect %d free workers, but got %d\", numWorker, freeCnt)\n}\n\nfunc TestWithDisablePurgePool(t *testing.T) {\n\tnumWorker := 10\n\tp, _ := ants.NewPool(numWorker, ants.WithDisablePurge(true))\n\ttestPoolWithDisablePurge(t, p, numWorker, ants.DefaultCleanIntervalTime)\n}\n\nfunc TestWithDisablePurgeAndWithExpirationPool(t *testing.T) {\n\tnumWorker := 10\n\texpiredDuration := time.Millisecond * 100\n\tp, _ := ants.NewPool(numWorker, ants.WithDisablePurge(true), ants.WithExpiryDuration(expiredDuration))\n\ttestPoolWithDisablePurge(t, p, numWorker, expiredDuration)\n}\n\nfunc testPoolFuncWithDisablePurge(t *testing.T, p *ants.PoolWithFunc, numWorker int, wg1, wg2 *sync.WaitGroup, sig chan struct{}, waitForPurge time.Duration) {\n\tfor i := 0; i < numWorker; i++ {\n\t\t_ = p.Invoke(i)\n\t}\n\twg1.Wait()\n\n\trunningCnt := p.Running()\n\trequire.EqualValuesf(t, numWorker, runningCnt, \"expect %d workers running, but got %d\", numWorker, runningCnt)\n\tfreeCnt := p.Free()\n\trequire.EqualValuesf(t, 0, freeCnt, \"expect %d free workers, but got %d\", 0, freeCnt)\n\n\t// Finish all tasks and sleep for a while to wait for purging, since we've disabled purge mechanism,\n\t// we should see that all workers are still running after the sleep.\n\tclose(sig)\n\twg2.Wait()\n\ttime.Sleep(waitForPurge + waitForPurge/2)\n\n\trunningCnt = p.Running()\n\trequire.EqualValuesf(t, numWorker, runningCnt, \"expect %d workers running, but got %d\", numWorker, runningCnt)\n\tfreeCnt = p.Free()\n\trequire.EqualValuesf(t, 0, freeCnt, \"expect %d free workers, but got %d\", 0, freeCnt)\n\n\terr := p.ReleaseTimeout(waitForPurge + waitForPurge/2)\n\trequire.NoErrorf(t, err, \"release pool failed: %v\", err)\n\n\trunningCnt = p.Running()\n\trequire.EqualValuesf(t, 0, runningCnt, \"expect %d workers running, but got %d\", 0, runningCnt)\n\tfreeCnt = p.Free()\n\trequire.EqualValuesf(t, numWorker, freeCnt, \"expect %d free workers, but got %d\", numWorker, freeCnt)\n}\n\nfunc TestWithDisablePurgePoolFunc(t *testing.T) {\n\tnumWorker := 10\n\tsig := make(chan struct{})\n\tvar wg1, wg2 sync.WaitGroup\n\twg1.Add(numWorker)\n\twg2.Add(numWorker)\n\tp, _ := ants.NewPoolWithFunc(numWorker, func(_ any) {\n\t\twg1.Done()\n\t\t<-sig\n\t\twg2.Done()\n\t}, ants.WithDisablePurge(true))\n\ttestPoolFuncWithDisablePurge(t, p, numWorker, &wg1, &wg2, sig, ants.DefaultCleanIntervalTime)\n}\n\nfunc TestWithDisablePurgeAndWithExpirationPoolFunc(t *testing.T) {\n\tnumWorker := 2\n\tsig := make(chan struct{})\n\tvar wg1, wg2 sync.WaitGroup\n\twg1.Add(numWorker)\n\twg2.Add(numWorker)\n\texpiredDuration := time.Millisecond * 100\n\tp, _ := ants.NewPoolWithFunc(numWorker, func(_ any) {\n\t\twg1.Done()\n\t\t<-sig\n\t\twg2.Done()\n\t}, ants.WithDisablePurge(true), ants.WithExpiryDuration(expiredDuration))\n\ttestPoolFuncWithDisablePurge(t, p, numWorker, &wg1, &wg2, sig, expiredDuration)\n}\n\nfunc TestInfinitePoolWithFunc(t *testing.T) {\n\tc := make(chan struct{})\n\tp, err := ants.NewPoolWithFunc(-1, func(i any) {\n\t\tdemoPoolFunc(i)\n\t\t<-c\n\t})\n\trequire.NoErrorf(t, err, \"create pool with func failed: %v\", err)\n\tdefer p.Release()\n\t_ = p.Invoke(10)\n\t_ = p.Invoke(10)\n\tc <- struct{}{}\n\tc <- struct{}{}\n\tif n := p.Running(); n != 2 {\n\t\tt.Errorf(\"expect 2 workers running, but got %d\", n)\n\t}\n\tif n := p.Free(); n != -1 {\n\t\tt.Errorf(\"expect -1 of free workers by unlimited pool, but got %d\", n)\n\t}\n\tp.Tune(10)\n\tif capacity := p.Cap(); capacity != -1 {\n\t\tt.Fatalf(\"expect capacity: -1 but got %d\", capacity)\n\t}\n\t_, err = ants.NewPoolWithFunc(-1, demoPoolFunc, ants.WithPreAlloc(true))\n\trequire.ErrorIsf(t, err, ants.ErrInvalidPreAllocSize, \"expect ErrInvalidPreAllocSize but got %v\", err)\n}\n\nfunc TestInfinitePoolWithFuncGeneric(t *testing.T) {\n\tc := make(chan struct{})\n\tp, err := ants.NewPoolWithFuncGeneric(-1, func(i int) {\n\t\tdemoPoolFuncInt(i)\n\t\t<-c\n\t})\n\trequire.NoErrorf(t, err, \"create pool with func failed: %v\", err)\n\tdefer p.Release()\n\t_ = p.Invoke(10)\n\t_ = p.Invoke(10)\n\tc <- struct{}{}\n\tc <- struct{}{}\n\tif n := p.Running(); n != 2 {\n\t\tt.Errorf(\"expect 2 workers running, but got %d\", n)\n\t}\n\tif n := p.Free(); n != -1 {\n\t\tt.Errorf(\"expect -1 of free workers by unlimited pool, but got %d\", n)\n\t}\n\tp.Tune(10)\n\tif capacity := p.Cap(); capacity != -1 {\n\t\tt.Fatalf(\"expect capacity: -1 but got %d\", capacity)\n\t}\n\t_, err = ants.NewPoolWithFuncGeneric(-1, demoPoolFuncInt, ants.WithPreAlloc(true))\n\trequire.ErrorIsf(t, err, ants.ErrInvalidPreAllocSize, \"expect ErrInvalidPreAllocSize but got %v\", err)\n}\n\nfunc TestReleaseWhenRunningPool(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPool(1)\n\trequire.NoErrorf(t, err, \"create pool failed: %v\", err)\n\twg.Add(2)\n\tgo func() {\n\t\tt.Log(\"start aaa\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop aaa\")\n\t\t}()\n\t\tfor i := 0; i < 30; i++ {\n\t\t\tj := i\n\t\t\t_ = p.Submit(func() {\n\t\t\t\tt.Log(\"do task\", j)\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t})\n\t\t}\n\t}()\n\n\tgo func() {\n\t\tt.Log(\"start bbb\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop bbb\")\n\t\t}()\n\t\tfor i := 100; i < 130; i++ {\n\t\t\tj := i\n\t\t\t_ = p.Submit(func() {\n\t\t\t\tt.Log(\"do task\", j)\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t})\n\t\t}\n\t}()\n\n\ttime.Sleep(3 * time.Second)\n\tp.Release()\n\tt.Log(\"wait for all goroutines to exit...\")\n\twg.Wait()\n}\n\nfunc TestReleaseWhenRunningPoolWithFunc(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPoolWithFunc(1, func(i any) {\n\t\tt.Log(\"do task\", i)\n\t\ttime.Sleep(1 * time.Second)\n\t})\n\trequire.NoErrorf(t, err, \"create pool with func failed: %v\", err)\n\n\twg.Add(2)\n\tgo func() {\n\t\tt.Log(\"start aaa\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop aaa\")\n\t\t}()\n\t\tfor i := 0; i < 30; i++ {\n\t\t\t_ = p.Invoke(i)\n\t\t}\n\t}()\n\n\tgo func() {\n\t\tt.Log(\"start bbb\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop bbb\")\n\t\t}()\n\t\tfor i := 100; i < 130; i++ {\n\t\t\t_ = p.Invoke(i)\n\t\t}\n\t}()\n\n\ttime.Sleep(3 * time.Second)\n\tp.Release()\n\tt.Log(\"wait for all goroutines to exit...\")\n\twg.Wait()\n}\n\nfunc TestReleaseWhenRunningPoolWithFuncGeneric(t *testing.T) {\n\tvar wg sync.WaitGroup\n\tp, err := ants.NewPoolWithFuncGeneric(1, func(i int) {\n\t\tt.Log(\"do task\", i)\n\t\ttime.Sleep(1 * time.Second)\n\t})\n\trequire.NoErrorf(t, err, \"create pool with func failed: %v\", err)\n\twg.Add(2)\n\n\tgo func() {\n\t\tt.Log(\"start aaa\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop aaa\")\n\t\t}()\n\t\tfor i := 0; i < 30; i++ {\n\t\t\t_ = p.Invoke(i)\n\t\t}\n\t}()\n\n\tgo func() {\n\t\tt.Log(\"start bbb\")\n\t\tdefer func() {\n\t\t\twg.Done()\n\t\t\tt.Log(\"stop bbb\")\n\t\t}()\n\t\tfor i := 100; i < 130; i++ {\n\t\t\t_ = p.Invoke(i)\n\t\t}\n\t}()\n\n\ttime.Sleep(3 * time.Second)\n\tp.Release()\n\tt.Log(\"wait for all goroutines to exit...\")\n\twg.Wait()\n}\n\nfunc TestRestCodeCoverage(t *testing.T) {\n\t_, err := ants.NewPool(-1, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\t_, err = ants.NewPool(1, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\t_, err = ants.NewPoolWithFunc(-1, demoPoolFunc, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\t_, err = ants.NewPoolWithFunc(1, demoPoolFunc, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\t_, err = ants.NewPoolWithFunc(1, nil, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrLackPoolFunc)\n\t_, err = ants.NewPoolWithFuncGeneric(-1, demoPoolFuncInt, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\t_, err = ants.NewPoolWithFuncGeneric(1, demoPoolFuncInt, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\tvar fn func(i int)\n\t_, err = ants.NewPoolWithFuncGeneric(1, fn, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrLackPoolFunc)\n\n\toptions := ants.Options{}\n\toptions.ExpiryDuration = time.Duration(10) * time.Second\n\toptions.Nonblocking = true\n\toptions.PreAlloc = true\n\tpoolOpts, _ := ants.NewPool(1, ants.WithOptions(options))\n\tt.Logf(\"Pool with options, capacity: %d\", poolOpts.Cap())\n\n\tp0, _ := ants.NewPool(TestSize, ants.WithLogger(log.New(os.Stderr, \"\", log.LstdFlags)))\n\tdefer func() {\n\t\t_ = p0.Submit(demoFunc)\n\t}()\n\tdefer p0.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p0.Submit(demoFunc)\n\t}\n\tt.Logf(\"pool, capacity:%d\", p0.Cap())\n\tt.Logf(\"pool, running workers number:%d\", p0.Running())\n\tt.Logf(\"pool, free workers number:%d\", p0.Free())\n\tp0.Tune(TestSize)\n\tp0.Tune(TestSize / 10)\n\tt.Logf(\"pool, after tuning capacity, capacity:%d, running:%d\", p0.Cap(), p0.Running())\n\n\tp1, _ := ants.NewPool(TestSize, ants.WithPreAlloc(true))\n\tdefer func() {\n\t\t_ = p1.Submit(demoFunc)\n\t}()\n\tdefer p1.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p1.Submit(demoFunc)\n\t}\n\tt.Logf(\"pre-malloc pool, capacity:%d\", p1.Cap())\n\tt.Logf(\"pre-malloc pool, running workers number:%d\", p1.Running())\n\tt.Logf(\"pre-malloc pool, free workers number:%d\", p1.Free())\n\tp1.Tune(TestSize)\n\tp1.Tune(TestSize / 10)\n\tt.Logf(\"pre-malloc pool, after tuning capacity, capacity:%d, running:%d\", p1.Cap(), p1.Running())\n\n\tp2, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc)\n\tdefer func() {\n\t\t_ = p2.Invoke(Param)\n\t}()\n\tdefer p2.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p2.Invoke(Param)\n\t}\n\ttime.Sleep(ants.DefaultCleanIntervalTime)\n\tt.Logf(\"pool with func, capacity:%d\", p2.Cap())\n\tt.Logf(\"pool with func, running workers number:%d\", p2.Running())\n\tt.Logf(\"pool with func, free workers number:%d\", p2.Free())\n\tp2.Tune(TestSize)\n\tp2.Tune(TestSize / 10)\n\tt.Logf(\"pool with func, after tuning capacity, capacity:%d, running:%d\", p2.Cap(), p2.Running())\n\n\tp3, _ := ants.NewPoolWithFuncGeneric(TestSize, demoPoolFuncInt)\n\tdefer func() {\n\t\t_ = p3.Invoke(Param)\n\t}()\n\tdefer p3.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p3.Invoke(Param)\n\t}\n\ttime.Sleep(ants.DefaultCleanIntervalTime)\n\tt.Logf(\"pool with func, capacity:%d\", p3.Cap())\n\tt.Logf(\"pool with func, running workers number:%d\", p3.Running())\n\tt.Logf(\"pool with func, free workers number:%d\", p3.Free())\n\tp3.Tune(TestSize)\n\tp3.Tune(TestSize / 10)\n\tt.Logf(\"pool with func, after tuning capacity, capacity:%d, running:%d\", p3.Cap(), p3.Running())\n\n\tp4, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc, ants.WithPreAlloc(true))\n\tdefer func() {\n\t\t_ = p4.Invoke(Param)\n\t}()\n\tdefer p4.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p4.Invoke(Param)\n\t}\n\ttime.Sleep(ants.DefaultCleanIntervalTime)\n\tt.Logf(\"pre-malloc pool with func, capacity:%d\", p4.Cap())\n\tt.Logf(\"pre-malloc pool with func, running workers number:%d\", p4.Running())\n\tt.Logf(\"pre-malloc pool with func, free workers number:%d\", p4.Free())\n\tp4.Tune(TestSize)\n\tp4.Tune(TestSize / 10)\n\tt.Logf(\"pre-malloc pool with func, after tuning capacity, capacity:%d, running:%d\", p4.Cap(),\n\t\tp4.Running())\n\n\tp5, _ := ants.NewPoolWithFuncGeneric(TestSize, demoPoolFuncInt, ants.WithPreAlloc(true))\n\tdefer func() {\n\t\t_ = p5.Invoke(Param)\n\t}()\n\tdefer p5.Release()\n\tfor i := 0; i < n; i++ {\n\t\t_ = p5.Invoke(Param)\n\t}\n\ttime.Sleep(ants.DefaultCleanIntervalTime)\n\tt.Logf(\"pre-malloc pool with func, capacity:%d\", p5.Cap())\n\tt.Logf(\"pre-malloc pool with func, running workers number:%d\", p5.Running())\n\tt.Logf(\"pre-malloc pool with func, free workers number:%d\", p5.Free())\n\tp5.Tune(TestSize)\n\tp5.Tune(TestSize / 10)\n\tt.Logf(\"pre-malloc pool with func, after tuning capacity, capacity:%d, running:%d\", p5.Cap(),\n\t\tp5.Running())\n}\n\nfunc TestPoolTuneScaleUp(t *testing.T) {\n\tc := make(chan struct{})\n\t// Test Pool\n\tp, _ := ants.NewPool(2)\n\tfor i := 0; i < 2; i++ {\n\t\t_ = p.Submit(func() {\n\t\t\t<-c\n\t\t})\n\t}\n\tn := p.Running()\n\trequire.EqualValuesf(t, 2, n, \"expect 2 workers running, but got %d\", p.Running())\n\t// test pool tune scale up one\n\tp.Tune(3)\n\t_ = p.Submit(func() {\n\t\t<-c\n\t})\n\tn = p.Running()\n\trequire.EqualValuesf(t, 3, n, \"expect 3 workers running, but got %d\", n)\n\t// test pool tune scale up multiple\n\tvar wg sync.WaitGroup\n\tfor i := 0; i < 5; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t_ = p.Submit(func() {\n\t\t\t\t<-c\n\t\t\t})\n\t\t}()\n\t}\n\tp.Tune(8)\n\twg.Wait()\n\tn = p.Running()\n\trequire.EqualValuesf(t, 8, n, \"expect 8 workers running, but got %d\", n)\n\tfor i := 0; i < 8; i++ {\n\t\tc <- struct{}{}\n\t}\n\tp.Release()\n\n\t// Test PoolWithFunc\n\tpf, _ := ants.NewPoolWithFunc(2, func(_ any) {\n\t\t<-c\n\t})\n\tfor i := 0; i < 2; i++ {\n\t\t_ = pf.Invoke(1)\n\t}\n\tn = pf.Running()\n\trequire.EqualValuesf(t, 2, n, \"expect 2 workers running, but got %d\", n)\n\t// test pool tune scale up one\n\tpf.Tune(3)\n\t_ = pf.Invoke(1)\n\tn = pf.Running()\n\trequire.EqualValuesf(t, 3, n, \"expect 3 workers running, but got %d\", n)\n\t// test pool tune scale up multiple\n\tfor i := 0; i < 5; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t_ = pf.Invoke(1)\n\t\t}()\n\t}\n\tpf.Tune(8)\n\twg.Wait()\n\tn = pf.Running()\n\trequire.EqualValuesf(t, 8, n, \"expect 8 workers running, but got %d\", n)\n\tfor i := 0; i < 8; i++ {\n\t\tc <- struct{}{}\n\t}\n\tpf.Release()\n\n\t// Test PoolWithFuncGeneric\n\tpfg, _ := ants.NewPoolWithFuncGeneric(2, func(_ int) {\n\t\t<-c\n\t})\n\tfor i := 0; i < 2; i++ {\n\t\t_ = pfg.Invoke(1)\n\t}\n\tn = pfg.Running()\n\trequire.EqualValuesf(t, 2, n, \"expect 2 workers running, but got %d\", n)\n\t// test pool tune scale up one\n\tpfg.Tune(3)\n\t_ = pfg.Invoke(1)\n\tn = pfg.Running()\n\trequire.EqualValuesf(t, 3, n, \"expect 3 workers running, but got %d\", n)\n\t// test pool tune scale up multiple\n\tfor i := 0; i < 5; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t_ = pfg.Invoke(1)\n\t\t}()\n\t}\n\tpfg.Tune(8)\n\twg.Wait()\n\tn = pfg.Running()\n\trequire.EqualValuesf(t, 8, n, \"expect 8 workers running, but got %d\", n)\n\tfor i := 0; i < 8; i++ {\n\t\tc <- struct{}{}\n\t}\n\tclose(c)\n\tpfg.Release()\n}\n\nfunc TestReleaseTimeout(t *testing.T) {\n\tp, err := ants.NewPool(10)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 5; i++ {\n\t\t_ = p.Submit(func() {\n\t\t\ttime.Sleep(time.Second)\n\t\t})\n\t}\n\trequire.NotZero(t, p.Running())\n\terr = p.ReleaseTimeout(2 * time.Second)\n\trequire.NoError(t, err)\n\n\tpf, err := ants.NewPoolWithFunc(10, func(i any) {\n\t\tdur := i.(time.Duration)\n\t\ttime.Sleep(dur)\n\t})\n\trequire.NoError(t, err)\n\tfor i := 0; i < 5; i++ {\n\t\t_ = pf.Invoke(time.Second)\n\t}\n\trequire.NotZero(t, pf.Running())\n\terr = pf.ReleaseTimeout(2 * time.Second)\n\trequire.NoError(t, err)\n\n\tpfg, err := ants.NewPoolWithFuncGeneric(10, func(d time.Duration) {\n\t\ttime.Sleep(d)\n\t})\n\trequire.NoError(t, err)\n\tfor i := 0; i < 5; i++ {\n\t\t_ = pfg.Invoke(time.Second)\n\t}\n\trequire.NotZero(t, pfg.Running())\n\terr = pfg.ReleaseTimeout(2 * time.Second)\n\trequire.NoError(t, err)\n}\n\nfunc TestDefaultPoolReleaseTimeout(t *testing.T) {\n\tants.Reboot() // should do nothing inside\n\tfor i := 0; i < 5; i++ {\n\t\t_ = ants.Submit(func() {\n\t\t\ttime.Sleep(time.Second)\n\t\t})\n\t}\n\trequire.NotZero(t, ants.Running())\n\terr := ants.ReleaseTimeout(2 * time.Second)\n\trequire.NoError(t, err)\n}\n\nfunc TestDefaultPoolReleaseContext(t *testing.T) {\n\tants.Reboot()\n\tfor i := 0; i < 5; i++ {\n\t\t_ = ants.Submit(func() {\n\t\t\ttime.Sleep(time.Second)\n\t\t})\n\t}\n\trequire.NotZero(t, ants.Running())\n\terr := ants.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n}\n\nfunc TestReleaseContextWithNil(t *testing.T) {\n\tp, err := ants.NewPool(10)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 5; i++ {\n\t\t_ = p.Submit(func() {\n\t\t\ttime.Sleep(time.Second)\n\t\t})\n\t}\n\trequire.NotZero(t, p.Running())\n\n\t// Passing nil context should release immediately without waiting for workers to exit.\n\terr = p.ReleaseContext(nil) //nolint:staticcheck\n\trequire.NoError(t, err)\n\trequire.True(t, p.IsClosed())\n}\n\nfunc TestMultiPool(t *testing.T) {\n\t_, err := ants.NewMultiPool(-1, 10, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidMultiPoolSize)\n\t_, err = ants.NewMultiPool(10, -1, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidLoadBalancingStrategy)\n\t_, err = ants.NewMultiPool(10, 10, ants.RoundRobin, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\n\tmp, err := ants.NewMultiPool(10, 5, ants.RoundRobin)\n\ttestFn := func() {\n\t\tfor i := 0; i < 50; i++ {\n\t\t\terr = mp.Submit(longRunningFunc)\n\t\t\trequire.NoError(t, err)\n\t\t}\n\t\trequire.EqualValues(t, mp.Waiting(), 0)\n\t\t_, err = mp.WaitingByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.WaitingByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Running())\n\t\t_, err = mp.RunningByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.RunningByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 0, mp.Free())\n\t\t_, err = mp.FreeByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.FreeByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Cap())\n\t\trequire.False(t, mp.IsClosed())\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tn, _ := mp.WaitingByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t\tn, _ = mp.RunningByIndex(i)\n\t\t\trequire.EqualValues(t, 5, n)\n\t\t\tn, _ = mp.FreeByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t}\n\t\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\t\trequire.NoError(t, mp.ReleaseTimeout(3*time.Second))\n\t\trequire.ErrorIs(t, mp.ReleaseTimeout(3*time.Second), ants.ErrPoolClosed)\n\t\trequire.ErrorIs(t, mp.Submit(nil), ants.ErrPoolClosed)\n\t\trequire.Zero(t, mp.Running())\n\t\trequire.True(t, mp.IsClosed())\n\t\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n\t}\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp, err = ants.NewMultiPool(10, 5, ants.LeastTasks)\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp.Tune(10)\n}\n\nfunc TestMultiPoolWithFunc(t *testing.T) {\n\t_, err := ants.NewMultiPoolWithFunc(-1, 10, longRunningPoolFunc, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidMultiPoolSize)\n\t_, err = ants.NewMultiPoolWithFunc(10, -1, longRunningPoolFunc, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidLoadBalancingStrategy)\n\t_, err = ants.NewMultiPoolWithFunc(10, 10, longRunningPoolFunc, ants.RoundRobin, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\n\tch := make(chan struct{})\n\tmp, err := ants.NewMultiPoolWithFunc(10, 5, longRunningPoolFunc, ants.RoundRobin)\n\ttestFn := func() {\n\t\tfor i := 0; i < 50; i++ {\n\t\t\terr = mp.Invoke(ch)\n\t\t\trequire.NoError(t, err)\n\t\t}\n\t\trequire.EqualValues(t, mp.Waiting(), 0)\n\t\t_, err = mp.WaitingByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.WaitingByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Running())\n\t\t_, err = mp.RunningByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.RunningByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 0, mp.Free())\n\t\t_, err = mp.FreeByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.FreeByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Cap())\n\t\trequire.False(t, mp.IsClosed())\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tn, _ := mp.WaitingByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t\tn, _ = mp.RunningByIndex(i)\n\t\t\trequire.EqualValues(t, 5, n)\n\t\t\tn, _ = mp.FreeByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t}\n\t\tclose(ch)\n\t\trequire.NoError(t, mp.ReleaseTimeout(3*time.Second))\n\t\trequire.ErrorIs(t, mp.ReleaseTimeout(3*time.Second), ants.ErrPoolClosed)\n\t\trequire.ErrorIs(t, mp.Invoke(nil), ants.ErrPoolClosed)\n\t\trequire.Zero(t, mp.Running())\n\t\trequire.True(t, mp.IsClosed())\n\t\tch = make(chan struct{})\n\t}\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp, err = ants.NewMultiPoolWithFunc(10, 5, longRunningPoolFunc, ants.LeastTasks)\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp.Tune(10)\n}\n\nfunc TestMultiPoolWithFuncGeneric(t *testing.T) {\n\t_, err := ants.NewMultiPoolWithFuncGeneric(-1, 10, longRunningPoolFuncCh, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidMultiPoolSize)\n\t_, err = ants.NewMultiPoolWithFuncGeneric(10, -1, longRunningPoolFuncCh, 8)\n\trequire.ErrorIs(t, err, ants.ErrInvalidLoadBalancingStrategy)\n\t_, err = ants.NewMultiPoolWithFuncGeneric(10, 10, longRunningPoolFuncCh, ants.RoundRobin, ants.WithExpiryDuration(-1))\n\trequire.ErrorIs(t, err, ants.ErrInvalidPoolExpiry)\n\n\tch := make(chan struct{})\n\tmp, err := ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.RoundRobin)\n\ttestFn := func() {\n\t\tfor i := 0; i < 50; i++ {\n\t\t\terr = mp.Invoke(ch)\n\t\t\trequire.NoError(t, err)\n\t\t}\n\t\trequire.EqualValues(t, mp.Waiting(), 0)\n\t\t_, err = mp.WaitingByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.WaitingByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Running())\n\t\t_, err = mp.RunningByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.RunningByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 0, mp.Free())\n\t\t_, err = mp.FreeByIndex(-1)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\t_, err = mp.FreeByIndex(11)\n\t\trequire.ErrorIs(t, err, ants.ErrInvalidPoolIndex)\n\t\trequire.EqualValues(t, 50, mp.Cap())\n\t\trequire.False(t, mp.IsClosed())\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tn, _ := mp.WaitingByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t\tn, _ = mp.RunningByIndex(i)\n\t\t\trequire.EqualValues(t, 5, n)\n\t\t\tn, _ = mp.FreeByIndex(i)\n\t\t\trequire.EqualValues(t, 0, n)\n\t\t}\n\t\tclose(ch)\n\t\trequire.NoError(t, mp.ReleaseTimeout(3*time.Second))\n\t\trequire.ErrorIs(t, mp.ReleaseTimeout(3*time.Second), ants.ErrPoolClosed)\n\t\trequire.ErrorIs(t, mp.Invoke(nil), ants.ErrPoolClosed)\n\t\trequire.Zero(t, mp.Running())\n\t\trequire.True(t, mp.IsClosed())\n\t\tch = make(chan struct{})\n\t}\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp, err = ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.LeastTasks)\n\ttestFn()\n\n\tmp.Reboot()\n\ttestFn()\n\n\tmp.Tune(10)\n}\n\nfunc TestMultiPoolReleaseContext(t *testing.T) {\n\tmp, err := ants.NewMultiPool(10, 5, ants.RoundRobin)\n\trequire.NoError(t, err)\n\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Submit(longRunningFunc)\n\t\trequire.NoError(t, err)\n\t}\n\trequire.EqualValues(t, 50, mp.Running())\n\n\t// Signal workers to stop, then release with a background context.\n\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n\n\t// Calling ReleaseContext on a closed pool should return ErrPoolClosed.\n\trequire.ErrorIs(t, mp.ReleaseContext(context.Background()), ants.ErrPoolClosed)\n\n\t// Test with LeastTasks strategy.\n\tmp, err = ants.NewMultiPool(10, 5, ants.LeastTasks)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Submit(longRunningFunc)\n\t\trequire.NoError(t, err)\n\t}\n\trequire.EqualValues(t, 50, mp.Running())\n\n\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n\n\t// Test that a cancelled context returns an error.\n\tmp, err = ants.NewMultiPool(10, 5, ants.RoundRobin)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Submit(longRunningFunc)\n\t\trequire.NoError(t, err)\n\t}\n\tctx, cancel := context.WithCancel(context.Background())\n\tcancel() // cancel immediately\n\terr = mp.ReleaseContext(ctx)\n\trequire.Error(t, err)\n\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\trequire.Eventually(t, func() bool {\n\t\treturn mp.Running() == 0\n\t}, 3*time.Second, 100*time.Millisecond)\n\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n\n\t// Test reboot after ReleaseContext.\n\tmp, err = ants.NewMultiPool(10, 5, ants.RoundRobin)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Submit(longRunningFunc)\n\t\trequire.NoError(t, err)\n\t}\n\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n\n\tmp.Reboot()\n\trequire.False(t, mp.IsClosed())\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Submit(longRunningFunc)\n\t\trequire.NoError(t, err)\n\t}\n\trequire.EqualValues(t, 50, mp.Running())\n\tatomic.StoreInt32(&stopLongRunningFunc, 1)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\tatomic.StoreInt32(&stopLongRunningFunc, 0)\n}\n\nfunc TestMultiPoolWithFuncReleaseContext(t *testing.T) {\n\tch := make(chan struct{})\n\tmp, err := ants.NewMultiPoolWithFunc(10, 5, longRunningPoolFunc, ants.RoundRobin)\n\trequire.NoError(t, err)\n\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\trequire.EqualValues(t, 50, mp.Running())\n\n\tclose(ch)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\n\t// Calling ReleaseContext on a closed pool should return ErrPoolClosed.\n\trequire.ErrorIs(t, mp.ReleaseContext(context.Background()), ants.ErrPoolClosed)\n\n\t// Test with LeastTasks strategy.\n\tch = make(chan struct{})\n\tmp, err = ants.NewMultiPoolWithFunc(10, 5, longRunningPoolFunc, ants.LeastTasks)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\tclose(ch)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\n\t// Test that a cancelled context returns an error.\n\tch = make(chan struct{})\n\tmp, err = ants.NewMultiPoolWithFunc(10, 5, longRunningPoolFunc, ants.RoundRobin)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\tctx, cancel := context.WithCancel(context.Background())\n\tcancel()\n\terr = mp.ReleaseContext(ctx)\n\trequire.Error(t, err)\n\tclose(ch)\n\trequire.Eventually(t, func() bool {\n\t\treturn mp.Running() == 0\n\t}, 3*time.Second, 100*time.Millisecond)\n}\n\nfunc TestMultiPoolWithFuncGenericReleaseContext(t *testing.T) {\n\tch := make(chan struct{})\n\tmp, err := ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.RoundRobin)\n\trequire.NoError(t, err)\n\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\trequire.EqualValues(t, 50, mp.Running())\n\n\tclose(ch)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\n\t// Calling ReleaseContext on a closed pool should return ErrPoolClosed.\n\trequire.ErrorIs(t, mp.ReleaseContext(context.Background()), ants.ErrPoolClosed)\n\n\t// Test with LeastTasks strategy.\n\tch = make(chan struct{})\n\tmp, err = ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.LeastTasks)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\tclose(ch)\n\terr = mp.ReleaseContext(context.Background())\n\trequire.NoError(t, err)\n\trequire.Zero(t, mp.Running())\n\trequire.True(t, mp.IsClosed())\n\n\t// Test that a cancelled context returns an error.\n\tch = make(chan struct{})\n\tmp, err = ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.RoundRobin)\n\trequire.NoError(t, err)\n\tfor i := 0; i < 50; i++ {\n\t\terr = mp.Invoke(ch)\n\t\trequire.NoError(t, err)\n\t}\n\tctx, cancel := context.WithCancel(context.Background())\n\tcancel()\n\terr = mp.ReleaseContext(ctx)\n\trequire.Error(t, err)\n\tclose(ch)\n\trequire.Eventually(t, func() bool {\n\t\treturn mp.Running() == 0\n\t}, 3*time.Second, 100*time.Millisecond)\n}\n\nfunc TestRebootNewPoolCalc(t *testing.T) {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tpool, err := ants.NewPool(10)\n\trequire.NoError(t, err)\n\tdefer pool.Release()\n\t// Use the default pool.\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = pool.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\trequire.EqualValues(t, 499500, sum, \"The result should be 499500\")\n\n\tatomic.StoreInt32(&sum, 0)\n\twg.Add(runTimes)\n\terr = pool.ReleaseTimeout(time.Second) // use both Release and ReleaseTimeout will occur panic\n\trequire.NoError(t, err)\n\tpool.Reboot()\n\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = pool.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\trequire.EqualValues(t, 499500, sum, \"The result should be 499500\")\n}\n\nfunc TestRebootNewPoolWithPreAllocCalc(t *testing.T) {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tpool, err := ants.NewPool(10, ants.WithPreAlloc(true))\n\trequire.NoError(t, err)\n\tdefer pool.Release()\n\t// Use the default pool.\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = pool.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\trequire.EqualValues(t, 499500, sum, \"The result should be 499500\")\n\n\tatomic.StoreInt32(&sum, 0)\n\terr = pool.ReleaseTimeout(time.Second)\n\trequire.NoError(t, err)\n\tpool.Reboot()\n\n\twg.Add(runTimes)\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = pool.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\trequire.EqualValues(t, 499500, sum, \"The result should be 499500\")\n}\n"
  },
  {
    "path": "example_test.go",
    "content": "/*\n * Copyright (c) 2025. Andy Pan. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants_test\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"time\"\n\n\t\"github.com/panjf2000/ants/v2\"\n)\n\nvar (\n\tsum int32\n\twg  sync.WaitGroup\n)\n\nfunc incSum(i any) {\n\tincSumInt(i.(int32))\n}\n\nfunc incSumInt(i int32) {\n\tatomic.AddInt32(&sum, i)\n\twg.Done()\n}\n\nfunc ExamplePool() {\n\tants.Reboot() // ensure the default pool is available\n\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\t// Use the default pool.\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = ants.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\tatomic.StoreInt32(&sum, 0)\n\twg.Add(runTimes)\n\t// Use the new pool.\n\tpool, _ := ants.NewPool(10)\n\tdefer pool.Release()\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = pool.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output:\n\t// The result is 499500\n\t// The result is 499500\n}\n\nfunc ExamplePoolWithFunc() {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tpool, _ := ants.NewPoolWithFunc(10, incSum)\n\tdefer pool.Release()\n\n\tfor i := 0; i < runTimes; i++ {\n\t\t_ = pool.Invoke(int32(i))\n\t}\n\twg.Wait()\n\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output: The result is 499500\n}\n\nfunc ExamplePoolWithFuncGeneric() {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tpool, _ := ants.NewPoolWithFuncGeneric(10, incSumInt)\n\tdefer pool.Release()\n\n\tfor i := 0; i < runTimes; i++ {\n\t\t_ = pool.Invoke(int32(i))\n\t}\n\twg.Wait()\n\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output: The result is 499500\n}\n\nfunc ExampleMultiPool() {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tmp, _ := ants.NewMultiPool(10, runTimes/10, ants.RoundRobin)\n\tdefer mp.ReleaseTimeout(time.Second) // nolint:errcheck\n\n\tfor i := 0; i < runTimes; i++ {\n\t\tj := i\n\t\t_ = mp.Submit(func() {\n\t\t\tincSumInt(int32(j))\n\t\t})\n\t}\n\twg.Wait()\n\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output: The result is 499500\n}\n\nfunc ExampleMultiPoolWithFunc() {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tmp, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, incSum, ants.RoundRobin)\n\tdefer mp.ReleaseTimeout(time.Second) // nolint:errcheck\n\n\tfor i := 0; i < runTimes; i++ {\n\t\t_ = mp.Invoke(int32(i))\n\t}\n\twg.Wait()\n\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output: The result is 499500\n}\n\nfunc ExampleMultiPoolWithFuncGeneric() {\n\tatomic.StoreInt32(&sum, 0)\n\trunTimes := 1000\n\twg.Add(runTimes)\n\n\tmp, _ := ants.NewMultiPoolWithFuncGeneric(10, runTimes/10, incSumInt, ants.RoundRobin)\n\tdefer mp.ReleaseTimeout(time.Second) // nolint:errcheck\n\n\tfor i := 0; i < runTimes; i++ {\n\t\t_ = mp.Invoke(int32(i))\n\t}\n\twg.Wait()\n\n\tfmt.Printf(\"The result is %d\\n\", sum)\n\n\t// Output: The result is 499500\n}\n"
  },
  {
    "path": "go.mod",
    "content": "module github.com/panjf2000/ants/v2\n\ngo 1.19\n\nrequire (\n\tgithub.com/stretchr/testify v1.10.0\n\tgolang.org/x/sync v0.11.0\n)\n\nrequire (\n\tgithub.com/davecgh/go-spew v1.1.1 // indirect\n\tgithub.com/pmezard/go-difflib v1.0.0 // indirect\n\tgopkg.in/yaml.v3 v3.0.1 // indirect\n)\n"
  },
  {
    "path": "go.sum",
    "content": "github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=\ngithub.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=\ngithub.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=\ngithub.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=\ngithub.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=\ngithub.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=\ngolang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=\ngolang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=\ngopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=\ngopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=\ngopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=\ngopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=\n"
  },
  {
    "path": "multipool.go",
    "content": "// MIT License\n\n// Copyright (c) 2023 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"math\"\n\t\"strings\"\n\t\"sync/atomic\"\n\t\"time\"\n\n\t\"golang.org/x/sync/errgroup\"\n)\n\n// LoadBalancingStrategy represents the type of load-balancing algorithm.\ntype LoadBalancingStrategy int\n\nconst (\n\t// RoundRobin distributes task to a list of pools in rotation.\n\tRoundRobin LoadBalancingStrategy = 1 << (iota + 1)\n\n\t// LeastTasks always selects the pool with the least number of pending tasks.\n\tLeastTasks\n)\n\ntype contextReleaser interface {\n\tReleaseContext(ctx context.Context) error\n}\n\nfunc releasePools(ctx context.Context, pools []contextReleaser) error {\n\terrCh := make(chan error, len(pools))\n\tvar wg errgroup.Group\n\tfor i, pool := range pools {\n\t\tfunc(p contextReleaser, idx int) {\n\t\t\twg.Go(func() error {\n\t\t\t\terr := p.ReleaseContext(ctx)\n\t\t\t\tif err != nil {\n\t\t\t\t\terr = fmt.Errorf(\"pool %d: %v\", idx, err)\n\t\t\t\t}\n\t\t\t\terrCh <- err\n\t\t\t\treturn err\n\t\t\t})\n\t\t}(pool, i)\n\t}\n\n\t_ = wg.Wait()\n\n\tvar errStr strings.Builder\n\tfor i := 0; i < len(pools); i++ {\n\t\tif err := <-errCh; err != nil {\n\t\t\terrStr.WriteString(err.Error())\n\t\t\terrStr.WriteString(\" | \")\n\t\t}\n\t}\n\n\tif errStr.Len() == 0 {\n\t\treturn nil\n\t}\n\n\treturn errors.New(strings.TrimSuffix(errStr.String(), \" | \"))\n}\n\n// MultiPool consists of multiple pools, from which you will benefit the\n// performance improvement on basis of the fine-grained locking that reduces\n// the lock contention.\n// MultiPool is a good fit for the scenario where you have a large number of\n// tasks to submit, and you don't want the single pool to be the bottleneck.\ntype MultiPool struct {\n\tpools []*Pool\n\tindex uint32\n\tstate int32\n\tlbs   LoadBalancingStrategy\n}\n\n// NewMultiPool instantiates a MultiPool with a size of the pool list and a size\n// per pool, and the load-balancing strategy.\nfunc NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...Option) (*MultiPool, error) {\n\tif size <= 0 {\n\t\treturn nil, ErrInvalidMultiPoolSize\n\t}\n\n\tif lbs != RoundRobin && lbs != LeastTasks {\n\t\treturn nil, ErrInvalidLoadBalancingStrategy\n\t}\n\tpools := make([]*Pool, size)\n\tfor i := 0; i < size; i++ {\n\t\tpool, err := NewPool(sizePerPool, options...)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tpools[i] = pool\n\t}\n\treturn &MultiPool{pools: pools, index: math.MaxUint32, lbs: lbs}, nil\n}\n\nfunc (mp *MultiPool) next(lbs LoadBalancingStrategy) (idx int) {\n\tswitch lbs {\n\tcase RoundRobin:\n\t\treturn int(atomic.AddUint32(&mp.index, 1) % uint32(len(mp.pools)))\n\tcase LeastTasks:\n\t\tleastTasks := 1<<31 - 1\n\t\tfor i, pool := range mp.pools {\n\t\t\tif n := pool.Running(); n < leastTasks {\n\t\t\t\tleastTasks = n\n\t\t\t\tidx = i\n\t\t\t}\n\t\t}\n\t\treturn\n\t}\n\treturn -1\n}\n\n// Submit submits a task to a pool selected by the load-balancing strategy.\nfunc (mp *MultiPool) Submit(task func()) (err error) {\n\tif mp.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\tif err = mp.pools[mp.next(mp.lbs)].Submit(task); err == nil {\n\t\treturn\n\t}\n\tif err == ErrPoolOverload && mp.lbs == RoundRobin {\n\t\treturn mp.pools[mp.next(LeastTasks)].Submit(task)\n\t}\n\treturn\n}\n\n// Running returns the number of the currently running workers across all pools.\nfunc (mp *MultiPool) Running() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Running()\n\t}\n\treturn\n}\n\n// RunningByIndex returns the number of the currently running workers in the specific pool.\nfunc (mp *MultiPool) RunningByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Running(), nil\n}\n\n// Free returns the number of available workers across all pools.\nfunc (mp *MultiPool) Free() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Free()\n\t}\n\treturn\n}\n\n// FreeByIndex returns the number of available workers in the specific pool.\nfunc (mp *MultiPool) FreeByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Free(), nil\n}\n\n// Waiting returns the number of the currently waiting tasks across all pools.\nfunc (mp *MultiPool) Waiting() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Waiting()\n\t}\n\treturn\n}\n\n// WaitingByIndex returns the number of the currently waiting tasks in the specific pool.\nfunc (mp *MultiPool) WaitingByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Waiting(), nil\n}\n\n// Cap returns the capacity of this multi-pool.\nfunc (mp *MultiPool) Cap() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Cap()\n\t}\n\treturn\n}\n\n// Tune resizes each pool in multi-pool.\n//\n// Note that this method doesn't resize the overall\n// capacity of multi-pool.\nfunc (mp *MultiPool) Tune(size int) {\n\tfor _, pool := range mp.pools {\n\t\tpool.Tune(size)\n\t}\n}\n\n// IsClosed indicates whether the multi-pool is closed.\nfunc (mp *MultiPool) IsClosed() bool {\n\treturn atomic.LoadInt32(&mp.state) == CLOSED\n}\n\n// ReleaseTimeout closes the multi-pool with a timeout,\n// it waits all pools to be closed before timing out.\nfunc (mp *MultiPool) ReleaseTimeout(timeout time.Duration) error {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\treturn mp.ReleaseContext(ctx)\n}\n\n// ReleaseContext closes the multi-pool with a context,\n// it waits all pools to be closed before the context is done.\nfunc (mp *MultiPool) ReleaseContext(ctx context.Context) error {\n\tif !atomic.CompareAndSwapInt32(&mp.state, OPENED, CLOSED) {\n\t\treturn ErrPoolClosed\n\t}\n\n\tpools := make([]contextReleaser, len(mp.pools))\n\tfor i, p := range mp.pools {\n\t\tpools[i] = p\n\t}\n\treturn releasePools(ctx, pools)\n}\n\n// Reboot reboots a released multi-pool.\nfunc (mp *MultiPool) Reboot() {\n\tif atomic.CompareAndSwapInt32(&mp.state, CLOSED, OPENED) {\n\t\tatomic.StoreUint32(&mp.index, 0)\n\t\tfor _, pool := range mp.pools {\n\t\t\tpool.Reboot()\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "multipool_func.go",
    "content": "// MIT License\n\n// Copyright (c) 2023 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"context\"\n\t\"math\"\n\t\"sync/atomic\"\n\t\"time\"\n)\n\n// MultiPoolWithFunc consists of multiple pools, from which you will benefit the\n// performance improvement on basis of the fine-grained locking that reduces\n// the lock contention.\n// MultiPoolWithFunc is a good fit for the scenario where you have a large number of\n// tasks to submit, and you don't want the single pool to be the bottleneck.\ntype MultiPoolWithFunc struct {\n\tpools []*PoolWithFunc\n\tindex uint32\n\tstate int32\n\tlbs   LoadBalancingStrategy\n}\n\n// NewMultiPoolWithFunc instantiates a MultiPoolWithFunc with a size of the pool list and a size\n// per pool, and the load-balancing strategy.\nfunc NewMultiPoolWithFunc(size, sizePerPool int, fn func(any), lbs LoadBalancingStrategy, options ...Option) (*MultiPoolWithFunc, error) {\n\tif size <= 0 {\n\t\treturn nil, ErrInvalidMultiPoolSize\n\t}\n\n\tif lbs != RoundRobin && lbs != LeastTasks {\n\t\treturn nil, ErrInvalidLoadBalancingStrategy\n\t}\n\tpools := make([]*PoolWithFunc, size)\n\tfor i := 0; i < size; i++ {\n\t\tpool, err := NewPoolWithFunc(sizePerPool, fn, options...)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tpools[i] = pool\n\t}\n\treturn &MultiPoolWithFunc{pools: pools, index: math.MaxUint32, lbs: lbs}, nil\n}\n\nfunc (mp *MultiPoolWithFunc) next(lbs LoadBalancingStrategy) (idx int) {\n\tswitch lbs {\n\tcase RoundRobin:\n\t\treturn int(atomic.AddUint32(&mp.index, 1) % uint32(len(mp.pools)))\n\tcase LeastTasks:\n\t\tleastTasks := 1<<31 - 1\n\t\tfor i, pool := range mp.pools {\n\t\t\tif n := pool.Running(); n < leastTasks {\n\t\t\t\tleastTasks = n\n\t\t\t\tidx = i\n\t\t\t}\n\t\t}\n\t\treturn\n\t}\n\treturn -1\n}\n\n// Invoke submits a task to a pool selected by the load-balancing strategy.\nfunc (mp *MultiPoolWithFunc) Invoke(args any) (err error) {\n\tif mp.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\n\tif err = mp.pools[mp.next(mp.lbs)].Invoke(args); err == nil {\n\t\treturn\n\t}\n\tif err == ErrPoolOverload && mp.lbs == RoundRobin {\n\t\treturn mp.pools[mp.next(LeastTasks)].Invoke(args)\n\t}\n\treturn\n}\n\n// Running returns the number of the currently running workers across all pools.\nfunc (mp *MultiPoolWithFunc) Running() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Running()\n\t}\n\treturn\n}\n\n// RunningByIndex returns the number of the currently running workers in the specific pool.\nfunc (mp *MultiPoolWithFunc) RunningByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Running(), nil\n}\n\n// Free returns the number of available workers across all pools.\nfunc (mp *MultiPoolWithFunc) Free() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Free()\n\t}\n\treturn\n}\n\n// FreeByIndex returns the number of available workers in the specific pool.\nfunc (mp *MultiPoolWithFunc) FreeByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Free(), nil\n}\n\n// Waiting returns the number of the currently waiting tasks across all pools.\nfunc (mp *MultiPoolWithFunc) Waiting() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Waiting()\n\t}\n\treturn\n}\n\n// WaitingByIndex returns the number of the currently waiting tasks in the specific pool.\nfunc (mp *MultiPoolWithFunc) WaitingByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Waiting(), nil\n}\n\n// Cap returns the capacity of this multi-pool.\nfunc (mp *MultiPoolWithFunc) Cap() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Cap()\n\t}\n\treturn\n}\n\n// Tune resizes each pool in multi-pool.\n//\n// Note that this method doesn't resize the overall\n// capacity of multi-pool.\nfunc (mp *MultiPoolWithFunc) Tune(size int) {\n\tfor _, pool := range mp.pools {\n\t\tpool.Tune(size)\n\t}\n}\n\n// IsClosed indicates whether the multi-pool is closed.\nfunc (mp *MultiPoolWithFunc) IsClosed() bool {\n\treturn atomic.LoadInt32(&mp.state) == CLOSED\n}\n\n// ReleaseTimeout closes the multi-pool with a timeout,\n// it waits all pools to be closed before timing out.\nfunc (mp *MultiPoolWithFunc) ReleaseTimeout(timeout time.Duration) error {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\treturn mp.ReleaseContext(ctx)\n}\n\n// ReleaseContext closes the multi-pool with a context,\n// it waits all pools to be closed before the context is done.\nfunc (mp *MultiPoolWithFunc) ReleaseContext(ctx context.Context) error {\n\tif !atomic.CompareAndSwapInt32(&mp.state, OPENED, CLOSED) {\n\t\treturn ErrPoolClosed\n\t}\n\n\tpools := make([]contextReleaser, len(mp.pools))\n\tfor i, p := range mp.pools {\n\t\tpools[i] = p\n\t}\n\treturn releasePools(ctx, pools)\n}\n\n// Reboot reboots a released multi-pool.\nfunc (mp *MultiPoolWithFunc) Reboot() {\n\tif atomic.CompareAndSwapInt32(&mp.state, CLOSED, OPENED) {\n\t\tatomic.StoreUint32(&mp.index, 0)\n\t\tfor _, pool := range mp.pools {\n\t\t\tpool.Reboot()\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "multipool_func_generic.go",
    "content": "// MIT License\n\n// Copyright (c) 2025 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"context\"\n\t\"math\"\n\t\"sync/atomic\"\n\t\"time\"\n)\n\n// MultiPoolWithFuncGeneric is the generic version of MultiPoolWithFunc.\ntype MultiPoolWithFuncGeneric[T any] struct {\n\tpools []*PoolWithFuncGeneric[T]\n\tindex uint32\n\tstate int32\n\tlbs   LoadBalancingStrategy\n}\n\n// NewMultiPoolWithFuncGeneric instantiates a MultiPoolWithFunc with a size of the pool list and a size\n// per pool, and the load-balancing strategy.\nfunc NewMultiPoolWithFuncGeneric[T any](size, sizePerPool int, fn func(T), lbs LoadBalancingStrategy, options ...Option) (*MultiPoolWithFuncGeneric[T], error) {\n\tif size <= 0 {\n\t\treturn nil, ErrInvalidMultiPoolSize\n\t}\n\n\tif lbs != RoundRobin && lbs != LeastTasks {\n\t\treturn nil, ErrInvalidLoadBalancingStrategy\n\t}\n\tpools := make([]*PoolWithFuncGeneric[T], size)\n\tfor i := 0; i < size; i++ {\n\t\tpool, err := NewPoolWithFuncGeneric(sizePerPool, fn, options...)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tpools[i] = pool\n\t}\n\treturn &MultiPoolWithFuncGeneric[T]{pools: pools, index: math.MaxUint32, lbs: lbs}, nil\n}\n\nfunc (mp *MultiPoolWithFuncGeneric[T]) next(lbs LoadBalancingStrategy) (idx int) {\n\tswitch lbs {\n\tcase RoundRobin:\n\t\treturn int(atomic.AddUint32(&mp.index, 1) % uint32(len(mp.pools)))\n\tcase LeastTasks:\n\t\tleastTasks := 1<<31 - 1\n\t\tfor i, pool := range mp.pools {\n\t\t\tif n := pool.Running(); n < leastTasks {\n\t\t\t\tleastTasks = n\n\t\t\t\tidx = i\n\t\t\t}\n\t\t}\n\t\treturn\n\t}\n\treturn -1\n}\n\n// Invoke submits a task to a pool selected by the load-balancing strategy.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Invoke(args T) (err error) {\n\tif mp.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\n\tif err = mp.pools[mp.next(mp.lbs)].Invoke(args); err == nil {\n\t\treturn\n\t}\n\tif err == ErrPoolOverload && mp.lbs == RoundRobin {\n\t\treturn mp.pools[mp.next(LeastTasks)].Invoke(args)\n\t}\n\treturn\n}\n\n// Running returns the number of the currently running workers across all pools.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Running() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Running()\n\t}\n\treturn\n}\n\n// RunningByIndex returns the number of the currently running workers in the specific pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) RunningByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Running(), nil\n}\n\n// Free returns the number of available workers across all pools.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Free() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Free()\n\t}\n\treturn\n}\n\n// FreeByIndex returns the number of available workers in the specific pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) FreeByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Free(), nil\n}\n\n// Waiting returns the number of the currently waiting tasks across all pools.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Waiting() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Waiting()\n\t}\n\treturn\n}\n\n// WaitingByIndex returns the number of the currently waiting tasks in the specific pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) WaitingByIndex(idx int) (int, error) {\n\tif idx < 0 || idx >= len(mp.pools) {\n\t\treturn -1, ErrInvalidPoolIndex\n\t}\n\treturn mp.pools[idx].Waiting(), nil\n}\n\n// Cap returns the capacity of this multi-pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Cap() (n int) {\n\tfor _, pool := range mp.pools {\n\t\tn += pool.Cap()\n\t}\n\treturn\n}\n\n// Tune resizes each pool in multi-pool.\n//\n// Note that this method doesn't resize the overall\n// capacity of multi-pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Tune(size int) {\n\tfor _, pool := range mp.pools {\n\t\tpool.Tune(size)\n\t}\n}\n\n// IsClosed indicates whether the multi-pool is closed.\nfunc (mp *MultiPoolWithFuncGeneric[T]) IsClosed() bool {\n\treturn atomic.LoadInt32(&mp.state) == CLOSED\n}\n\n// ReleaseTimeout closes the multi-pool with a timeout,\n// it waits all pools to be closed before timing out.\nfunc (mp *MultiPoolWithFuncGeneric[T]) ReleaseTimeout(timeout time.Duration) error {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\treturn mp.ReleaseContext(ctx)\n}\n\n// ReleaseContext closes the multi-pool with a context,\n// it waits all pools to be closed before the context is done.\nfunc (mp *MultiPoolWithFuncGeneric[T]) ReleaseContext(ctx context.Context) error {\n\tif !atomic.CompareAndSwapInt32(&mp.state, OPENED, CLOSED) {\n\t\treturn ErrPoolClosed\n\t}\n\n\tpools := make([]contextReleaser, len(mp.pools))\n\tfor i, p := range mp.pools {\n\t\tpools[i] = p\n\t}\n\treturn releasePools(ctx, pools)\n}\n\n// Reboot reboots a released multi-pool.\nfunc (mp *MultiPoolWithFuncGeneric[T]) Reboot() {\n\tif atomic.CompareAndSwapInt32(&mp.state, CLOSED, OPENED) {\n\t\tatomic.StoreUint32(&mp.index, 0)\n\t\tfor _, pool := range mp.pools {\n\t\t\tpool.Reboot()\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "options.go",
    "content": "/*\n * Copyright (c) 2018. Andy Pan. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport \"time\"\n\n// Option represents the optional function.\ntype Option func(opts *Options)\n\nfunc loadOptions(options ...Option) *Options {\n\topts := new(Options)\n\tfor _, option := range options {\n\t\toption(opts)\n\t}\n\treturn opts\n}\n\n// Options contains all options which will be applied when instantiating an ants pool.\ntype Options struct {\n\t// ExpiryDuration is a period for the scavenger goroutine to clean up those expired workers,\n\t// the scavenger scans all workers every `ExpiryDuration` and clean up those workers that haven't been\n\t// used for more than `ExpiryDuration`.\n\tExpiryDuration time.Duration\n\n\t// PreAlloc indicates whether to make memory pre-allocation when initializing Pool.\n\tPreAlloc bool\n\n\t// Max number of goroutine blocking on pool.Submit.\n\t// 0 (default value) means no such limit.\n\tMaxBlockingTasks int\n\n\t// When Nonblocking is true, Pool.Submit will never be blocked.\n\t// ErrPoolOverload will be returned when Pool.Submit cannot be done at once.\n\t// When Nonblocking is true, MaxBlockingTasks is inoperative.\n\tNonblocking bool\n\n\t// PanicHandler is used to handle panics from each worker goroutine.\n\t// If nil, the default behavior is to capture the value given to panic\n\t// and resume normal execution and print that value along with the\n\t// stack trace of the goroutine\n\tPanicHandler func(any)\n\n\t// Logger is the customized logger for logging info, if it is not set,\n\t// default standard logger from log package is used.\n\tLogger Logger\n\n\t// When DisablePurge is true, workers are not purged and are resident.\n\tDisablePurge bool\n}\n\n// WithOptions accepts the whole Options config.\nfunc WithOptions(options Options) Option {\n\treturn func(opts *Options) {\n\t\t*opts = options\n\t}\n}\n\n// WithExpiryDuration sets up the interval time of cleaning up goroutines.\nfunc WithExpiryDuration(expiryDuration time.Duration) Option {\n\treturn func(opts *Options) {\n\t\topts.ExpiryDuration = expiryDuration\n\t}\n}\n\n// WithPreAlloc indicates whether it should malloc for workers.\nfunc WithPreAlloc(preAlloc bool) Option {\n\treturn func(opts *Options) {\n\t\topts.PreAlloc = preAlloc\n\t}\n}\n\n// WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.\nfunc WithMaxBlockingTasks(maxBlockingTasks int) Option {\n\treturn func(opts *Options) {\n\t\topts.MaxBlockingTasks = maxBlockingTasks\n\t}\n}\n\n// WithNonblocking indicates that pool will return nil when there is no available workers.\nfunc WithNonblocking(nonblocking bool) Option {\n\treturn func(opts *Options) {\n\t\topts.Nonblocking = nonblocking\n\t}\n}\n\n// WithPanicHandler sets up panic handler.\nfunc WithPanicHandler(panicHandler func(any)) Option {\n\treturn func(opts *Options) {\n\t\topts.PanicHandler = panicHandler\n\t}\n}\n\n// WithLogger sets up a customized logger.\nfunc WithLogger(logger Logger) Option {\n\treturn func(opts *Options) {\n\t\topts.Logger = logger\n\t}\n}\n\n// WithDisablePurge indicates whether we turn off automatically purge.\nfunc WithDisablePurge(disable bool) Option {\n\treturn func(opts *Options) {\n\t\topts.DisablePurge = disable\n\t}\n}\n"
  },
  {
    "path": "pkg/sync/spinlock.go",
    "content": "// Copyright 2019 Andy Pan & Dietoad. All rights reserved.\n// Use of this source code is governed by an MIT-style\n// license that can be found in the LICENSE file.\n\npackage sync\n\nimport (\n\t\"runtime\"\n\t\"sync\"\n\t\"sync/atomic\"\n)\n\ntype spinLock uint32\n\nconst maxBackoff = 16\n\nfunc (sl *spinLock) Lock() {\n\tbackoff := 1\n\tfor !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {\n\t\t// Leverage the exponential backoff algorithm, see https://en.wikipedia.org/wiki/Exponential_backoff.\n\t\tfor i := 0; i < backoff; i++ {\n\t\t\truntime.Gosched()\n\t\t}\n\t\tif backoff < maxBackoff {\n\t\t\tbackoff <<= 1\n\t\t}\n\t}\n}\n\nfunc (sl *spinLock) Unlock() {\n\tatomic.StoreUint32((*uint32)(sl), 0)\n}\n\n// NewSpinLock instantiates a spin-lock.\nfunc NewSpinLock() sync.Locker {\n\treturn new(spinLock)\n}\n"
  },
  {
    "path": "pkg/sync/spinlock_test.go",
    "content": "// Copyright 2021 Andy Pan & Dietoad. All rights reserved.\n// Use of this source code is governed by an MIT-style\n// license that can be found in the LICENSE file.\n\npackage sync\n\nimport (\n\t\"runtime\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"testing\"\n)\n\n/*\nBenchmark result for three types of locks:\n\tgoos: darwin\n\tgoarch: arm64\n\tpkg: github.com/panjf2000/ants/v2/pkg/sync\n\tBenchmarkMutex-10              \t10452573\t       111.1 ns/op\t       0 B/op\t       0 allocs/op\n\tBenchmarkSpinLock-10           \t58953211\t        18.01 ns/op\t       0 B/op\t       0 allocs/op\n\tBenchmarkBackOffSpinLock-10    \t100000000\t        10.81 ns/op\t       0 B/op\t       0 allocs/op\n*/\n\ntype originSpinLock uint32\n\nfunc (sl *originSpinLock) Lock() {\n\tfor !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {\n\t\truntime.Gosched()\n\t}\n}\n\nfunc (sl *originSpinLock) Unlock() {\n\tatomic.StoreUint32((*uint32)(sl), 0)\n}\n\nfunc NewOriginSpinLock() sync.Locker {\n\treturn new(originSpinLock)\n}\n\nfunc BenchmarkMutex(b *testing.B) {\n\tm := sync.Mutex{}\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tfor pb.Next() {\n\t\t\tm.Lock()\n\t\t\t//nolint:staticcheck\n\t\t\tm.Unlock()\n\t\t}\n\t})\n}\n\nfunc BenchmarkSpinLock(b *testing.B) {\n\tspin := NewOriginSpinLock()\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tfor pb.Next() {\n\t\t\tspin.Lock()\n\t\t\t//nolint:staticcheck\n\t\t\tspin.Unlock()\n\t\t}\n\t})\n}\n\nfunc BenchmarkBackOffSpinLock(b *testing.B) {\n\tspin := NewSpinLock()\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tfor pb.Next() {\n\t\t\tspin.Lock()\n\t\t\t//nolint:staticcheck\n\t\t\tspin.Unlock()\n\t\t}\n\t})\n}\n"
  },
  {
    "path": "pkg/sync/sync.go",
    "content": "/*\n * Copyright (c) 2025. Andy Pan. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n// Package sync provides some handy implementations for synchronization access.\n// At the moment, there is only an implementation of spin-lock.\npackage sync\n"
  },
  {
    "path": "pool.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\n// Pool is a goroutine pool that limits and recycles a mass of goroutines.\n// The pool capacity can be fixed or unlimited.\ntype Pool struct {\n\t*poolCommon\n}\n\n// Submit submits a task to the pool.\n//\n// Note that you are allowed to call Pool.Submit() from the current Pool.Submit(),\n// but what calls for special attention is that you will get blocked with the last\n// Pool.Submit() call once the current Pool runs out of its capacity, and to avoid this,\n// you should instantiate a Pool with ants.WithNonblocking(true).\nfunc (p *Pool) Submit(task func()) error {\n\tif p.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\n\tw, err := p.retrieveWorker()\n\tif w != nil {\n\t\tw.inputFunc(task)\n\t}\n\treturn err\n}\n\n// NewPool instantiates a Pool with customized options.\nfunc NewPool(size int, options ...Option) (*Pool, error) {\n\tpc, err := newPool(size, options...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tpool := &Pool{poolCommon: pc}\n\tpool.workerCache.New = func() any {\n\t\treturn &goWorker{\n\t\t\tpool: pool,\n\t\t\ttask: make(chan func(), workerChanCap),\n\t\t}\n\t}\n\n\treturn pool, nil\n}\n"
  },
  {
    "path": "pool_func.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\n// PoolWithFunc is like Pool but accepts a unified function for all goroutines to execute.\ntype PoolWithFunc struct {\n\t*poolCommon\n\n\t// fn is the unified function for processing tasks.\n\tfn func(any)\n}\n\n// Invoke passes arguments to the pool.\n//\n// Note that you are allowed to call Pool.Invoke() from the current Pool.Invoke(),\n// but what calls for special attention is that you will get blocked with the last\n// Pool.Invoke() call once the current Pool runs out of its capacity, and to avoid this,\n// you should instantiate a PoolWithFunc with ants.WithNonblocking(true).\nfunc (p *PoolWithFunc) Invoke(arg any) error {\n\tif p.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\n\tw, err := p.retrieveWorker()\n\tif w != nil {\n\t\tw.inputArg(arg)\n\t}\n\treturn err\n}\n\n// NewPoolWithFunc instantiates a PoolWithFunc with customized options.\nfunc NewPoolWithFunc(size int, pf func(any), options ...Option) (*PoolWithFunc, error) {\n\tif pf == nil {\n\t\treturn nil, ErrLackPoolFunc\n\t}\n\n\tpc, err := newPool(size, options...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tpool := &PoolWithFunc{\n\t\tpoolCommon: pc,\n\t\tfn:         pf,\n\t}\n\n\tpool.workerCache.New = func() any {\n\t\treturn &goWorkerWithFunc{\n\t\t\tpool: pool,\n\t\t\targ:  make(chan any, workerChanCap),\n\t\t}\n\t}\n\n\treturn pool, nil\n}\n"
  },
  {
    "path": "pool_func_generic.go",
    "content": "// MIT License\n\n// Copyright (c) 2025 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\n// PoolWithFuncGeneric is the generic version of PoolWithFunc.\ntype PoolWithFuncGeneric[T any] struct {\n\t*poolCommon\n\n\t// fn is the unified function for processing tasks.\n\tfn func(T)\n}\n\n// Invoke passes the argument to the pool to start a new task.\nfunc (p *PoolWithFuncGeneric[T]) Invoke(arg T) error {\n\tif p.IsClosed() {\n\t\treturn ErrPoolClosed\n\t}\n\n\tw, err := p.retrieveWorker()\n\tif w != nil {\n\t\tw.(*goWorkerWithFuncGeneric[T]).arg <- arg\n\t}\n\treturn err\n}\n\n// NewPoolWithFuncGeneric instantiates a PoolWithFuncGeneric[T] with customized options.\nfunc NewPoolWithFuncGeneric[T any](size int, pf func(T), options ...Option) (*PoolWithFuncGeneric[T], error) {\n\tif pf == nil {\n\t\treturn nil, ErrLackPoolFunc\n\t}\n\n\tpc, err := newPool(size, options...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tpool := &PoolWithFuncGeneric[T]{\n\t\tpoolCommon: pc,\n\t\tfn:         pf,\n\t}\n\n\tpool.workerCache.New = func() any {\n\t\treturn &goWorkerWithFuncGeneric[T]{\n\t\t\tpool: pool,\n\t\t\targ:  make(chan T, workerChanCap),\n\t\t\texit: make(chan struct{}, 1),\n\t\t}\n\t}\n\n\treturn pool, nil\n}\n"
  },
  {
    "path": "worker.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"runtime/debug\"\n)\n\n// goWorker is the actual executor who runs the tasks,\n// it starts a goroutine that accepts tasks and\n// performs function calls.\ntype goWorker struct {\n\tworker\n\n\t// pool who owns this worker.\n\tpool *Pool\n\n\t// task is a job should be done.\n\ttask chan func()\n\n\t// lastUsed will be updated when putting a worker back into queue.\n\tlastUsed int64\n}\n\n// run starts a goroutine to repeat the process\n// that performs the function calls.\nfunc (w *goWorker) run() {\n\tw.pool.addRunning(1)\n\tgo func() {\n\t\tdefer func() {\n\t\t\tif w.pool.addRunning(-1) == 0 && w.pool.IsClosed() {\n\t\t\t\tw.pool.once.Do(func() {\n\t\t\t\t\tclose(w.pool.allDone)\n\t\t\t\t})\n\t\t\t}\n\t\t\tw.pool.workerCache.Put(w)\n\t\t\tif p := recover(); p != nil {\n\t\t\t\tif ph := w.pool.options.PanicHandler; ph != nil {\n\t\t\t\t\tph(p)\n\t\t\t\t} else {\n\t\t\t\t\tw.pool.options.Logger.Printf(\"worker exits from panic: %v\\n%s\\n\", p, debug.Stack())\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Call Signal() here in case there are goroutines waiting for available workers.\n\t\t\tw.pool.cond.Signal()\n\t\t}()\n\n\t\tfor fn := range w.task {\n\t\t\tif fn == nil {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tfn()\n\t\t\tif ok := w.pool.revertWorker(w); !ok {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}()\n}\n\nfunc (w *goWorker) finish() {\n\tw.task <- nil\n}\n\nfunc (w *goWorker) lastUsedTime() int64 {\n\treturn w.lastUsed\n}\n\nfunc (w *goWorker) setLastUsedTime(t int64) {\n\tw.lastUsed = t\n}\n\nfunc (w *goWorker) inputFunc(fn func()) {\n\tw.task <- fn\n}\n"
  },
  {
    "path": "worker_func.go",
    "content": "// MIT License\n\n// Copyright (c) 2018 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"runtime/debug\"\n)\n\n// goWorkerWithFunc is the actual executor who runs the tasks,\n// it starts a goroutine that accepts tasks and\n// performs function calls.\ntype goWorkerWithFunc struct {\n\tworker\n\n\t// pool who owns this worker.\n\tpool *PoolWithFunc\n\n\t// arg is the argument for the function.\n\targ chan any\n\n\t// lastUsed will be updated when putting a worker back into queue.\n\tlastUsed int64\n}\n\n// run starts a goroutine to repeat the process\n// that performs the function calls.\nfunc (w *goWorkerWithFunc) run() {\n\tw.pool.addRunning(1)\n\tgo func() {\n\t\tdefer func() {\n\t\t\tif w.pool.addRunning(-1) == 0 && w.pool.IsClosed() {\n\t\t\t\tw.pool.once.Do(func() {\n\t\t\t\t\tclose(w.pool.allDone)\n\t\t\t\t})\n\t\t\t}\n\t\t\tw.pool.workerCache.Put(w)\n\t\t\tif p := recover(); p != nil {\n\t\t\t\tif ph := w.pool.options.PanicHandler; ph != nil {\n\t\t\t\t\tph(p)\n\t\t\t\t} else {\n\t\t\t\t\tw.pool.options.Logger.Printf(\"worker exits from panic: %v\\n%s\\n\", p, debug.Stack())\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Call Signal() here in case there are goroutines waiting for available workers.\n\t\t\tw.pool.cond.Signal()\n\t\t}()\n\n\t\tfor arg := range w.arg {\n\t\t\tif arg == nil {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tw.pool.fn(arg)\n\t\t\tif ok := w.pool.revertWorker(w); !ok {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}()\n}\n\nfunc (w *goWorkerWithFunc) finish() {\n\tw.arg <- nil\n}\n\nfunc (w *goWorkerWithFunc) lastUsedTime() int64 {\n\treturn w.lastUsed\n}\n\nfunc (w *goWorkerWithFunc) setLastUsedTime(t int64) {\n\tw.lastUsed = t\n}\n\nfunc (w *goWorkerWithFunc) inputArg(arg any) {\n\tw.arg <- arg\n}\n"
  },
  {
    "path": "worker_func_generic.go",
    "content": "// MIT License\n\n// Copyright (c) 2025 Andy Pan\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\npackage ants\n\nimport (\n\t\"runtime/debug\"\n)\n\n// goWorkerWithFunc is the actual executor who runs the tasks,\n// it starts a goroutine that accepts tasks and\n// performs function calls.\ntype goWorkerWithFuncGeneric[T any] struct {\n\tworker\n\n\t// pool who owns this worker.\n\tpool *PoolWithFuncGeneric[T]\n\n\t// arg is a job should be done.\n\targ chan T\n\n\t// exit signals the goroutine to exit.\n\texit chan struct{}\n\n\t// lastUsed will be updated when putting a worker back into queue.\n\tlastUsed int64\n}\n\n// run starts a goroutine to repeat the process\n// that performs the function calls.\nfunc (w *goWorkerWithFuncGeneric[T]) run() {\n\tw.pool.addRunning(1)\n\tgo func() {\n\t\tdefer func() {\n\t\t\tif w.pool.addRunning(-1) == 0 && w.pool.IsClosed() {\n\t\t\t\tw.pool.once.Do(func() {\n\t\t\t\t\tclose(w.pool.allDone)\n\t\t\t\t})\n\t\t\t}\n\t\t\tw.pool.workerCache.Put(w)\n\t\t\tif p := recover(); p != nil {\n\t\t\t\tif ph := w.pool.options.PanicHandler; ph != nil {\n\t\t\t\t\tph(p)\n\t\t\t\t} else {\n\t\t\t\t\tw.pool.options.Logger.Printf(\"worker exits from panic: %v\\n%s\\n\", p, debug.Stack())\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Call Signal() here in case there are goroutines waiting for available workers.\n\t\t\tw.pool.cond.Signal()\n\t\t}()\n\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase <-w.exit:\n\t\t\t\treturn\n\t\t\tcase arg := <-w.arg:\n\t\t\t\tw.pool.fn(arg)\n\t\t\t\tif ok := w.pool.revertWorker(w); !ok {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}()\n}\n\nfunc (w *goWorkerWithFuncGeneric[T]) finish() {\n\tw.exit <- struct{}{}\n}\n\nfunc (w *goWorkerWithFuncGeneric[T]) lastUsedTime() int64 {\n\treturn w.lastUsed\n}\n\nfunc (w *goWorkerWithFuncGeneric[T]) setLastUsedTime(t int64) {\n\tw.lastUsed = t\n}\n"
  },
  {
    "path": "worker_loop_queue.go",
    "content": "/*\n * Copyright (c) 2019. Ants Authors. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport \"time\"\n\ntype loopQueue struct {\n\titems  []worker\n\texpiry []worker\n\thead   int\n\ttail   int\n\tsize   int\n\tisFull bool\n}\n\nfunc newWorkerLoopQueue(size int) *loopQueue {\n\tif size <= 0 {\n\t\treturn nil\n\t}\n\treturn &loopQueue{\n\t\titems: make([]worker, size),\n\t\tsize:  size,\n\t}\n}\n\nfunc (wq *loopQueue) len() int {\n\tif wq.size == 0 || wq.isEmpty() {\n\t\treturn 0\n\t}\n\n\tif wq.head == wq.tail && wq.isFull {\n\t\treturn wq.size\n\t}\n\n\tif wq.tail > wq.head {\n\t\treturn wq.tail - wq.head\n\t}\n\n\treturn wq.size - wq.head + wq.tail\n}\n\nfunc (wq *loopQueue) isEmpty() bool {\n\treturn wq.head == wq.tail && !wq.isFull\n}\n\nfunc (wq *loopQueue) insert(w worker) error {\n\tif wq.isFull {\n\t\treturn errQueueIsFull\n\t}\n\twq.items[wq.tail] = w\n\twq.tail = (wq.tail + 1) % wq.size\n\n\tif wq.tail == wq.head {\n\t\twq.isFull = true\n\t}\n\n\treturn nil\n}\n\nfunc (wq *loopQueue) detach() worker {\n\tif wq.isEmpty() {\n\t\treturn nil\n\t}\n\n\tw := wq.items[wq.head]\n\twq.items[wq.head] = nil\n\twq.head = (wq.head + 1) % wq.size\n\n\twq.isFull = false\n\n\treturn w\n}\n\nfunc (wq *loopQueue) refresh(duration time.Duration) []worker {\n\texpiryTime := time.Now().Add(-duration).UnixNano()\n\tindex := wq.binarySearch(expiryTime)\n\tif index == -1 {\n\t\treturn nil\n\t}\n\twq.expiry = wq.expiry[:0]\n\n\tif wq.head <= index {\n\t\twq.expiry = append(wq.expiry, wq.items[wq.head:index+1]...)\n\t\tfor i := wq.head; i < index+1; i++ {\n\t\t\twq.items[i] = nil\n\t\t}\n\t} else {\n\t\twq.expiry = append(wq.expiry, wq.items[0:index+1]...)\n\t\twq.expiry = append(wq.expiry, wq.items[wq.head:]...)\n\t\tfor i := 0; i < index+1; i++ {\n\t\t\twq.items[i] = nil\n\t\t}\n\t\tfor i := wq.head; i < wq.size; i++ {\n\t\t\twq.items[i] = nil\n\t\t}\n\t}\n\thead := (index + 1) % wq.size\n\twq.head = head\n\tif len(wq.expiry) > 0 {\n\t\twq.isFull = false\n\t}\n\n\treturn wq.expiry\n}\n\nfunc (wq *loopQueue) binarySearch(expiryTime int64) int {\n\tvar mid, nlen, basel, tmid int\n\tnlen = len(wq.items)\n\n\t// if no need to remove work, return -1\n\tif wq.isEmpty() || expiryTime < wq.items[wq.head].lastUsedTime() {\n\t\treturn -1\n\t}\n\n\t// example\n\t// size = 8, head = 7, tail = 4\n\t// [ 2, 3, 4, 5, nil, nil, nil,  1]  true position\n\t//   0  1  2  3    4   5     6   7\n\t//              tail          head\n\t//\n\t//   1  2  3  4  nil nil   nil   0   mapped position\n\t//            r                  l\n\n\t// base algorithm is a copy from worker_stack\n\t// map head and tail to effective left and right\n\tr := (wq.tail - 1 - wq.head + nlen) % nlen\n\tbasel = wq.head\n\tl := 0\n\tfor l <= r {\n\t\tmid = l + ((r - l) >> 1) // avoid overflow when computing mid\n\t\t// calculate true mid position from mapped mid position\n\t\ttmid = (mid + basel + nlen) % nlen\n\t\tif expiryTime < wq.items[tmid].lastUsedTime() {\n\t\t\tr = mid - 1\n\t\t} else {\n\t\t\tl = mid + 1\n\t\t}\n\t}\n\t// return true position from mapped position\n\treturn (r + basel + nlen) % nlen\n}\n\nfunc (wq *loopQueue) reset() {\n\tif wq.isEmpty() {\n\t\treturn\n\t}\n\nretry:\n\tif w := wq.detach(); w != nil {\n\t\tw.finish()\n\t\tgoto retry\n\t}\n\twq.head = 0\n\twq.tail = 0\n}\n"
  },
  {
    "path": "worker_loop_queue_test.go",
    "content": "/*\n * Copyright (c) 2019. Ants Authors. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/require\"\n)\n\nfunc TestNewLoopQueue(t *testing.T) {\n\tsize := 100\n\tq := newWorkerLoopQueue(size)\n\trequire.EqualValues(t, 0, q.len(), \"Len error\")\n\trequire.Equal(t, true, q.isEmpty(), \"IsEmpty error\")\n\trequire.Nil(t, q.detach(), \"Dequeue error\")\n\n\trequire.Nil(t, newWorkerLoopQueue(0))\n}\n\nfunc TestLoopQueue(t *testing.T) {\n\tsize := 10\n\tq := newWorkerLoopQueue(size)\n\n\tfor i := 0; i < 5; i++ {\n\t\terr := q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t}\n\trequire.EqualValues(t, 5, q.len(), \"Len error\")\n\t_ = q.detach()\n\trequire.EqualValues(t, 4, q.len(), \"Len error\")\n\n\ttime.Sleep(time.Second)\n\n\tfor i := 0; i < 6; i++ {\n\t\terr := q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t}\n\trequire.EqualValues(t, 10, q.len(), \"Len error\")\n\n\terr := q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\trequire.Error(t, err, \"Enqueue, error\")\n\n\tq.refresh(time.Second)\n\trequire.EqualValuesf(t, 6, q.len(), \"Len error: %d\", q.len())\n}\n\nfunc TestRotatedQueueSearch(t *testing.T) {\n\tsize := 10\n\tq := newWorkerLoopQueue(size)\n\n\tcurrTime := time.Now().UnixNano()\n\n\t// 1\n\texpiry1 := currTime\n\tcurrTime++\n\t_ = q.insert(&goWorker{lastUsed: currTime})\n\n\trequire.EqualValues(t, 0, q.binarySearch(currTime), \"index should be 0\")\n\trequire.EqualValues(t, -1, q.binarySearch(expiry1), \"index should be -1\")\n\n\t// 2\n\tcurrTime++\n\texpiry2 := currTime\n\tcurrTime++\n\t_ = q.insert(&goWorker{lastUsed: currTime})\n\n\trequire.EqualValues(t, -1, q.binarySearch(expiry1), \"index should be -1\")\n\n\trequire.EqualValues(t, 0, q.binarySearch(expiry2), \"index should be 0\")\n\n\trequire.EqualValues(t, 1, q.binarySearch(currTime), \"index should be 1\")\n\n\t// more\n\tfor i := 0; i < 5; i++ {\n\t\tcurrTime++\n\t\t_ = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\n\tcurrTime++\n\texpiry3 := currTime\n\t_ = q.insert(&goWorker{lastUsed: expiry3})\n\n\tvar err error\n\tfor err != errQueueIsFull {\n\t\tcurrTime++\n\t\terr = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\n\trequire.EqualValues(t, 7, q.binarySearch(expiry3), \"index should be 7\")\n\n\t// rotate\n\tfor i := 0; i < 6; i++ {\n\t\t_ = q.detach()\n\t}\n\n\tcurrTime++\n\texpiry4 := currTime\n\t_ = q.insert(&goWorker{lastUsed: expiry4})\n\n\tfor i := 0; i < 4; i++ {\n\t\tcurrTime++\n\t\t_ = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\t//\thead = 6, tail = 5, insert direction ->\n\t// [expiry4, time, time, time,  time, nil/tail,  time/head, time, time, time]\n\trequire.EqualValues(t, 0, q.binarySearch(expiry4), \"index should be 0\")\n\n\tfor i := 0; i < 3; i++ {\n\t\t_ = q.detach()\n\t}\n\tcurrTime++\n\texpiry5 := currTime\n\t_ = q.insert(&goWorker{lastUsed: expiry5})\n\n\t//\thead = 6, tail = 5, insert direction ->\n\t// [expiry4, time, time, time,  time, expiry5,  nil/tail, nil, nil, time/head]\n\trequire.EqualValues(t, 5, q.binarySearch(expiry5), \"index should be 5\")\n\n\tfor i := 0; i < 3; i++ {\n\t\tcurrTime++\n\t\t_ = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\t//\thead = 9, tail = 9, insert direction ->\n\t// [expiry4, time, time, time,  time, expiry5,  time, time, time, time/head/tail]\n\trequire.EqualValues(t, -1, q.binarySearch(expiry2), \"index should be -1\")\n\n\trequire.EqualValues(t, 9, q.binarySearch(q.items[9].lastUsedTime()), \"index should be 9\")\n\trequire.EqualValues(t, 8, q.binarySearch(currTime), \"index should be 8\")\n}\n\nfunc TestRetrieveExpiry(t *testing.T) {\n\tsize := 10\n\tq := newWorkerLoopQueue(size)\n\texpirew := make([]worker, 0)\n\tu, _ := time.ParseDuration(\"1s\")\n\n\t// test [ time+1s, time+1s, time+1s, time+1s, time+1s, time, time, time, time, time]\n\tfor i := 0; i < size/2; i++ {\n\t\t_ = q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t}\n\texpirew = append(expirew, q.items[:size/2]...)\n\ttime.Sleep(u)\n\n\tfor i := 0; i < size/2; i++ {\n\t\t_ = q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t}\n\tworkers := q.refresh(u)\n\n\trequire.EqualValues(t, expirew, workers, \"expired workers aren't right\")\n\n\t// test [ time, time, time, time, time, time+1s, time+1s, time+1s, time+1s, time+1s]\n\ttime.Sleep(u)\n\n\tfor i := 0; i < size/2; i++ {\n\t\t_ = q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t}\n\texpirew = expirew[:0]\n\texpirew = append(expirew, q.items[size/2:]...)\n\n\tworkers2 := q.refresh(u)\n\n\trequire.EqualValues(t, expirew, workers2, \"expired workers aren't right\")\n\n\t// test [ time+1s, time+1s, time+1s, nil, nil, time+1s, time+1s, time+1s, time+1s, time+1s]\n\tfor i := 0; i < size/2; i++ {\n\t\t_ = q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t}\n\tfor i := 0; i < size/2; i++ {\n\t\t_ = q.detach()\n\t}\n\tfor i := 0; i < 3; i++ {\n\t\t_ = q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t}\n\ttime.Sleep(u)\n\n\texpirew = expirew[:0]\n\texpirew = append(expirew, q.items[0:3]...)\n\texpirew = append(expirew, q.items[size/2:]...)\n\n\tworkers3 := q.refresh(u)\n\n\trequire.EqualValues(t, expirew, workers3, \"expired workers aren't right\")\n}\n"
  },
  {
    "path": "worker_queue.go",
    "content": "/*\n * Copyright (c) 2019. Ants Authors. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport (\n\t\"errors\"\n\t\"time\"\n)\n\n// errQueueIsFull will be returned when the worker queue is full.\nvar errQueueIsFull = errors.New(\"the queue is full\")\n\ntype worker interface {\n\trun()\n\tfinish()\n\tlastUsedTime() int64\n\tsetLastUsedTime(t int64)\n\tinputFunc(func())\n\tinputArg(any)\n}\n\ntype workerQueue interface {\n\tlen() int\n\tisEmpty() bool\n\tinsert(worker) error\n\tdetach() worker\n\trefresh(duration time.Duration) []worker // clean up the stale workers and return them\n\treset()\n}\n\ntype queueType int\n\nconst (\n\tqueueTypeStack queueType = 1 << iota\n\tqueueTypeLoopQueue\n)\n\nfunc newWorkerQueue(qType queueType, size int) workerQueue {\n\tswitch qType {\n\tcase queueTypeStack:\n\t\treturn newWorkerStack(size)\n\tcase queueTypeLoopQueue:\n\t\treturn newWorkerLoopQueue(size)\n\tdefault:\n\t\treturn newWorkerStack(size)\n\t}\n}\n"
  },
  {
    "path": "worker_stack.go",
    "content": "/*\n * Copyright (c) 2019. Ants Authors. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport \"time\"\n\ntype workerStack struct {\n\titems  []worker\n\texpiry []worker\n}\n\nfunc newWorkerStack(size int) *workerStack {\n\treturn &workerStack{\n\t\titems: make([]worker, 0, size),\n\t}\n}\n\nfunc (ws *workerStack) len() int {\n\treturn len(ws.items)\n}\n\nfunc (ws *workerStack) isEmpty() bool {\n\treturn len(ws.items) == 0\n}\n\nfunc (ws *workerStack) insert(w worker) error {\n\tws.items = append(ws.items, w)\n\treturn nil\n}\n\nfunc (ws *workerStack) detach() worker {\n\tl := ws.len()\n\tif l == 0 {\n\t\treturn nil\n\t}\n\n\tw := ws.items[l-1]\n\tws.items[l-1] = nil // avoid memory leaks\n\tws.items = ws.items[:l-1]\n\n\treturn w\n}\n\nfunc (ws *workerStack) refresh(duration time.Duration) []worker {\n\tn := ws.len()\n\tif n == 0 {\n\t\treturn nil\n\t}\n\n\texpiryTime := time.Now().Add(-duration).UnixNano()\n\tindex := ws.binarySearch(0, n-1, expiryTime)\n\n\tws.expiry = ws.expiry[:0]\n\tif index != -1 {\n\t\tws.expiry = append(ws.expiry, ws.items[:index+1]...)\n\t\tm := copy(ws.items, ws.items[index+1:])\n\t\tfor i := m; i < n; i++ {\n\t\t\tws.items[i] = nil\n\t\t}\n\t\tws.items = ws.items[:m]\n\t}\n\treturn ws.expiry\n}\n\nfunc (ws *workerStack) binarySearch(l, r int, expiryTime int64) int {\n\tfor l <= r {\n\t\tmid := l + ((r - l) >> 1) // avoid overflow when computing mid\n\t\tif expiryTime < ws.items[mid].lastUsedTime() {\n\t\t\tr = mid - 1\n\t\t} else {\n\t\t\tl = mid + 1\n\t\t}\n\t}\n\treturn r\n}\n\nfunc (ws *workerStack) reset() {\n\tfor i := 0; i < ws.len(); i++ {\n\t\tws.items[i].finish()\n\t\tws.items[i] = nil\n\t}\n\tws.items = ws.items[:0]\n}\n"
  },
  {
    "path": "worker_stack_test.go",
    "content": "/*\n * Copyright (c) 2019. Ants Authors. All rights reserved.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\npackage ants\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/require\"\n)\n\nfunc TestNewWorkerStack(t *testing.T) {\n\tsize := 100\n\tq := newWorkerStack(size)\n\trequire.EqualValues(t, 0, q.len(), \"Len error\")\n\trequire.Equal(t, true, q.isEmpty(), \"IsEmpty error\")\n\trequire.Nil(t, q.detach(), \"Dequeue error\")\n}\n\nfunc TestWorkerStack(t *testing.T) {\n\tq := newWorkerQueue(queueType(-1), 0)\n\n\tfor i := 0; i < 5; i++ {\n\t\terr := q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t}\n\trequire.EqualValues(t, 5, q.len(), \"Len error\")\n\n\texpired := time.Now().UnixNano()\n\n\terr := q.insert(&goWorker{lastUsed: expired})\n\tif err != nil {\n\t\tt.Fatal(\"Enqueue error\")\n\t}\n\n\ttime.Sleep(time.Second)\n\n\tfor i := 0; i < 6; i++ {\n\t\terr := q.insert(&goWorker{lastUsed: time.Now().UnixNano()})\n\t\tif err != nil {\n\t\t\tt.Fatal(\"Enqueue error\")\n\t\t}\n\t}\n\trequire.EqualValues(t, 12, q.len(), \"Len error\")\n\tq.refresh(time.Second)\n\trequire.EqualValues(t, 6, q.len(), \"Len error\")\n}\n\n// It seems that something wrong with time.Now() on Windows, not sure whether it is a bug on Windows,\n// so exclude this test from Windows platform temporarily.\nfunc TestSearch(t *testing.T) {\n\tq := newWorkerStack(0)\n\n\tcurrTime := time.Now().UnixNano()\n\n\t// 1\n\texpiry1 := currTime\n\tcurrTime++\n\t_ = q.insert(&goWorker{lastUsed: currTime})\n\n\trequire.EqualValues(t, 0, q.binarySearch(0, q.len()-1, currTime), \"index should be 0\")\n\trequire.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), \"index should be -1\")\n\n\t// 2\n\tcurrTime++\n\texpiry2 := currTime\n\tcurrTime++\n\t_ = q.insert(&goWorker{lastUsed: currTime})\n\n\trequire.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), \"index should be -1\")\n\n\trequire.EqualValues(t, 0, q.binarySearch(0, q.len()-1, expiry2), \"index should be 0\")\n\n\trequire.EqualValues(t, 1, q.binarySearch(0, q.len()-1, currTime), \"index should be 1\")\n\n\t// more\n\tfor i := 0; i < 5; i++ {\n\t\tcurrTime++\n\t\t_ = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\n\tcurrTime++\n\texpiry3 := currTime\n\n\t_ = q.insert(&goWorker{lastUsed: expiry3})\n\n\tfor i := 0; i < 10; i++ {\n\t\tcurrTime++\n\t\t_ = q.insert(&goWorker{lastUsed: currTime})\n\t}\n\n\trequire.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), \"index should be 7\")\n}\n"
  }
]