Supports configuring fetch_timeout and fetch_interval. (#100)

Fix #99.

Fix #94.

Reviewed-on: https://gitea.com/gitea/act_runner/pulls/100
Reviewed-by: Zettat123 <zettat123@noreply.gitea.io>
This commit is contained in:
Jason Song 2023-04-06 10:57:36 +08:00
parent 220efa69c0
commit d139faa40c
3 changed files with 27 additions and 16 deletions

View File

@ -7,7 +7,6 @@ import (
"context" "context"
"errors" "errors"
"sync" "sync"
"time"
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
"github.com/bufbuild/connect-go" "github.com/bufbuild/connect-go"
@ -22,21 +21,21 @@ import (
type Poller struct { type Poller struct {
client client.Client client client.Client
runner *run.Runner runner *run.Runner
capacity int cfg *config.Config
} }
func New(cfg *config.Config, client client.Client, runner *run.Runner) *Poller { func New(cfg *config.Config, client client.Client, runner *run.Runner) *Poller {
return &Poller{ return &Poller{
client: client, client: client,
runner: runner, runner: runner,
capacity: cfg.Runner.Capacity, cfg: cfg,
} }
} }
func (p *Poller) Poll(ctx context.Context) { func (p *Poller) Poll(ctx context.Context) {
limiter := rate.NewLimiter(rate.Every(2*time.Second), 1) limiter := rate.NewLimiter(rate.Every(p.cfg.Runner.FetchInterval), 1)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for i := 0; i < p.capacity; i++ { for i := 0; i < p.cfg.Runner.Capacity; i++ {
wg.Add(1) wg.Add(1)
go p.poll(ctx, wg, limiter) go p.poll(ctx, wg, limiter)
} }
@ -63,7 +62,7 @@ func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Lim
} }
func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) { func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {
reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second) reqCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.FetchTimeout)
defer cancel() defer cancel()
resp, err := p.client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{})) resp, err := p.client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{}))

View File

@ -22,6 +22,10 @@ runner:
timeout: 3h timeout: 3h
# Whether skip verifying the TLS certificate of the Gitea instance. # Whether skip verifying the TLS certificate of the Gitea instance.
insecure: false insecure: false
# The timeout for fetching the job from the Gitea instance.
fetch_timeout: 5s
# The interval for fetching the job from the Gitea instance.
fetch_interval: 2s
cache: cache:
# Enable cache server to use actions/cache. # Enable cache server to use actions/cache.

View File

@ -24,6 +24,8 @@ type Config struct {
EnvFile string `yaml:"env_file"` EnvFile string `yaml:"env_file"`
Timeout time.Duration `yaml:"timeout"` Timeout time.Duration `yaml:"timeout"`
Insecure bool `yaml:"insecure"` Insecure bool `yaml:"insecure"`
FetchTimeout time.Duration `yaml:"fetch_timeout"`
FetchInterval time.Duration `yaml:"fetch_interval"`
} `yaml:"runner"` } `yaml:"runner"`
Cache struct { Cache struct {
Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set
@ -90,6 +92,12 @@ func LoadDefault(file string) (*Config, error) {
if cfg.Container.NetworkMode == "" { if cfg.Container.NetworkMode == "" {
cfg.Container.NetworkMode = "bridge" cfg.Container.NetworkMode = "bridge"
} }
if cfg.Runner.FetchTimeout <= 0 {
cfg.Runner.FetchTimeout = 5 * time.Second
}
if cfg.Runner.FetchInterval <= 0 {
cfg.Runner.FetchInterval = 2 * time.Second
}
return cfg, nil return cfg, nil
} }