github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/context/context.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Copyright 2019 PingCAP, Inc.
    15  //
    16  // Licensed under the Apache License, Version 2.0 (the "License");
    17  // you may not use this file except in compliance with the License.
    18  // You may obtain a copy of the License at
    19  //
    20  //     http://www.apache.org/licenses/LICENSE-2.0
    21  //
    22  // Unless required by applicable law or agreed to in writing, software
    23  // distributed under the License is distributed on an "AS IS" BASIS,
    24  // See the License for the specific language governing permissions and
    25  // limitations under the License.
    26  
    27  package context
    28  
    29  import (
    30  	"context"
    31  	"time"
    32  
    33  	"github.com/pingcap/tiflow/engine/pkg/deps"
    34  	"github.com/pingcap/tiflow/engine/pkg/p2p"
    35  	"github.com/pingcap/tiflow/engine/pkg/tenant"
    36  )
    37  
    38  // Context is used to in dm to record some context field like
    39  // * go context
    40  type Context struct {
    41  	context.Context
    42  	Environ     Environment
    43  	ProjectInfo tenant.ProjectInfo
    44  
    45  	deps *deps.Deps
    46  }
    47  
    48  // Background return a nop context.
    49  func Background() *Context {
    50  	return &Context{
    51  		Context: context.Background(),
    52  	}
    53  }
    54  
    55  // NewContext return a new Context.
    56  func NewContext(ctx context.Context) *Context {
    57  	return &Context{
    58  		Context: ctx,
    59  	}
    60  }
    61  
    62  // WithContext set go context.
    63  func (c *Context) WithContext(ctx context.Context) *Context {
    64  	return &Context{
    65  		Context: ctx,
    66  	}
    67  }
    68  
    69  // WithTimeout sets a timeout associated context.
    70  func (c *Context) WithTimeout(timeout time.Duration) (*Context, context.CancelFunc) {
    71  	ctx, cancel := context.WithTimeout(c, timeout)
    72  	return &Context{
    73  		Context: ctx,
    74  	}, cancel
    75  }
    76  
    77  // WithDeps puts a built dependency container into the context.
    78  func (c *Context) WithDeps(deps *deps.Deps) *Context {
    79  	return &Context{
    80  		Context:     c.Context,
    81  		Environ:     c.Environ,
    82  		ProjectInfo: c.ProjectInfo,
    83  
    84  		deps: deps,
    85  	}
    86  }
    87  
    88  // Deps returns a handle used for dependency injection.
    89  func (c *Context) Deps() *deps.Deps {
    90  	return c.deps
    91  }
    92  
    93  // Environment contains some configuration related environ values
    94  type Environment struct {
    95  	NodeID          p2p.NodeID
    96  	Addr            string
    97  	MasterMetaBytes []byte
    98  }