You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.go 4.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package tm
  18. import (
  19. "context"
  20. "github.com/seata/seata-go/pkg/protocol/message"
  21. "github.com/seata/seata-go/pkg/rm/tcc/fence/enum"
  22. )
  23. type ContextParam string
  24. const (
  25. seataContextVariable = ContextParam("seataContextVariable")
  26. )
  27. type BusinessActionContext struct {
  28. Xid string
  29. BranchId int64
  30. ActionName string
  31. IsDelayReport bool
  32. IsUpdated bool
  33. ActionContext map[string]interface{}
  34. }
  35. type ContextVariable struct {
  36. TxName string
  37. Xid string
  38. XidCopy string
  39. FencePhase enum.FencePhase
  40. TxRole *GlobalTransactionRole
  41. BusinessActionContext *BusinessActionContext
  42. TxStatus *message.GlobalStatus
  43. }
  44. func InitSeataContext(ctx context.Context) context.Context {
  45. return context.WithValue(ctx, seataContextVariable, &ContextVariable{})
  46. }
  47. func GetTxStatus(ctx context.Context) *message.GlobalStatus {
  48. variable := ctx.Value(seataContextVariable)
  49. if variable == nil {
  50. return nil
  51. }
  52. return variable.(*ContextVariable).TxStatus
  53. }
  54. func SetTxStatus(ctx context.Context, status message.GlobalStatus) {
  55. variable := ctx.Value(seataContextVariable)
  56. if variable != nil {
  57. variable.(*ContextVariable).TxStatus = &status
  58. }
  59. }
  60. func GetTxName(ctx context.Context) string {
  61. variable := ctx.Value(seataContextVariable)
  62. if variable == nil {
  63. return ""
  64. }
  65. return variable.(*ContextVariable).TxName
  66. }
  67. func SetTxName(ctx context.Context, name string) {
  68. variable := ctx.Value(seataContextVariable)
  69. if variable != nil {
  70. variable.(*ContextVariable).TxName = name
  71. }
  72. }
  73. func IsSeataContext(ctx context.Context) bool {
  74. return ctx.Value(seataContextVariable) != nil
  75. }
  76. func GetBusinessActionContext(ctx context.Context) *BusinessActionContext {
  77. variable := ctx.Value(seataContextVariable)
  78. if variable == nil {
  79. return nil
  80. }
  81. return variable.(*ContextVariable).BusinessActionContext
  82. }
  83. func SetBusinessActionContext(ctx context.Context, businessActionContext *BusinessActionContext) {
  84. variable := ctx.Value(seataContextVariable)
  85. if variable != nil {
  86. variable.(*ContextVariable).BusinessActionContext = businessActionContext
  87. }
  88. }
  89. func GetTransactionRole(ctx context.Context) *GlobalTransactionRole {
  90. variable := ctx.Value(seataContextVariable)
  91. if variable == nil {
  92. return nil
  93. }
  94. return variable.(*ContextVariable).TxRole
  95. }
  96. func SetTransactionRole(ctx context.Context, role GlobalTransactionRole) {
  97. variable := ctx.Value(seataContextVariable)
  98. if variable != nil {
  99. variable.(*ContextVariable).TxRole = &role
  100. }
  101. }
  102. func IsTransactionOpened(ctx context.Context) bool {
  103. variable := ctx.Value(seataContextVariable)
  104. if variable == nil {
  105. return false
  106. }
  107. xid := variable.(*ContextVariable).Xid
  108. return xid != ""
  109. }
  110. func GetXID(ctx context.Context) string {
  111. variable := ctx.Value(seataContextVariable)
  112. if variable == nil {
  113. return ""
  114. }
  115. xid := variable.(*ContextVariable).Xid
  116. if xid == "" {
  117. xid = variable.(*ContextVariable).XidCopy
  118. }
  119. return xid
  120. }
  121. func SetXID(ctx context.Context, xid string) {
  122. variable := ctx.Value(seataContextVariable)
  123. if variable != nil {
  124. variable.(*ContextVariable).Xid = xid
  125. }
  126. }
  127. func SetXIDCopy(ctx context.Context, xid string) {
  128. variable := ctx.Value(seataContextVariable)
  129. if variable != nil {
  130. variable.(*ContextVariable).XidCopy = xid
  131. }
  132. }
  133. func UnbindXid(ctx context.Context) {
  134. variable := ctx.Value(seataContextVariable)
  135. if variable != nil {
  136. variable.(*ContextVariable).Xid = ""
  137. variable.(*ContextVariable).XidCopy = ""
  138. }
  139. }
  140. func SetFencePhase(ctx context.Context, phase enum.FencePhase) {
  141. variable := ctx.Value(seataContextVariable)
  142. if variable != nil {
  143. variable.(*ContextVariable).FencePhase = phase
  144. }
  145. }
  146. func GetFencePhase(ctx context.Context) enum.FencePhase {
  147. variable := ctx.Value(seataContextVariable)
  148. if variable != nil {
  149. return variable.(*ContextVariable).FencePhase
  150. }
  151. return enum.FencePhaseNotExist
  152. }