Skip to content

Commit

Permalink
*: support proxy-protocol (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Sep 30, 2022
1 parent 0ef11e1 commit 3b6c664
Show file tree
Hide file tree
Showing 28 changed files with 402 additions and 234 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Please answer these questions before submitting your issue. Thanks!

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

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

2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Notable changes

- [ ] Has configuration change
- [ ] 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))
- [ ] Has weirctl change
- [ ] Has tiproxyctl change
- [ ] Other user behavior changes

### Release note
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ ifneq ($(RELEASE), "")
endif
BUILD_TAGS ?=
LDFLAGS ?=
DEBUG ?=
BUILDFLAGS := $(BUILDFLAGS) -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}'
ifeq ("$(WITH_RACE)", "1")
BUILDFLAGS = $(BUILDFLAGS) -race
BUILDFLAGS += -race
endif
IMAGE_TAG ?= latest
EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*))
Expand Down
2 changes: 1 addition & 1 deletion conf/namespace/example.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace: "example"
namespace: "default"
frontend:
security:
backend:
Expand Down
1 change: 1 addition & 0 deletions conf/proxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ proxy:
tcp-keep-alive: true
max-connections: 1000
pd-addrs: "127.0.0.1:2379"
# proxy-protocol: "v2"
metrics:
api:
addr: "0.0.0.0:3080"
Expand Down
19 changes: 15 additions & 4 deletions lib/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import (
"os"
"path/filepath"

"github.com/pingcap/TiProxy/lib/util/errors"
"gopkg.in/yaml.v3"
)

var (
ErrUnsupportedProxyProtocolVersion = errors.New("unsupported proxy protocol version")
)

type Config struct {
Proxy ProxyServer `yaml:"proxy,omitempty" toml:"proxy,omitempty" json:"proxy,omitempty"`
API API `yaml:"api,omitempty" toml:"api,omitempty" json:"api,omitempty"`
Expand All @@ -40,13 +45,13 @@ type Metrics struct {
type ProxyServerOnline struct {
MaxConnections uint64 `yaml:"max-connections,omitempty" toml:"max-connections,omitempty" json:"max-connections,omitempty"`
TCPKeepAlive bool `yaml:"tcp-keep-alive,omitempty" toml:"tcp-keep-alive,omitempty" json:"tcp-keep-alive,omitempty"`
ProxyProtocol string `yaml:"proxy-protocol,omitempty" toml:"proxy-protocol,omitempty" json:"proxy-protocol,omitempty"`
}

type ProxyServer struct {
Addr string `yaml:"addr,omitempty" toml:"addr,omitempty" json:"addr,omitempty"`
PDAddrs string `yaml:"pd-addrs,omitempty" toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
ProxyProtocol string `yaml:"proxy-protocol,omitempty" toml:"proxy-protocol,omitempty" json:"proxy-protocol,omitempty"`
ProxyServerOnline
Addr string `yaml:"addr,omitempty" toml:"addr,omitempty" json:"addr,omitempty"`
PDAddrs string `yaml:"pd-addrs,omitempty" toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
ProxyServerOnline `yaml:",inline" toml:",inline" json:",inline"`
}

type API struct {
Expand Down Expand Up @@ -123,6 +128,12 @@ func (cfg *Config) Check() error {
}
cfg.Workdir = filepath.Clean(d)
}
switch cfg.Proxy.ProxyProtocol {
case "v2":
case "":
default:
return errors.Wrapf(ErrUnsupportedProxyProtocolVersion, "%s", cfg.Proxy.ProxyProtocol)
}
return nil
}

Expand Down
38 changes: 38 additions & 0 deletions lib/config/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -33,6 +35,7 @@ var testProxyConfig = Config{
ProxyServerOnline: ProxyServerOnline{
MaxConnections: 1,
TCPKeepAlive: true,
ProxyProtocol: "v2",
},
},
API: API{
Expand Down Expand Up @@ -95,3 +98,38 @@ func TestProxyConfig(t *testing.T) {
require.NoError(t, err)
require.Equal(t, data1, data2)
}

func TestProxyCheck(t *testing.T) {
testcases := []struct {
pre func(*testing.T, *Config)
post func(*testing.T, *Config)
err error
}{
{
pre: func(t *testing.T, c *Config) {
c.Workdir = ""
},
post: func(t *testing.T, c *Config) {
cwd, err := os.Getwd()
require.NoError(t, err)
require.Equal(t, filepath.Clean(cwd), c.Workdir)
},
},
{
pre: func(t *testing.T, c *Config) {
c.Proxy.ProxyProtocol = "v1"
},
err: ErrUnsupportedProxyProtocolVersion,
},
}
for _, tc := range testcases {
cfg := testProxyConfig
tc.pre(t, &cfg)
if tc.err != nil {
require.ErrorIs(t, cfg.Check(), tc.err)
continue
}
require.NoError(t, cfg.Check())
tc.post(t, &cfg)
}
}
2 changes: 1 addition & 1 deletion pkg/manager/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewScoreBasedRouter(logger *zap.Logger, cfg *config.BackendNamespace, clien
router.wg.Run(func() {
router.rebalanceLoop(childCtx)
})
return router, err
return router, nil
}

// Route implements Router.Route interface.
Expand Down
Loading

0 comments on commit 3b6c664

Please sign in to comment.