File: api.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (72 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download
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 modshared

import (
	"context"

	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/errz"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/retry"
	"go.uber.org/zap"
)

type ModuleStartStopPhase byte

const (
	_ ModuleStartStopPhase = iota
	ModuleStartBeforeServers
	ModuleStartAfterServers
)

const (
	NoAgentId int64 = 0
)

type Factory interface {
	// Name returns module's name.
	Name() string
	// StartStopPhase defines when to start and stop module during the program lifecycle.
	StartStopPhase() ModuleStartStopPhase
}

// Api provides the API for the module to use.
type Api interface {
	// HandleProcessingError can be used to handle errors occurring while processing a request.
	// If err is a (or wraps a) errz.UserError, it might be handled specially.
	HandleProcessingError(ctx context.Context, log *zap.Logger, agentId int64, msg string, err error)
}

// RpcApi provides the API for the module's gRPC handlers to use.
type RpcApi interface {
	// Log returns a logger to use in the context of the request being processed.
	Log() *zap.Logger
	// HandleProcessingError can be used to handle errors occurring while processing a request.
	// If err is a (or wraps a) errz.UserError, it might be handled specially.
	HandleProcessingError(log *zap.Logger, agentId int64, msg string, err error)
	// HandleIoError can be used to handle I/O error produced by gRPC Send(), Recv() methods or any other I/O error.
	// It returns an error, compatible with gRPC status package.
	HandleIoError(log *zap.Logger, msg string, err error) error
	// PollWithBackoff runs f every duration given by BackoffManager.
	//
	// PollWithBackoff should be used by the top-level polling, so that it can be gracefully interrupted
	// by the server when necessary. E.g. when stream is nearing it's max connection age or program needs to
	// be shut down.
	// If sliding is true, the period is computed after f runs. If it is false then
	// period includes the runtime for f.
	// It returns when:
	// - stream's context is cancelled or max connection age has been reached. nil is returned in this case.
	// - f returns Done. error from f is returned in this case.
	PollWithBackoff(cfg retry.PollConfig, f retry.PollWithBackoffFunc) error
}

func ApiToErrReporter(api Api) errz.ErrReporter {
	return &errReporter{
		Api: api,
	}
}

type errReporter struct {
	Api Api
}

func (r *errReporter) HandleProcessingError(ctx context.Context, log *zap.Logger, msg string, err error) {
	r.Api.HandleProcessingError(ctx, log, NoAgentId, msg, err)
}