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.

grpc_transaction_interceptor.go 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 grpc
  18. import (
  19. "context"
  20. "time"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/metadata"
  23. "github.com/seata/seata-go/pkg/common"
  24. "github.com/seata/seata-go/pkg/common/log"
  25. "github.com/seata/seata-go/pkg/tm"
  26. )
  27. // ClientTransactionInterceptor is client interceptor of grpc,
  28. // it's function is obtain xid in SeataContext,
  29. // and put it in the http header.
  30. func ClientTransactionInterceptor(ctx context.Context, method string, req, reply interface{},
  31. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  32. //set the XID when intercepting a client request and release it directly when intercepting a response
  33. if tm.IsSeataContext(ctx) {
  34. xid := tm.GetXID(ctx)
  35. header := make(map[string]string)
  36. header[common.XidKey] = xid
  37. ctx = metadata.NewOutgoingContext(ctx, metadata.New(header))
  38. }
  39. start := time.Now()
  40. err := invoker(ctx, method, req, reply, cc, opts...)
  41. end := time.Now()
  42. log.Infof("RPC: %s, start time: %s, end time: %s, err: %v", method,
  43. start.Format("Basic"), end.Format(time.RFC3339), err)
  44. return err
  45. }
  46. // ServerTransactionInterceptor is server interceptor of grpc
  47. // it's function is get xid from grpc http header ,and put it
  48. // into the context.
  49. func ServerTransactionInterceptor(ctx context.Context, req interface{},
  50. _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  51. md, ok := metadata.FromIncomingContext(ctx)
  52. if !ok {
  53. log.Errorf("missing grpc metadata")
  54. }
  55. var xid string
  56. if slice := md.Get(common.XidKey); slice != nil && len(slice) > 0 {
  57. xid = slice[0]
  58. }
  59. if xid == "" {
  60. if slice := md.Get(common.XidKeyLowercase); slice != nil && len(slice) > 0 {
  61. xid = slice[0]
  62. }
  63. }
  64. if xid != "" {
  65. ctx = tm.InitSeataContext(ctx)
  66. tm.SetXID(ctx, xid)
  67. log.Infof("global transaction xid is :%s", xid)
  68. } else {
  69. log.Info("global transaction xid is empty")
  70. }
  71. m, err := handler(ctx, req)
  72. if err != nil {
  73. log.Errorf("RPC failed with error %v", err)
  74. }
  75. return m, err
  76. }