feat: add setting item for api request timeout

This commit is contained in:
genteure 2022-08-25 19:23:18 +08:00
parent ba57f0cc21
commit 82f281e3f5
6 changed files with 55 additions and 7 deletions

View File

@ -32,6 +32,7 @@ namespace BililiveRecorder.Cli.Configure
Cookie,
LiveApiHost,
TimingCheckInterval,
TimingApiTimeout,
TimingStreamRetry,
TimingStreamRetryNoQn,
TimingStreamConnect,
@ -85,6 +86,7 @@ namespace BililiveRecorder.Cli.Configure
GlobalConfig.Add(GlobalConfigProperties.Cookie, new ConfigInstruction<GlobalConfig, string>(config => config.HasCookie = false, (config, value) => config.Cookie = value) { Name = "Cookie", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.LiveApiHost, new ConfigInstruction<GlobalConfig, string>(config => config.HasLiveApiHost = false, (config, value) => config.LiveApiHost = value) { Name = "LiveApiHost", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.TimingCheckInterval, new ConfigInstruction<GlobalConfig, uint>(config => config.HasTimingCheckInterval = false, (config, value) => config.TimingCheckInterval = value) { Name = "TimingCheckInterval", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.TimingApiTimeout, new ConfigInstruction<GlobalConfig, uint>(config => config.HasTimingApiTimeout = false, (config, value) => config.TimingApiTimeout = value) { Name = "TimingApiTimeout", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.TimingStreamRetry, new ConfigInstruction<GlobalConfig, uint>(config => config.HasTimingStreamRetry = false, (config, value) => config.TimingStreamRetry = value) { Name = "TimingStreamRetry", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.TimingStreamRetryNoQn, new ConfigInstruction<GlobalConfig, uint>(config => config.HasTimingStreamRetryNoQn = false, (config, value) => config.TimingStreamRetryNoQn = value) { Name = "TimingStreamRetryNoQn", CanBeOptional = true });
GlobalConfig.Add(GlobalConfigProperties.TimingStreamConnect, new ConfigInstruction<GlobalConfig, uint>(config => config.HasTimingStreamConnect = false, (config, value) => config.TimingStreamConnect = value) { Name = "TimingStreamConnect", CanBeOptional = true });

View File

@ -17,7 +17,6 @@ namespace BililiveRecorder.Core.Api.Http
internal const string HttpHeaderReferer = "https://live.bilibili.com/";
internal const string HttpHeaderOrigin = "https://live.bilibili.com";
internal const string HttpHeaderUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36";
private static readonly TimeSpan TimeOutTimeSpan = TimeSpan.FromSeconds(15);
private readonly GlobalConfig config;
private readonly HttpClient anonClient;
@ -33,11 +32,11 @@ namespace BililiveRecorder.Core.Api.Http
config.PropertyChanged += this.Config_PropertyChanged;
this.mainClient = null!;
this.SetCookie();
this.UpdateHttpClient();
this.anonClient = new HttpClient
{
Timeout = TimeOutTimeSpan
Timeout = TimeSpan.FromMilliseconds(config.TimingApiTimeout)
};
var headers = this.anonClient.DefaultRequestHeaders;
headers.Add("Accept", HttpHeaderAccept);
@ -46,7 +45,7 @@ namespace BililiveRecorder.Core.Api.Http
headers.Add("User-Agent", HttpHeaderUserAgent);
}
private void SetCookie()
private void UpdateHttpClient()
{
var client = new HttpClient(new HttpClientHandler
{
@ -54,7 +53,7 @@ namespace BililiveRecorder.Core.Api.Http
UseDefaultCredentials = false,
})
{
Timeout = TimeOutTimeSpan
Timeout = TimeSpan.FromMilliseconds(this.config.TimingApiTimeout)
};
var headers = client.DefaultRequestHeaders;
headers.Add("Accept", HttpHeaderAccept);
@ -72,8 +71,8 @@ namespace BililiveRecorder.Core.Api.Http
private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(this.config.Cookie))
this.SetCookie();
if (e.PropertyName == nameof(this.config.Cookie) || e.PropertyName == nameof(this.config.TimingApiTimeout))
this.UpdateHttpClient();
}
private static async Task<BilibiliApiResponse<T>> FetchAsync<T>(HttpClient client, string url) where T : class

View File

@ -149,6 +149,11 @@ namespace BililiveRecorder.Core.Config.V3
/// </summary>
public uint TimingCheckInterval => this.GetPropertyValue<uint>();
/// <summary>
/// 请求B站API超时时间 毫秒
/// </summary>
public uint TimingApiTimeout => this.GetPropertyValue<uint>();
/// <summary>
/// 录制断开重连时间间隔 毫秒
/// </summary>
@ -348,6 +353,14 @@ namespace BililiveRecorder.Core.Config.V3
[JsonProperty(nameof(TimingCheckInterval)), EditorBrowsable(EditorBrowsableState.Never)]
public Optional<uint> OptionalTimingCheckInterval { get => this.GetPropertyValueOptional<uint>(nameof(this.TimingCheckInterval)); set => this.SetPropertyValueOptional(value, nameof(this.TimingCheckInterval)); }
/// <summary>
/// 请求B站API超时时间 毫秒
/// </summary>
public uint TimingApiTimeout { get => this.GetPropertyValue<uint>(); set => this.SetPropertyValue(value); }
public bool HasTimingApiTimeout { get => this.GetPropertyHasValue(nameof(this.TimingApiTimeout)); set => this.SetPropertyHasValue<uint>(value, nameof(this.TimingApiTimeout)); }
[JsonProperty(nameof(TimingApiTimeout)), EditorBrowsable(EditorBrowsableState.Never)]
public Optional<uint> OptionalTimingApiTimeout { get => this.GetPropertyValueOptional<uint>(nameof(this.TimingApiTimeout)); set => this.SetPropertyValueOptional(value, nameof(this.TimingApiTimeout)); }
/// <summary>
/// 录制断开重连时间间隔 毫秒
/// </summary>
@ -471,6 +484,8 @@ namespace BililiveRecorder.Core.Config.V3
public uint TimingCheckInterval => 600;
public uint TimingApiTimeout => 10000;
public uint TimingStreamRetry => 6000;
public uint TimingStreamRetryNoQn => 90;

View File

@ -60,6 +60,7 @@ namespace BililiveRecorder.Web.Models
public Optional<string?>? OptionalCookie { get; set; }
public Optional<string?>? OptionalLiveApiHost { get; set; }
public Optional<uint>? OptionalTimingCheckInterval { get; set; }
public Optional<uint>? OptionalTimingApiTimeout { get; set; }
public Optional<uint>? OptionalTimingStreamRetry { get; set; }
public Optional<uint>? OptionalTimingStreamRetryNoQn { get; set; }
public Optional<uint>? OptionalTimingStreamConnect { get; set; }
@ -91,6 +92,7 @@ namespace BililiveRecorder.Web.Models
if (this.OptionalCookie.HasValue) config.OptionalCookie = this.OptionalCookie.Value;
if (this.OptionalLiveApiHost.HasValue) config.OptionalLiveApiHost = this.OptionalLiveApiHost.Value;
if (this.OptionalTimingCheckInterval.HasValue) config.OptionalTimingCheckInterval = this.OptionalTimingCheckInterval.Value;
if (this.OptionalTimingApiTimeout.HasValue) config.OptionalTimingApiTimeout = this.OptionalTimingApiTimeout.Value;
if (this.OptionalTimingStreamRetry.HasValue) config.OptionalTimingStreamRetry = this.OptionalTimingStreamRetry.Value;
if (this.OptionalTimingStreamRetryNoQn.HasValue) config.OptionalTimingStreamRetryNoQn = this.OptionalTimingStreamRetryNoQn.Value;
if (this.OptionalTimingStreamConnect.HasValue) config.OptionalTimingStreamConnect = this.OptionalTimingStreamConnect.Value;
@ -143,6 +145,7 @@ namespace BililiveRecorder.Web.Models.Rest
public Optional<string?> OptionalCookie { get; set; }
public Optional<string?> OptionalLiveApiHost { get; set; }
public Optional<uint> OptionalTimingCheckInterval { get; set; }
public Optional<uint> OptionalTimingApiTimeout { get; set; }
public Optional<uint> OptionalTimingStreamRetry { get; set; }
public Optional<uint> OptionalTimingStreamRetryNoQn { get; set; }
public Optional<uint> OptionalTimingStreamConnect { get; set; }
@ -200,6 +203,7 @@ namespace BililiveRecorder.Web.Models.Graphql
this.Field(x => x.OptionalCookie, type: typeof(HierarchicalOptionalType<string>));
this.Field(x => x.OptionalLiveApiHost, type: typeof(HierarchicalOptionalType<string>));
this.Field(x => x.OptionalTimingCheckInterval, type: typeof(HierarchicalOptionalType<uint>));
this.Field(x => x.OptionalTimingApiTimeout, type: typeof(HierarchicalOptionalType<uint>));
this.Field(x => x.OptionalTimingStreamRetry, type: typeof(HierarchicalOptionalType<uint>));
this.Field(x => x.OptionalTimingStreamRetryNoQn, type: typeof(HierarchicalOptionalType<uint>));
this.Field(x => x.OptionalTimingStreamConnect, type: typeof(HierarchicalOptionalType<uint>));
@ -235,6 +239,7 @@ namespace BililiveRecorder.Web.Models.Graphql
this.Field(x => x.Cookie);
this.Field(x => x.LiveApiHost);
this.Field(x => x.TimingCheckInterval);
this.Field(x => x.TimingApiTimeout);
this.Field(x => x.TimingStreamRetry);
this.Field(x => x.TimingStreamRetryNoQn);
this.Field(x => x.TimingStreamConnect);
@ -288,6 +293,7 @@ namespace BililiveRecorder.Web.Models.Graphql
this.Field(x => x.OptionalCookie, nullable: true, type: typeof(HierarchicalOptionalInputType<string>));
this.Field(x => x.OptionalLiveApiHost, nullable: true, type: typeof(HierarchicalOptionalInputType<string>));
this.Field(x => x.OptionalTimingCheckInterval, nullable: true, type: typeof(HierarchicalOptionalInputType<uint>));
this.Field(x => x.OptionalTimingApiTimeout, nullable: true, type: typeof(HierarchicalOptionalInputType<uint>));
this.Field(x => x.OptionalTimingStreamRetry, nullable: true, type: typeof(HierarchicalOptionalInputType<uint>));
this.Field(x => x.OptionalTimingStreamRetryNoQn, nullable: true, type: typeof(HierarchicalOptionalInputType<uint>));
this.Field(x => x.OptionalTimingStreamConnect, nullable: true, type: typeof(HierarchicalOptionalInputType<uint>));

View File

@ -137,6 +137,24 @@
}
}
},
"TimingApiTimeout": {
"description": "请求B站API超时时间 毫秒\n默认: 10000",
"markdownDescription": "请求B站API超时时间 毫秒 \n默认: `10000 `\n\n",
"type": "object",
"additionalProperties": false,
"properties": {
"HasValue": {
"type": "boolean",
"default": true
},
"Value": {
"type": "integer",
"minimum": 0,
"maximum": 4294967295,
"default": 10000
}
}
},
"TimingStreamRetry": {
"description": "录制断开重连时间间隔 毫秒\n默认: 6000",
"markdownDescription": "录制断开重连时间间隔 毫秒 \n默认: `6000 `\n\n",

View File

@ -145,6 +145,14 @@ export const data: Array<ConfigEntry> = [
advancedConfig: true,
default: 10 * 60,
},
{
id: "TimingApiTimeout",
name: "请求B站API超时时间 毫秒",
type: "uint",
configType: "globalOnly",
advancedConfig: true,
default: 10 * 1000,
},
{
id: "TimingStreamRetry",
name: "录制断开重连时间间隔 毫秒",