Skip to content

Commit 2a81096

Browse files
committed
*: support proxy-protocol (pingcap#96)
1 parent f01a5e3 commit 2a81096

28 files changed

+402
-234
lines changed

.github/ISSUE_TEMPLATE/bug-report.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Please answer these questions before submitting your issue. Thanks!
1818

1919
### 4. What is your version? (Required)
2020

21-
<!-- Paste the output of weirproxy -V -->
22-
<!-- Paste the output of weirctl -V if related -->
21+
<!-- Paste the output of tiproxy version -->
22+
<!-- Paste the output of tiproxyctl version if related -->
2323
<!-- Paste the output of SELECT tidb_version() if related -->
2424

.github/pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Notable changes
3939

4040
- [ ] Has configuration change
4141
- [ ] Has HTTP API interfaces change (Don't forget to [add the declarative for API](https://github.com/tikv/pd/blob/master/docs/development.md#updating-api-documentation))
42-
- [ ] Has weirctl change
42+
- [ ] Has tiproxyctl change
4343
- [ ] Other user behavior changes
4444

4545
### Release note

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ ifneq ($(RELEASE), "")
2222
endif
2323
BUILD_TAGS ?=
2424
LDFLAGS ?=
25+
DEBUG ?=
2526
BUILDFLAGS := $(BUILDFLAGS) -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}'
2627
ifeq ("$(WITH_RACE)", "1")
27-
BUILDFLAGS = $(BUILDFLAGS) -race
28+
BUILDFLAGS += -race
2829
endif
2930
IMAGE_TAG ?= latest
3031
EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*))

conf/namespace/example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace: "example"
1+
namespace: "default"
22
frontend:
33
security:
44
backend:

conf/proxy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ proxy:
44
tcp-keep-alive: true
55
max-connections: 1000
66
pd-addrs: "127.0.0.1:2379"
7+
# proxy-protocol: "v2"
78
metrics:
89
api:
910
addr: "0.0.0.0:3080"

lib/config/proxy.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ import (
1919
"os"
2020
"path/filepath"
2121

22+
"github.com/pingcap/TiProxy/lib/util/errors"
2223
"gopkg.in/yaml.v3"
2324
)
2425

26+
var (
27+
ErrUnsupportedProxyProtocolVersion = errors.New("unsupported proxy protocol version")
28+
)
29+
2530
type Config struct {
2631
Proxy ProxyServer `yaml:"proxy,omitempty" toml:"proxy,omitempty" json:"proxy,omitempty"`
2732
API API `yaml:"api,omitempty" toml:"api,omitempty" json:"api,omitempty"`
@@ -40,13 +45,13 @@ type Metrics struct {
4045
type ProxyServerOnline struct {
4146
MaxConnections uint64 `yaml:"max-connections,omitempty" toml:"max-connections,omitempty" json:"max-connections,omitempty"`
4247
TCPKeepAlive bool `yaml:"tcp-keep-alive,omitempty" toml:"tcp-keep-alive,omitempty" json:"tcp-keep-alive,omitempty"`
48+
ProxyProtocol string `yaml:"proxy-protocol,omitempty" toml:"proxy-protocol,omitempty" json:"proxy-protocol,omitempty"`
4349
}
4450

4551
type ProxyServer struct {
46-
Addr string `yaml:"addr,omitempty" toml:"addr,omitempty" json:"addr,omitempty"`
47-
PDAddrs string `yaml:"pd-addrs,omitempty" toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
48-
ProxyProtocol string `yaml:"proxy-protocol,omitempty" toml:"proxy-protocol,omitempty" json:"proxy-protocol,omitempty"`
49-
ProxyServerOnline
52+
Addr string `yaml:"addr,omitempty" toml:"addr,omitempty" json:"addr,omitempty"`
53+
PDAddrs string `yaml:"pd-addrs,omitempty" toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
54+
ProxyServerOnline `yaml:",inline" toml:",inline" json:",inline"`
5055
}
5156

5257
type API struct {
@@ -123,6 +128,12 @@ func (cfg *Config) Check() error {
123128
}
124129
cfg.Workdir = filepath.Clean(d)
125130
}
131+
switch cfg.Proxy.ProxyProtocol {
132+
case "v2":
133+
case "":
134+
default:
135+
return errors.Wrapf(ErrUnsupportedProxyProtocolVersion, "%s", cfg.Proxy.ProxyProtocol)
136+
}
126137
return nil
127138
}
128139

lib/config/proxy_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package config
1616

1717
import (
18+
"os"
19+
"path/filepath"
1820
"testing"
1921

2022
"github.com/stretchr/testify/require"
@@ -33,6 +35,7 @@ var testProxyConfig = Config{
3335
ProxyServerOnline: ProxyServerOnline{
3436
MaxConnections: 1,
3537
TCPKeepAlive: true,
38+
ProxyProtocol: "v2",
3639
},
3740
},
3841
API: API{
@@ -95,3 +98,38 @@ func TestProxyConfig(t *testing.T) {
9598
require.NoError(t, err)
9699
require.Equal(t, data1, data2)
97100
}
101+
102+
func TestProxyCheck(t *testing.T) {
103+
testcases := []struct {
104+
pre func(*testing.T, *Config)
105+
post func(*testing.T, *Config)
106+
err error
107+
}{
108+
{
109+
pre: func(t *testing.T, c *Config) {
110+
c.Workdir = ""
111+
},
112+
post: func(t *testing.T, c *Config) {
113+
cwd, err := os.Getwd()
114+
require.NoError(t, err)
115+
require.Equal(t, filepath.Clean(cwd), c.Workdir)
116+
},
117+
},
118+
{
119+
pre: func(t *testing.T, c *Config) {
120+
c.Proxy.ProxyProtocol = "v1"
121+
},
122+
err: ErrUnsupportedProxyProtocolVersion,
123+
},
124+
}
125+
for _, tc := range testcases {
126+
cfg := testProxyConfig
127+
tc.pre(t, &cfg)
128+
if tc.err != nil {
129+
require.ErrorIs(t, cfg.Check(), tc.err)
130+
continue
131+
}
132+
require.NoError(t, cfg.Check())
133+
tc.post(t, &cfg)
134+
}
135+
}

pkg/manager/router/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func NewScoreBasedRouter(logger *zap.Logger, cfg *config.BackendNamespace, clien
134134
router.wg.Run(func() {
135135
router.rebalanceLoop(childCtx)
136136
})
137-
return router, err
137+
return router, nil
138138
}
139139

140140
// Route implements Router.Route interface.

0 commit comments

Comments
 (0)