HttpClient不必每次新建实例而RestSharp推荐新建实例的原因
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推荐新建实例的原因的更多相关文章
- Asp.Net MVC是否针对每次请求都重新创建一个控制器实例
一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...
- 9.scrapy pycharm调试小技巧,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍
pycharm调试技巧:调试时,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍 [用法]cmd命令运行:scrapy shell 网址 第一步,cmd进行一次请求: scrapy sh ...
- Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串
ylbtech-Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串 1.返回顶部 1. Java 实例 - 连接字符串 Java 实例 以下实例演示了通过 &qu ...
- Windows 8实例教程系列 - 数据绑定基础实例
原文:Windows 8实例教程系列 - 数据绑定基础实例 数据绑定是WPF,Silverlight以及Windows Phone应用开发中最为常用的开发技术,在基于XAML的Windows Stor ...
- Windows 8实例教程系列 - 数据绑定高级实例
原文:Windows 8实例教程系列 - 数据绑定高级实例 上篇Windows 8实例教程系列 - 数据绑定基础实例中,介绍Windows 8应用开发数据绑定基础,其中包括一些简单的数据绑定控件的使用 ...
- Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法-un
ylbtech-Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法 1.返回顶部 1. Java 实例 - 汉诺塔算法 Java 实例 汉诺塔(又称河内塔)问题是源 ...
- centos下mysql多实例安装3306、3307实例(2014-10-15)
背景说明 mysql的安装方法有多种,如二进制安装.源代码编译安装.yum安装等.yum安装仅仅能安装mysql 5.1 版本号:源代码安装编译的过程比較长.若没有对源代码进行改动且要求使 ...
- 官网实例详解-目录和实例简介-keras学习笔记四
官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras 版权声明: ...
- Python - 面向对象编程 - 类变量、实例变量/类属性、实例属性
什么是对象和类 https://www.cnblogs.com/poloyy/p/15178423.html 什么是 Python 类.类对象.实例对象 https://www.cnblogs.com ...
随机推荐
- SPFA求最短路——Bellman-Ford算法的优化
SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA 最坏情况下复杂度和朴素 Bellman-Ford 相同,为 O(VE), ...
- Nginx从入门到实践(一)
结合实践.收集各种场景.常见问题,讲解Nginx中最实用的Webserver场景,提供一套整体的搭建配置方式 Nginx中间件,不局限于业务逻辑,有效独立于后台开发框架(不论后端是Java开发.PHP ...
- 【BZOJ3996】[TJOI2015]线性代数(最小割)
[BZOJ3996][TJOI2015]线性代数(最小割) 题面 BZOJ 洛谷 题解 首先把式子拆开,发现我们的答案式就是这个: \[\sum_{i=1}^n\sum_{j=1}^n B_{i,j} ...
- 内网MySQL YUM源记录
#mysql yum */180 * * * * rsync -av --delete rsync://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-con ...
- Ubuntu安装配置protobuf 2.5
Ubuntu安装配置protobuf 2.5 一.安装配置环境 Linux 1.安装protobuf 下载文件 https://github.com/protocolbuffers/protobuf/ ...
- spring的事件驱动模型
在工作中会遇到这样的业务,生成一个订单后需要给指定的用户发送短信或者邮件,但是短信或者邮件发送失败又不会影响正常的业务: 这里介绍通过ApplicationContext和spring的@EventL ...
- .NET框架 - NETCORE + API + EF + MYSQL
.NET框架 - NETCORE + API + EFCORE + MYSQL 1. 新建项目: 本文中使用 框架 .netcore2.2 . 2. 生成项目框架 3 安装MYSQL插件 点击“工具” ...
- 第二章,循环结构,输入输出,clock
计时 计时函数: clock() 返回目前为止运行的时间 注意要除以常数 CLOCKS_PER_SEC, 才能得到以秒为单位. 头文件 time.h 管道 在windows命令行下执行echo 20| ...
- 面向对象学习(python)
面向对象总结 一.面向对象与面向过程的区别 面向过程:根据业务逻辑从上到下写垒代码 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 1.面向过程编程: 概念:发过程中最常见的操作就是粘贴 ...
- Pollard-rho算法学习笔记
写在前面 目录 问题的提出 生日悖论 利用生日悖论来因数分解 Pollard-rho算法 例题完成进度 1/3 问题的提出 如何快速地找到一个自然数N的两个因数(除自己和1以外)? 首先想到的肯定是传 ...