https://stackoverflow.com/questions/49588205/should-restclient-be-singleton-or-new-for-every-request

Q1:

should I use singleton pattern for RestClient or should I new it everytime, if I new it everytime will any performance concern?

A1:

Recommended way to use RestSharp is to create a new instance per request.

It differs from Singleton approach recommended for HttpClient. And the reason is that under the hood RestSharp uses HttpWebRequest for HTTP interaction, not HttpClient. That's why the usage model differs.

Q2:

If I create it everytime do I get performance issue just like the HttpClient?

A2:

The main reason why you shouldn't create a new instance of HttpClient for each request is not a performance consideration. The time spent for creation and initialization will take a tiny fraction of time spent for following network call. The main reason to use singleton instance of HttpClient is the following:

HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of RestClient per request.

Will you gain any performance improvement if you use reuse instance of RestClient? Well, you will save the time for creation of object and its initialization. However this time is very close to 0 and moreover it's a tiny fraction of time spent for following network call. You don't reuse other .NET objects like List<T> because of performance considerations, are you? You should do the same for RestClient. It's just developed in a way that implies such usage scenario.

HttpClient不必每次新建实例而RestSharp推荐新建实例的原因的更多相关文章

  1. Asp.Net MVC是否针对每次请求都重新创建一个控制器实例

    一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...

  2. 9.scrapy pycharm调试小技巧,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍

    pycharm调试技巧:调试时,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍 [用法]cmd命令运行:scrapy shell 网址 第一步,cmd进行一次请求: scrapy sh ...

  3. Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串

    ylbtech-Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串 1.返回顶部 1. Java 实例 - 连接字符串  Java 实例 以下实例演示了通过 &qu ...

  4. Windows 8实例教程系列 - 数据绑定基础实例

    原文:Windows 8实例教程系列 - 数据绑定基础实例 数据绑定是WPF,Silverlight以及Windows Phone应用开发中最为常用的开发技术,在基于XAML的Windows Stor ...

  5. Windows 8实例教程系列 - 数据绑定高级实例

    原文:Windows 8实例教程系列 - 数据绑定高级实例 上篇Windows 8实例教程系列 - 数据绑定基础实例中,介绍Windows 8应用开发数据绑定基础,其中包括一些简单的数据绑定控件的使用 ...

  6. Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法-un

    ylbtech-Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法 1.返回顶部 1. Java 实例 - 汉诺塔算法  Java 实例 汉诺塔(又称河内塔)问题是源 ...

  7. centos下mysql多实例安装3306、3307实例(2014-10-15)

    背景说明       mysql的安装方法有多种,如二进制安装.源代码编译安装.yum安装等.yum安装仅仅能安装mysql 5.1 版本号:源代码安装编译的过程比較长.若没有对源代码进行改动且要求使 ...

  8. 官网实例详解-目录和实例简介-keras学习笔记四

    官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras   版权声明: ...

  9. Python - 面向对象编程 - 类变量、实例变量/类属性、实例属性

    什么是对象和类 https://www.cnblogs.com/poloyy/p/15178423.html 什么是 Python 类.类对象.实例对象 https://www.cnblogs.com ...

随机推荐

  1. [BZOJ 1095] [ZJOI 2007] 捉迷藏

    Description 传送门 Solution 先将原树转化成点分树: 然后维护三个堆: \(c[i]\) 保存点分树中子树 \(i\) 中的黑色节点到 \(fa[i]\) 的距离: \(b[i]\ ...

  2. bzoj 3669: [Noi2014]魔法森林 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec  ...

  3. git只追踪特定类型的文件

    比如我只关心所有office文档并排除掉~开头的辅助文件: * !*/ !*.docx !*.doc !*.xlsx ~*

  4. Keras 获取中间某一层输出

    1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict. #coding=utf-8 import seaborn as ...

  5. OpenStack视图

    OpenStack视图 OpenStack视图是个全局资源的概念,统计了OpenStack所纳管资源的总量和使用量,因此OpenStack视图的资源通常又称为物理资源.OpenStack基于该资源使用 ...

  6. 小白月赛13 小A的柱状图 (单调栈)

    链接:https://ac.nowcoder.com/acm/contest/549/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  7. win10更改无线网卡的MAC地址

    https://blog.csdn.net/qq_31778495/article/details/80932472 前段时间电脑蹭网被禁了MAC地址,故寻找修改MAC地址的方法. 本机配置: win ...

  8. np.random.shuffle(x)的用法

    此函数主要是通过改变序列的内容来修改序列的位置.此函数只沿多维数组的第一个轴移动数组.子数组的顺序已更改,但其内容保持不变. 参数 x:即将被打乱顺序的list 返回值 无

  9. 第三周博客作业<西北师范大学|李晓婷>

    1.助教博客链接:https://www.cnblogs.com/lxt-/MyComments.html 2.学生作业打分要求:   https://www.cnblogs.com/nwnu-dai ...

  10. 20175209 《Java程序设计》第七周学习总结

    20175209 <Java程序设计>第七周学习总结 一.教材知识点总结 第八章 常用类和实用类 1.String类 构造String对象 常量对象:""中的字符序列, ...