Skip to content

Commit

Permalink
feat: 加入改变重试次数的环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 29, 2020
1 parent 7f897c1 commit 34ecfec
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 13 deletions.
15 changes: 14 additions & 1 deletion docs/guide/advance/api-gateway-beta.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ module.exports = gateway.createHttpServer();

欢迎在交流群内汇报问题!

## 常见问题

1. 由于免费用户单次请求的超时时间为 10s,所以不建议使用过多的远程片段、较高的超时时间和重试机制。你可以在 `now.json` 中加入以下的环境变量缓解遇到 Vercel 超时报错的概率:

```json
{
"env": {
"NETWORK_RETRY": 0,
"NETWORK_TIMEOUT": "5000"
}
}
```

## 新增功能

### 1. Artifact 分类
Expand Down Expand Up @@ -166,7 +179,7 @@ https://surgio-demo.herokuapp.com/get-artifact/
```

:::tip 提示
Heroku 今后会和 now.sh 一样成为推荐的部署网关平台。
Heroku 今后会和 Vercel 一样成为推荐的部署网关平台。
:::

### 6. 直接导出 Provider
Expand Down
24 changes: 17 additions & 7 deletions docs/guide/advance/api-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ sidebarDepth: 1

## 简述实现

目前 Surgio 支持两个部署平台,阿里云函数服务和 zeit 的 now.sh。他们各自有各自的优缺点,由各位定夺使用谁(无法同时使用)。
目前 Surgio 支持两个部署平台,阿里云函数服务和 Vercel。他们各自有各自的优缺点,由各位定夺使用谁(无法同时使用)。

### now.sh <Badge text="推荐" vertical="middle" />
### Vercel <Badge text="推荐" vertical="middle" />

优点:

Expand All @@ -42,11 +42,11 @@ sidebarDepth: 1

- 管理复杂(文档很多很杂)

## 部署 - now.sh <Badge text="推荐" vertical="middle" />
## 部署 - Vercel <Badge text="推荐" vertical="middle" />

### 准备

1. 注册一个 [now.sh](https://now.sh) 账号
1. 注册一个 [Vercel](https://vercel.com) 账号
2. 可以不绑定付款方式

### 配置
Expand Down Expand Up @@ -171,9 +171,19 @@ $ now --prod

有几点需要大家注意的:

1. 每一次更新本地的代码,都需要执行一次 `now`,保证远端和本地代码一致
2. 访问日志、监控、域名绑定等复杂功能恕不提供教程
3. 如果访问地址泄漏,请立即删除云函数然后修改机场密码
1. 每一次更新本地的代码,都需要执行一次 `now`,保证远端和本地代码一致;
2. 访问日志、监控、域名绑定等复杂功能恕不提供教程;
3. 如果访问地址泄漏,请立即删除云函数然后修改机场密码;
4. 由于免费用户单次请求的超时时间为 10s,所以不建议使用过多的远程片段、较高的超时时间和重试机制。你可以在 `now.json` 中加入以下的环境变量缓解遇到 Vercel 超时报错的概率:

```json
{
"env": {
"NETWORK_RETRY": 0,
"NETWORK_TIMEOUT": "5000"
}
}
```

## 部署 - 阿里云函数 <Badge text="即将废弃" vertical="middle" type="error" />

Expand Down
6 changes: 6 additions & 0 deletions docs/guide/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ sidebarDepth: 2

- 默认值: `20000`

### `SURGIO_NETWORK_RETRY`

- 默认值: `2`

最大重试次数为 2 时,加上原始的请求最多会请求 3 次。

### `SURGIO_NETWORK_CONCURRENCY`

- 默认值: `5`
Expand Down
3 changes: 2 additions & 1 deletion lib/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { lowercaseHeaderKeys } from '../utils';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

type SupportConfigTypes = ShadowsocksNodeConfig|VmessNodeConfig|HttpsNodeConfig|HttpNodeConfig|ShadowsocksrNodeConfig|SnellNodeConfig|TrojanNodeConfig;
Expand Down Expand Up @@ -98,6 +98,7 @@ export const getClashSubscription = async (
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
responseType: 'text',
retry: NETWORK_RETRY,
});
const subsciptionCacheItem: SubsciptionCacheItem = {
body: res.body,
Expand Down
3 changes: 2 additions & 1 deletion lib/provider/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { decodeStringList, fromBase64, fromUrlSafeBase64 } from '../utils';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

const logger = createLogger({
Expand Down Expand Up @@ -97,6 +97,7 @@ export const getShadowsocksSubscription = async (
async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
responseType: 'text',
});
const subsciptionCacheItem: SubsciptionCacheItem = {
Expand Down
3 changes: 2 additions & 1 deletion lib/provider/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ShadowsocksrNodeConfig, ShadowsocksrSubscribeProviderConfig, Subscripti
import { fromBase64 } from '../utils';
import { parseSubscriptionNode, parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { parseSSRUri } from '../utils/ssr';
import Provider from './Provider';

Expand Down Expand Up @@ -85,6 +85,7 @@ export const getShadowsocksrSubscription = async (
async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
responseType: 'text',
});
const subsciptionCacheItem: SubsciptionCacheItem = {
Expand Down
3 changes: 2 additions & 1 deletion lib/provider/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import got from 'got';
import { NodeTypeEnum, V2rayNSubscribeProviderConfig, VmessNodeConfig } from '../types';
import { fromBase64 } from '../utils';
import { ConfigCache } from '../utils/cache';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

export default class V2rayNSubscribeProvider extends Provider {
Expand Down Expand Up @@ -74,6 +74,7 @@ export const getV2rayNSubscription = async (
const response = ConfigCache.has(url) ? ConfigCache.get(url) as string : await (async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
});

ConfigCache.set(url, res.body);
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const NETWORK_TIMEOUT = process.env.SURGIO_NETWORK_TIMEOUT ? Number(proce

export const NETWORK_CONCURRENCY = process.env.SURGIO_NETWORK_CONCURRENCY ? Number(process.env.SURGIO_NETWORK_CONCURRENCY) : 5;

export const NETWORK_RETRY = process.env.SURGIO_NETWORK_RETRY ? Number(process.env.SURGIO_NETWORK_RETRY) : 2;

export const OBFS_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1';

export const PROXY_TEST_URL = 'http://cp.cloudflare.com/generate_204';
Expand Down
11 changes: 10 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ import {
VmessNodeConfig,
} from '../types';
import { ConfigCache } from './cache';
import { ERR_INVALID_FILTER, NETWORK_TIMEOUT, OBFS_UA, PROXY_TEST_INTERVAL, PROXY_TEST_URL } from './constant';
import {
ERR_INVALID_FILTER,
NETWORK_RETRY,
NETWORK_TIMEOUT,
OBFS_UA,
PROXY_TEST_INTERVAL,
PROXY_TEST_URL,
} from './constant';
import { validateFilter } from './filter';
import { formatVmessUri } from './v2ray';

Expand Down Expand Up @@ -75,6 +82,7 @@ export const getBlackSSLConfig = async (username: string, password: string): Pro
password,
},
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
headers: {
'User-Agent': 'GoAgentX/774 CFNetwork/901.1 Darwin/17.6.0 (x86_64)',
},
Expand Down Expand Up @@ -108,6 +116,7 @@ export const getShadowsocksJSONConfig = async (
const response = ConfigCache.has(url) ? JSON.parse(ConfigCache.get(url) as string) : await (async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
});

ConfigCache.set(url, res.body);
Expand Down

0 comments on commit 34ecfec

Please sign in to comment.