Welcome to mirror list, hosted at ThFree Co, Russian Federation.

errorhandler.go « middleware « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5339419a513af1547683baa7b2f0b5a983208b60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package middleware

import (
	"context"
	"fmt"
	"io"

	"gitlab.com/gitlab-org/gitaly/internal/praefect/nodes/tracker"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry"
	"google.golang.org/grpc"
)

// StreamErrorHandler returns a client interceptor that will track accessor/mutator errors from internal gitaly nodes
func StreamErrorHandler(registry *protoregistry.Registry, errorTracker tracker.ErrorTracker, nodeStorage string) grpc.StreamClientInterceptor {
	return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
		stream, err := streamer(ctx, desc, cc, method, opts...)

		mi, lookupErr := registry.LookupMethod(method)
		if err != nil {
			return nil, fmt.Errorf("error when looking up method: %w %v", err, lookupErr)
		}

		return newCatchErrorStreamer(stream, errorTracker, mi.Operation, nodeStorage), err
	}
}

// catchErrorSteamer is a custom ClientStream that adheres to grpc.ClientStream but keeps track of accessor/mutator errors
type catchErrorStreamer struct {
	grpc.ClientStream
	errors      tracker.ErrorTracker
	operation   protoregistry.OpType
	nodeStorage string
}

func newCatchErrorStreamer(streamer grpc.ClientStream, errors tracker.ErrorTracker, operation protoregistry.OpType, nodeStorage string) *catchErrorStreamer {
	return &catchErrorStreamer{
		ClientStream: streamer,
		errors:       errors,
		operation:    operation,
		nodeStorage:  nodeStorage,
	}
}

// SendMsg proxies the send but records any errors
func (c *catchErrorStreamer) SendMsg(m interface{}) error {
	err := c.ClientStream.SendMsg(m)
	if err != nil {
		switch c.operation {
		case protoregistry.OpAccessor:
			c.errors.IncrReadErr(c.nodeStorage)
		case protoregistry.OpMutator:
			c.errors.IncrWriteErr(c.nodeStorage)
		}
	}

	return err
}

// RecvMsg proxies the send but records any errors
func (c *catchErrorStreamer) RecvMsg(m interface{}) error {
	err := c.ClientStream.RecvMsg(m)
	if err != nil && err != io.EOF {
		switch c.operation {
		case protoregistry.OpAccessor:
			c.errors.IncrReadErr(c.nodeStorage)
		case protoregistry.OpMutator:
			c.errors.IncrWriteErr(c.nodeStorage)
		}
	}

	return err
}