• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trento-project / agent / 12753131938

13 Jan 2025 05:46PM UTC coverage: 71.479% (-0.4%) from 71.911%
12753131938

Pull #361

github

balanza
fix lint
Pull Request #361: Handle context in plugins

0 of 48 new or added lines in 4 files covered. (0.0%)

3 existing lines in 1 file now uncovered.

4050 of 5666 relevant lines covered (71.48%)

20.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/factsengine/plugininterface/rpc.go
1
package plugininterface
2

3
import (
4
        "context"
5
        "net/rpc"
6

7
        log "github.com/sirupsen/logrus"
8

9
        "github.com/google/uuid"
10
        "github.com/trento-project/agent/pkg/factsengine/entities"
11
)
12

13
type GathererRPC struct{ client *rpc.Client }
14

15
func (g *GathererRPC) RequestGathering(
16
        ctx context.Context,
17
        factsRequest []entities.FactRequest,
NEW
18
) ([]entities.Fact, error) {
×
19
        var resp []entities.Fact
×
NEW
20
        var err error
×
NEW
21

×
NEW
22
        requestID := uuid.New().String()
×
NEW
23
        args := GatheringArgs{
×
NEW
24
                Facts:     factsRequest,
×
NEW
25
                RequestID: requestID,
×
NEW
26
        }
×
27

×
NEW
28
        gathering := make(chan error)
×
29

×
NEW
30
        go func() {
×
NEW
31
                gathering <- g.client.Call("Plugin.ServeGathering", args, &resp)
×
NEW
32
        }()
×
33

NEW
34
        select {
×
NEW
35
        case <-ctx.Done():
×
NEW
36
                err = g.client.Call("Plugin.Cancel", requestID, &resp)
×
NEW
37
                return []entities.Fact{}, err
×
NEW
38
        case err = <-gathering:
×
NEW
39
                if err != nil {
×
NEW
40
                        return nil, err
×
NEW
41
                }
×
NEW
42
                return resp, nil
×
43
        }
44
}
45

46
type GathererRPCServer struct {
47
        Impl      Gatherer
48
        cancelMap map[string]context.CancelFunc
49
}
50

51
type GatheringArgs struct {
52
        Facts     []entities.FactRequest
53
        RequestID string
54
}
55

NEW
56
func (s *GathererRPCServer) ServeGathering(args GatheringArgs, resp *[]entities.Fact) error {
×
NEW
57

×
NEW
58
        ctx, cancel := context.WithCancel(context.Background())
×
NEW
59
        if s.cancelMap == nil {
×
NEW
60
                s.cancelMap = make(map[string]context.CancelFunc)
×
NEW
61
        }
×
NEW
62
        s.cancelMap[args.RequestID] = cancel
×
NEW
63
        defer delete(s.cancelMap, args.RequestID)
×
NEW
64

×
65
        var err error
×
NEW
66
        *resp, err = s.Impl.Gather(ctx, args.Facts)
×
67
        return err
×
68
}
69

NEW
70
func (s *GathererRPCServer) Cancel(requestID string, _ *[]entities.Fact) (_ error) {
×
NEW
71
        cancel, ok := s.cancelMap[requestID]
×
NEW
72
        if ok {
×
NEW
73
                cancel()
×
NEW
74
                delete(s.cancelMap, requestID)
×
NEW
75
        } else {
×
NEW
76
                log.Warnf("Cannot find cancel function for request %s", requestID)
×
NEW
77
        }
×
78

NEW
79
        return nil
×
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc