Skip to content

Thread safety and pooling best practices #53

@programcsharp

Description

@programcsharp

It's not covered in the docs, but I believe the client is thread safe as it uses HttpClient under the covers for everything and that's thread safe.

Can you confirm that the client is indeed thread safe?

If so, I'd recommend adding a "best practices" section to the docs. All of the examples show newing up a client every time. That ends up doing expensive init and dns lookups every time. Reusing a single client (both the base PineconeClient and the IndexClient for an individual index) avoids that several second init.

I'm using something like this:

public class PineconeService
{
    static readonly ConcurrentDictionary<string, IndexClient> _indexClients = new();
    static readonly string PineconeToken = ConfigurationManager.AppSettings["PineconeToken"];
    
    static readonly Lazy<PineconeClient> _client = new(() => new PineconeClient(PineconeToken));
    
    public static PineconeClient Client => _client.Value;
    
    public static IndexClient GetIndexClient(string indexName)
    {
        return _indexClients.GetOrAdd(indexName, name => Client.Index(name: name));
    }
    
    public static void ClearIndexCache() => _indexClients.Clear();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions