Skip to content

Commit

Permalink
Make capture ID accessible through context (pingcap#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku authored Dec 3, 2019
1 parent 10137f3 commit bb5d4cb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cdc/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package cdc

import (
"context"
"net/http"
"strings"

"github.com/pingcap/ticdc/pkg/util"

"github.com/pingcap/log"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -78,6 +93,7 @@ func NewServer(opt ...ServerOption) (*Server, error) {
// Run runs the server.
func (s *Server) Run(ctx context.Context) error {
s.startStatusHTTP()
ctx = util.PutCaptureIDInCtx(ctx, s.capture.info.ID)
return s.capture.Start(ctx)
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/util/ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import "context"

type ctxKey string

const (
ctxKeyCaptureID = ctxKey("captureID")
)

// CaptureIDFromCtx returns a capture ID stored in the specified context.
// It returns an empty string if there's no valid capture ID found.
func CaptureIDFromCtx(ctx context.Context) string {
captureID, ok := ctx.Value(ctxKeyCaptureID).(string)
if !ok {
return ""
}
return captureID
}

// PutCaptureIDInCtx returns a new child context with the specified capture ID stored.
func PutCaptureIDInCtx(ctx context.Context, captureID string) context.Context {
return context.WithValue(ctx, ctxKeyCaptureID, captureID)
}
35 changes: 35 additions & 0 deletions pkg/util/ctx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"context"

"github.com/pingcap/check"
)

type captureIDSuite struct{}

var _ = check.Suite(&captureIDSuite{})

func (s *captureIDSuite) TestShouldReturnCaptureID(c *check.C) {
ctx := PutCaptureIDInCtx(context.Background(), "ello")
c.Assert(CaptureIDFromCtx(ctx), check.Equals, "ello")
}

func (s *captureIDSuite) TestShouldReturnEmptyStr(c *check.C) {
c.Assert(CaptureIDFromCtx(context.Background()), check.Equals, "")
ctx := context.WithValue(context.Background(), ctxKeyCaptureID, 1321)
c.Assert(CaptureIDFromCtx(ctx), check.Equals, "")
}

0 comments on commit bb5d4cb

Please sign in to comment.