default.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  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. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package context
  15. import (
  16. "context"
  17. "time"
  18. "github.com/sirupsen/logrus"
  19. "github.com/lf-edge/ekuiper/sdk/go/api"
  20. )
  21. const LoggerKey = "$$logger"
  22. type DefaultContext struct {
  23. ruleId string
  24. opId string
  25. instanceId int
  26. ctx context.Context
  27. // Only initialized after withMeta set
  28. logger api.Logger
  29. }
  30. func Background() *DefaultContext {
  31. c := &DefaultContext{
  32. ctx: context.Background(),
  33. }
  34. return c
  35. }
  36. func WithValue(parent *DefaultContext, key, val interface{}) *DefaultContext {
  37. parent.ctx = context.WithValue(parent.ctx, key, val)
  38. return parent
  39. }
  40. // Implement context interface
  41. func (c *DefaultContext) Deadline() (deadline time.Time, ok bool) {
  42. return c.ctx.Deadline()
  43. }
  44. func (c *DefaultContext) Done() <-chan struct{} {
  45. return c.ctx.Done()
  46. }
  47. func (c *DefaultContext) Err() error {
  48. return c.ctx.Err()
  49. }
  50. func (c *DefaultContext) Value(key interface{}) interface{} {
  51. return c.ctx.Value(key)
  52. }
  53. // Stream metas
  54. func (c *DefaultContext) GetContext() context.Context {
  55. return c.ctx
  56. }
  57. func (c *DefaultContext) GetLogger() api.Logger {
  58. l, ok := c.ctx.Value(LoggerKey).(*logrus.Entry)
  59. if l != nil && ok {
  60. return l
  61. }
  62. return LogEntry("rule", c.ruleId)
  63. }
  64. func (c *DefaultContext) GetRuleId() string {
  65. return c.ruleId
  66. }
  67. func (c *DefaultContext) GetOpId() string {
  68. return c.opId
  69. }
  70. func (c *DefaultContext) GetInstanceId() int {
  71. return c.instanceId
  72. }
  73. func (c *DefaultContext) WithMeta(ruleId string, opId string) api.StreamContext {
  74. return &DefaultContext{
  75. ruleId: ruleId,
  76. opId: opId,
  77. instanceId: 0,
  78. ctx: c.ctx,
  79. }
  80. }
  81. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  82. return &DefaultContext{
  83. instanceId: instanceId,
  84. ruleId: c.ruleId,
  85. opId: c.opId,
  86. ctx: c.ctx,
  87. }
  88. }
  89. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  90. ctx, cancel := context.WithCancel(c.ctx)
  91. return &DefaultContext{
  92. ruleId: c.ruleId,
  93. opId: c.opId,
  94. instanceId: c.instanceId,
  95. ctx: ctx,
  96. }, cancel
  97. }