Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: support proxy-protocol #96

Merged
merged 15 commits into from
Sep 30, 2022
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/go-mysql-org/go-mysql v1.6.0
github.com/pingcap/TiProxy/lib v0.0.0-00010101000000-000000000000
github.com/pingcap/log v1.1.0
github.com/pingcap/tidb v1.1.0-beta.0.20220908042057-08b1faf2ad1e
github.com/pingcap/tidb/parser v0.0.0-20220908042057-08b1faf2ad1e
github.com/prometheus/client_golang v1.13.0
Expand All @@ -22,6 +23,7 @@ require (
go.uber.org/zap v1.23.0
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0
google.golang.org/grpc v1.49.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

require (
Expand Down Expand Up @@ -71,7 +73,6 @@ require (
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c // indirect
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 // indirect
github.com/pingcap/kvproto v0.0.0-20220906053631-2e37953b2b43 // indirect
github.com/pingcap/log v1.1.0 // indirect
github.com/pingcap/tipb v0.0.0-20220824081009-0714a57aff1d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
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 @@ -119,6 +124,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 @@ -93,3 +96,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)
}
}
1 change: 1 addition & 0 deletions pkg/manager/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (e *ConfigManager) watchCfgProxy(ctx context.Context, cfg *config.Config) e
if err := e.SetProxyConfig(ctx, &config.ProxyServerOnline{
MaxConnections: cfg.Proxy.MaxConnections,
TCPKeepAlive: cfg.Proxy.TCPKeepAlive,
ProxyProtocol: cfg.Proxy.ProxyProtocol,
}); err != nil {
return err
}
Expand Down
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