是否可以确定 Thrift TBaseClient 当前是否繁忙或可用? [英] Is it possible to determine whether a Thrift TBaseClient is currently busy or available?

查看:23
本文介绍了是否可以确定 Thrift TBaseClient 当前是否繁忙或可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的系统中,我们有一个 C++ 组件作为 Thrift Server,一个 .netCore/C# 组件作为客户端.到目前为止,我管理的是单个连接,因此使用单例来创建实现 TBaseClient 的 ThriftPushClientWrapper.(通过thrift接口生成的对象)

In our system, we have one C++ component acting as a Thrift Server, and one .netCore/C# component as a client. So far, I was managing a single connection, so using a singleton to create my ThriftPushClientWrapper which implements TBaseClient. (via the generated object from the thrift interface)

.AddSingleton<IThriftPushClientWrapper>(sp =>
{
     var localIpAddress = IPAddress.Parse(serverIp);
     var transport = new TSocketTransport(localIpAddress, dataPort);
     var protocol = new TBinaryProtocol(transport);
     return new ThriftPushClientWrapper(protocol);
});

(目前使用的是 0.13 版本的 Thrift 库,需要尽快更新到 0.14.1,但不知道服务器部分是否也必须/先更新).这很好用.

(so far using 0.13 version of the Thrift library, need to update to 0.14.1 soon, but wonder if the server part must be updated too/first). This is working great.

现在,我想要多个客户端可以同时连接到服务器,都在同一个 ip:port所以我正在启动一个ClientFactory,但不知道如何处理客户端的创建.更准确地说,服务器部分配置为5个线程,所以我需要5个客户端.

Now, I want multiple clients that can connect to the server simultaneously, all on the same ip:port So I am starting a ClientFactory, but wonder how to deal with the creation of the client. To be more precise, the server part is configured for 5 threads, so I need 5 clients.

一种简单的方法是每次都创建一个新客户端,但可能效率低下.

One simple approach would be to create a new client each time, but probably inefficient.

更好的方法是收集 5 个客户端,然后使用下一个可用的免费客户端.所以我从下面的工厂开始,我应该从外面获取索引.

A better approach is to have a collection of 5 clients, and using the next available free one. So I started with the following factory, where I should get the index from outside.

private readonly ConcurrentDictionary<int, IThriftPushClientWrapper> _clientDict;

public IThriftPushClientWrapper GetNextAvailablePushClient(int index)
{
    IThriftPushClientWrapper client;
    if (_clientDict.ContainsKey(index))
    {
        if (_clientDict.TryGetValue(index, out client) && client != null)
            return client;
        else  // error handling
    }

    // add new client for the expecting index
    client = CreateNewPushClient();
    _clientDict.TryAdd(index, client);
    return client;
}

private IThriftPushClientWrapper CreateNewPushClient()
{        
    var localIpAddress = IPAddress.Parse(serverIp);
    var transport = new TSocketTransport(localIpAddress, dataPort);
    var protocol = new TBinaryProtocol(transport);
    return new ThriftPushClientWrapper(protocol);
}

我的下一个问题是确定如何从外部设置索引.我从使用 semaphore.CurrentCount 作为索引的 SemaphoreSlim(5,5) 开始,但可能不是最好的主意.还尝试使用从 0 到 5 的滚动索引.但显然,CancellationToken 用于取消进一步处理.还不确定根本原因.

My next issue it to determine how to set the index from outside. I started with a SemaphoreSlim(5,5) using the semaphore.CurrentCount as index, but probably not the best idea. Also tried with a rolling index from 0 to 5. But apparently, a CancellationToken is used to cancel further procceesing. Not sure the root cause yet.

是否可以确定 TBaseClient 当前是否繁忙或可用?

Is it possible to determine whether a TBaseClient is currently busy or available?

处理大量客户的推荐策略是什么?

What is the recommended strategy to deal with a pool of clients?

推荐答案

解决这个问题最简单的方法就是把事情做对.如果您要使用资源池中的某些资源,请将其从资源池中移除,或者以某种合适的方式将其标记为当时使用.

The easiest solution to solve this is to do it right. If you are going to use some resource from a pool of resources, either get it off the pool, or mark it used in some suitable way for that time.

值得注意的是,这个问题与 Thrift 无关.您正试图通过尝试利用其他人的代码来解决薄弱的资源管理方法,这些代码从未打算在这种情况下工作.

It's notable that the question has nothing to do with Thrift in particular. You are trying to solve a weak resource management approach by trying to leverage other peoples code that was never intended to work in such a context.

关于如何实现对象池,这个其他问题可以提供进一步的建议.另请记住,尤其是在 Windows 平台上,并非所有系统资源都可以跨线程自由共享.

Regarding how to implement object pooling, this other question can provide further advice. Also keep in mind that especially on Windows platforms not all system resources can be shared freely across threads.

这篇关于是否可以确定 Thrift TBaseClient 当前是否繁忙或可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆