1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-04-19 01:12:23 -04:00

normalized env variable names for bash

This commit is contained in:
Darien Raymond 2017-05-26 01:11:38 +02:00
parent c5761919da
commit b16a82024c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 96 additions and 19 deletions

View File

@ -1,10 +1,10 @@
package buf package buf
import ( import (
"os"
"runtime" "runtime"
"strconv"
"sync" "sync"
"v2ray.com/core/common/platform"
) )
// Pool provides functionality to generate and recycle buffers on demand. // Pool provides functionality to generate and recycle buffers on demand.
@ -99,7 +99,7 @@ var (
mediumPool Pool mediumPool Pool
) )
func getDefaultPoolSize() uint32 { func getDefaultPoolSize() int {
switch runtime.GOARCH { switch runtime.GOARCH {
case "amd64", "386": case "amd64", "386":
return 20 return 20
@ -109,14 +109,11 @@ func getDefaultPoolSize() uint32 {
} }
func init() { func init() {
size := getDefaultPoolSize() f := platform.EnvFlag{
sizeStr := os.Getenv(poolSizeEnvKey) Name: poolSizeEnvKey,
if len(sizeStr) > 0 { AltName: platform.NormalizeEnvName(poolSizeEnvKey),
customSize, err := strconv.ParseUint(sizeStr, 10, 32)
if err == nil {
size = uint32(customSize)
}
} }
size := f.GetValueAsInt(getDefaultPoolSize())
if size > 0 { if size > 0 {
totalByteSize := size * 1024 * 1024 totalByteSize := size * 1024 * 1024
mediumPool = NewBufferPool(Size, totalByteSize/Size) mediumPool = NewBufferPool(Size, totalByteSize/Size)

View File

@ -0,0 +1,42 @@
package platform
import (
"os"
"strconv"
"strings"
)
type EnvFlag struct {
Name string
AltName string
}
func (f EnvFlag) GetValue(defaultValue string) string {
if v, found := os.LookupEnv(f.Name); found {
return v
}
if len(f.AltName) > 0 {
if v, found := os.LookupEnv(f.AltName); found {
return v
}
}
return defaultValue
}
func (f EnvFlag) GetValueAsInt(defaultValue int) int {
const PlaceHolder = "xxxxxx"
s := f.GetValue(PlaceHolder)
if s == PlaceHolder {
return defaultValue
}
v, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return defaultValue
}
return int(v)
}
func NormalizeEnvName(name string) string {
return strings.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1)
}

View File

@ -0,0 +1,41 @@
package platform_test
import (
"testing"
. "v2ray.com/core/common/platform"
"v2ray.com/core/testing/assert"
)
func TestNormalizeEnvName(t *testing.T) {
assert := assert.On(t)
cases := []struct {
input string
output string
}{
{
input: "a",
output: "A",
},
{
input: "a.a",
output: "A_A",
},
{
input: "A.A.B",
output: "A_A_B",
},
}
for _, test := range cases {
assert.String(NormalizeEnvName(test.input)).Equals(test.output)
}
}
func TestEnvFlag(t *testing.T) {
assert := assert.On(t)
assert.Int(EnvFlag{
Name: "xxxxx.y",
}.GetValueAsInt(10)).Equals(10)
}

View File

@ -3,12 +3,11 @@ package ray
import ( import (
"context" "context"
"io" "io"
"os"
"strconv"
"sync" "sync"
"time" "time"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/platform"
) )
// NewRay creates a new Ray for direct traffic transport. // NewRay creates a new Ray for direct traffic transport.
@ -44,13 +43,11 @@ var streamSizeLimit uint64 = 10 * 1024 * 1024
func init() { func init() {
const raySizeEnvKey = "v2ray.ray.buffer.size" const raySizeEnvKey = "v2ray.ray.buffer.size"
sizeStr := os.Getenv(raySizeEnvKey) size := platform.EnvFlag{
if len(sizeStr) > 0 { Name: raySizeEnvKey,
customSize, err := strconv.ParseUint(sizeStr, 10, 32) AltName: platform.NormalizeEnvName(raySizeEnvKey),
if err == nil { }.GetValueAsInt(10)
streamSizeLimit = customSize * 1024 * 1024 streamSizeLimit = uint64(size) * 1024 * 1024
}
}
} }
type Stream struct { type Stream struct {