Files
gitea-MIRROR/models/actions/utils.go
Lunny Xiao d821a986ef perf(auth): store tokens as SHA-256 and throttle last-used writes (#39933)
Store PAT, Actions task and runner tokens as SHA-256 instead of PBKDF2,
which 160-bit random tokens don't need, and drop the token caches and
`SUCCESSFUL_TOKENS_CACHE_SIZE` that PBKDF2 required. Legacy rows are
rehashed on first use. Older releases can't verify new or rehashed
tokens. A PAT use now writes only its last-used timestamp, at most once
a minute.

Benchmarks on M2 Max, SQLite, median of 5:

| Benchmark | 1 token | 128 tokens |
| :--- | ---: | ---: |
| PAT lookup | 29.4 → 30.7 µs | 2.24 ms → 29.7 µs |
| PAT lookup with last-used | 107 → 29.6 µs | 2.44 ms → 30.1 µs |
| Task lookup | 63.2 → 48.4 µs | 2.25 ms → 49.4 µs |
| Runner verify, no DB | 122 → 160 ns | 141 → 162 ns |

Docs: https://gitea.com/gitea/docs/pulls/607

Assisted-by: Codet
Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-25 15:36:10 +00:00

81 lines
2.4 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"time"
"gitea.dev/modules/log"
"gitea.dev/modules/timeutil"
)
/*
LogIndexes is the index for mapping log line number to buffer offset.
Because it uses varint encoding, it is impossible to predict its size.
But we can make a simple estimate with an assumption that each log line has 200 byte, then:
| lines | file size | index size |
|-----------|---------------------|--------------------|
| 100 | 20 KiB(20000) | 258 B(258) |
| 1000 | 195 KiB(200000) | 2.9 KiB(2958) |
| 10000 | 1.9 MiB(2000000) | 34 KiB(34715) |
| 100000 | 19 MiB(20000000) | 386 KiB(394715) |
| 1000000 | 191 MiB(200000000) | 4.1 MiB(4323626) |
| 10000000 | 1.9 GiB(2000000000) | 47 MiB(49323626) |
| 100000000 | 19 GiB(20000000000) | 490 MiB(513424280) |
*/
type LogIndexes []int64
func (indexes *LogIndexes) FromDB(b []byte) error {
reader := bytes.NewReader(b)
for {
v, err := binary.ReadVarint(reader)
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return fmt.Errorf("binary ReadVarint: %w", err)
}
*indexes = append(*indexes, v)
}
}
func (indexes *LogIndexes) ToDB() ([]byte, error) {
buf, i := make([]byte, binary.MaxVarintLen64*len(*indexes)), 0
for _, v := range *indexes {
n := binary.PutVarint(buf[i:], v)
i += n
}
return buf[:i], nil
}
var timeSince = time.Since
// calculateDuration computes wall time for a run, job, task, or step. When status is terminal
// but stopped is missing or inconsistent with started, fallbackEnd (typically the row Updated
// time) is used so duration still reflects approximate elapsed time instead of 0 or a negative.
func calculateDuration(started, stopped timeutil.TimeStamp, status Status, fallbackEnd timeutil.TimeStamp) time.Duration {
if started == 0 {
return 0
}
s := started.AsTime()
if status.IsDone() {
end := stopped
if stopped.IsZero() || stopped < started {
if !fallbackEnd.IsZero() && fallbackEnd >= started {
end = fallbackEnd
} else {
log.Trace("actions: invalid duration timestamps (started=%d, stopped=%d, fallbackEnd=%d, status=%s)", started, stopped, fallbackEnd, status)
return 0
}
}
return end.AsTime().Sub(s)
}
return max(timeSince(s), 0).Truncate(time.Second)
}