Cache sniffio availability instead of importing it on every call
current_async_library() re-imported sniffio on every invocation, falling back to "asyncio" on ImportError. Since anyio>=4.11 no longer depends on sniffio, anyone using httpcore2[asyncio] without trio installed pays a failed-import cost on every lock/event/semaphore setup, and Python doesn't cache failed imports.
Now sniffio is imported once at module load, like trio and anyio already are, and current_async_library() just checks the cached reference. Only availability is cached, not the detected backend.
Reported in encode/httpcore discussion #1102, with the fix and test strategy suggested in a follow-up comment:
https://github.com/encode/httpcore/discussions/1102#discussioncomment-17975309
Summary
improve import time when modern anyio =->4.11 is installed and sniffio isn't
Checklist
- I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.