Skip to content

Commit

Permalink
Perfom less reload when servers change
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Gilles committed Aug 26, 2019
1 parent 1cecb04 commit 1054b75
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 126 deletions.
88 changes: 77 additions & 11 deletions haproxy/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@ type dataplaneClient struct {
type tnx struct {
txID string
client *dataplaneClient

after []func() error
}

func (c *dataplaneClient) Tnx() (*tnx, error) {
func (c *dataplaneClient) Tnx() *tnx {
return &tnx{
client: c,
}
}

func (t *tnx) ensureTnx() error {
if t.txID != "" {
return nil
}
res := models.Transaction{}
err := c.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/transactions?version=%d", c.version), nil, &res)
err := t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/transactions?version=%d", t.client.version), nil, &res)
if err != nil {
return nil, err
return err
}

return &tnx{
txID: res.ID,
client: c,
}, nil
t.txID = res.ID

return nil
}

func (c *dataplaneClient) Info() (*models.ProcessInfo, error) {
Expand All @@ -64,57 +74,113 @@ func (c *dataplaneClient) Stats() (models.NativeStats, error) {
}

func (t *tnx) Commit() error {
err := t.client.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/transactions/%s", t.txID), nil, nil)
if err != nil {
return err
if t.txID != "" {
err := t.client.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/transactions/%s", t.txID), nil, nil)
if err != nil {
return err
}

t.client.version++
}

t.client.version++
for _, f := range t.after {
err := f()
if err != nil {
return err
}
}

return nil
}

func (t *tnx) After(fn func() error) {
t.after = append(t.after, fn)
}

func (t *tnx) CreateFrontend(fe models.Frontend) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/frontends?transaction_id=%s", t.txID), fe, nil)
}

func (t *tnx) DeleteFrontend(name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/frontends/%s?transaction_id=%s", name, t.txID), nil, nil)
}

func (t *tnx) CreateBind(feName string, bind models.Bind) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/binds?frontend=%s&transaction_id=%s", feName, t.txID), bind, nil)
}

func (t *tnx) DeleteBackend(name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/backends/%s?transaction_id=%s", name, t.txID), nil, nil)
}

func (t *tnx) CreateBackend(be models.Backend) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/backends?transaction_id=%s", t.txID), be, nil)
}

func (t *tnx) CreateServer(beName string, srv models.Server) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/servers?backend=%s&transaction_id=%s", beName, t.txID), srv, nil)
}

func (t *tnx) ReplaceServer(beName string, srv models.Server) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&transaction_id=%s", srv.Name, beName, t.txID), srv, nil)
}

func (c *dataplaneClient) ReplaceServer(beName string, srv models.Server) error {
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, c.version), srv, nil)
if err != nil {
return err
}

c.version++
return nil
}

func (t *tnx) DeleteServer(beName string, name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&transaction_id=%s", name, beName, t.txID), nil, nil)
}

func (t *tnx) CreateFilter(parentType, parentName string, filter models.Filter) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/filters?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), filter, nil)
}

func (t *tnx) CreateTCPRequestRule(parentType, parentName string, rule models.TCPRequestRule) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/tcp_request_rules?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
}

func (t *tnx) CreateLogTargets(parentType, parentName string, rule models.LogTarget) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
}

Expand Down
23 changes: 10 additions & 13 deletions haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ type HAProxy struct {
cfgC chan consul.Config
currentCfg *consul.Config

upstreamServerSlots map[string][]upstreamSlot

haConfig *haConfig
}

func New(consulClient *api.Client, cfg chan consul.Config, opts Options) *HAProxy {
return &HAProxy{
opts: opts,
consulClient: consulClient,
cfgC: cfg,
opts: opts,
consulClient: consulClient,
cfgC: cfg,
upstreamServerSlots: make(map[string][]upstreamSlot),
}
}

Expand Down Expand Up @@ -107,13 +110,10 @@ func (h *HAProxy) Run(sd *lib.Shutdown) error {
}

func (h *HAProxy) init() error {
tx, err := h.dataplaneClient.Tnx()
if err != nil {
return err
}
tx := h.dataplaneClient.Tnx()

timeout := int64(30000)
err = tx.CreateBackend(models.Backend{
err := tx.CreateBackend(models.Backend{
Name: "spoe_back",
ServerTimeout: &timeout,
ConnectTimeout: &timeout,
Expand All @@ -137,12 +137,9 @@ func (h *HAProxy) init() error {
}

func (h *HAProxy) handleChange(cfg consul.Config) error {
tx, err := h.dataplaneClient.Tnx()
if err != nil {
return err
}
tx := h.dataplaneClient.Tnx()

err = h.handleDownstream(tx, cfg.Downstream)
err := h.handleDownstream(tx, cfg.Downstream)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1054b75

Please sign in to comment.