MCPcopy
hub / github.com/keploy/keploy / decodeGeneric

Function decodeGeneric

pkg/agent/proxy/integrations/generic/decode.go:19–120  ·  view source on GitHub ↗
(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, mockDb integrations.MockMemDb, _ models.OutgoingOptions)

Source from the content-addressed store, hash-verified

17)
18
19func decodeGeneric(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, mockDb integrations.MockMemDb, _ models.OutgoingOptions) error {
20 genericRequests := [][]byte{reqBuf}
21 logger.Debug("Into the generic parser in test mode")
22 errCh := make(chan error, 1)
23 go func(errCh chan error, genericRequests [][]byte) {
24 defer pUtil.Recover(logger, clientConn, nil)
25 defer close(errCh)
26 for {
27 // Since protocol packets have to be parsed for checking stream end,
28 // clientConnection have deadline for read to determine the end of stream.
29 err := clientConn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
30 if err != nil {
31 utils.LogError(logger, err, "failed to set the read deadline for the client conn")
32 return
33 }
34
35 // To read the stream of request packets from the client
36 for {
37 buffer, err := pUtil.ReadBytes(ctx, logger, clientConn)
38 // Applied this nolint to ignore the staticcheck error here because of readability
39 // nolint:staticcheck
40 if netErr, ok := err.(net.Error); !(ok && netErr.Timeout()) && err != nil && err.Error() != "EOF" {
41 utils.LogError(logger, err, "failed to read the request message in proxy for generic dependency")
42 return
43 }
44 if netErr, ok := err.(net.Error); (ok && netErr.Timeout()) || (err != nil && err.Error() == "EOF") {
45 logger.Debug("the timeout for the client read in generic or EOF")
46 break
47 }
48 genericRequests = append(genericRequests, buffer)
49 }
50
51 if len(genericRequests) == 0 {
52 logger.Debug("the generic request buffer is empty")
53 continue
54 }
55
56 // bestMatchedIndx := 0
57 // fuzzy match gives the index for the best matched generic mock
58 matched, genericResponses, err := fuzzyMatch(ctx, logger, genericRequests, mockDb)
59 if err != nil {
60 utils.LogError(logger, err, "error while matching generic mocks")
61 }
62
63 if !matched {
64 preview := genericRequests[0]
65 if len(preview) > 64 {
66 preview = preview[:64]
67 }
68 // Build the universal mismatch report so generic misses show
69 // up in the mismatch table / report yaml like HTTP and MySQL
70 // misses do, instead of vanishing as a bare error.
71 report := buildGenericMismatchReport(ctx, genericRequests, mockDb)
72 // Per-call generic detail at Debug; the canonical mock-mismatch
73 // WARN is emitted once in proxy.sendMockNotFoundError (covers
74 // every parser), so this stays Debug to avoid double-logging.
75 logger.Debug("no matching generic mock found for outgoing call",
76 zap.String("protocol", report.Protocol),

Callers 1

MockOutgoingMethod · 0.85

Calls 11

LogErrorFunction · 0.92
NewMockMismatchErrorFunction · 0.92
fuzzyMatchFunction · 0.85
AddMethod · 0.80
DebugMethod · 0.65
TimeoutMethod · 0.65
WriteMethod · 0.65
SetReadDeadlineMethod · 0.45
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected