Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go/apps/api/routes/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func Register(srv *zen.Server, svc *Services, info zen.InstanceInfo) {
RatelimitNamespaceCache: svc.Caches.RatelimitNamespace,
TestMode: srv.Flags().TestMode,
Auditlogs: svc.Auditlogs,
Clock: svc.CachedClock,
},
)

Expand Down
3 changes: 0 additions & 3 deletions go/apps/api/routes/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/unkeyed/unkey/go/internal/services/ratelimit"
"github.com/unkeyed/unkey/go/internal/services/usagelimiter"
"github.com/unkeyed/unkey/go/pkg/clickhouse"
"github.com/unkeyed/unkey/go/pkg/clock"
"github.com/unkeyed/unkey/go/pkg/db"
"github.com/unkeyed/unkey/go/pkg/otel/logging"
"github.com/unkeyed/unkey/go/pkg/vault"
Expand All @@ -31,6 +30,4 @@ type Services struct {
PprofPassword string
UsageLimiter usagelimiter.Service
AnalyticsConnectionManager analytics.ConnectionManager

CachedClock clock.Clock
}
1 change: 0 additions & 1 deletion go/apps/api/routes/v2_ratelimit_multi_limit/200_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func TestLimitSuccessfully(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down
2 changes: 0 additions & 2 deletions go/apps/api/routes/v2_ratelimit_multi_limit/400_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestBadRequests(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down Expand Up @@ -137,7 +136,6 @@ func TestMissingAuthorizationHeader(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down
1 change: 0 additions & 1 deletion go/apps/api/routes/v2_ratelimit_multi_limit/401_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestUnauthorizedAccess(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down
2 changes: 0 additions & 2 deletions go/apps/api/routes/v2_ratelimit_multi_limit/403_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestWorkspacePermissions(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down Expand Up @@ -79,7 +78,6 @@ func TestInsufficientPermissions(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down
1 change: 0 additions & 1 deletion go/apps/api/routes/v2_ratelimit_multi_limit/410_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestSoftDeletedNamespace(t *testing.T) {
Ratelimit: h.Ratelimit,
RatelimitNamespaceCache: h.Caches.RatelimitNamespace,
Auditlogs: h.Auditlogs,
Clock: h.CachedClock,
}

h.Register(route)
Expand Down
8 changes: 3 additions & 5 deletions go/apps/api/routes/v2_ratelimit_multi_limit/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/unkeyed/unkey/go/pkg/cache"
"github.com/unkeyed/unkey/go/pkg/clickhouse"
"github.com/unkeyed/unkey/go/pkg/clickhouse/schema"
"github.com/unkeyed/unkey/go/pkg/clock"
"github.com/unkeyed/unkey/go/pkg/codes"
"github.com/unkeyed/unkey/go/pkg/db"
"github.com/unkeyed/unkey/go/pkg/fault"
Expand All @@ -43,7 +42,6 @@ type Handler struct {
Ratelimit ratelimit.Service
RatelimitNamespaceCache cache.Cache[cache.ScopedKey, db.FindRatelimitNamespace]
Auditlogs auditlogs.AuditLogService
Clock clock.Clock
TestMode bool
}

Expand Down Expand Up @@ -200,7 +198,7 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error {
ratelimitReqs := make([]ratelimit.RatelimitRequest, len(req))
checkMetadata := make([]checkMeta, len(req))

reqTime := h.Clock.Now()
reqTime := time.Now()
if h.TestMode {
header := s.Request().Header.Get("X-Test-Time")
if header != "" {
Expand Down Expand Up @@ -252,7 +250,7 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error {
}

// Batch rate limit all requests using RatelimitMany
start := h.Clock.Now()
start := time.Now()
results, err := h.Ratelimit.RatelimitMany(ctx, ratelimitReqs)
if err != nil {
return fault.Wrap(err,
Expand Down Expand Up @@ -337,7 +335,7 @@ func (h *Handler) createMissingNamespaces(
}

createdNamespaces, err := db.TxWithResult(ctx, h.DB.RW(), func(ctx context.Context, tx db.DBTX) (map[cache.ScopedKey]db.FindRatelimitNamespace, error) {
now := h.Clock.Now().UnixMilli()
now := time.Now().UnixMilli()
created := make(map[cache.ScopedKey]db.FindRatelimitNamespace)

// Prepare bulk insert params
Expand Down
2 changes: 0 additions & 2 deletions go/apps/api/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func Run(ctx context.Context, cfg Config) error {
}

clk := clock.New()
cachedClock := clock.NewCachedClock(time.Millisecond)

shutdowns := shutdown.New()

Expand Down Expand Up @@ -308,7 +307,6 @@ func Run(ctx context.Context, cfg Config) error {
PprofPassword: cfg.PprofPassword,
UsageLimiter: ulSvc,
AnalyticsConnectionManager: analyticsConnMgr,
CachedClock: cachedClock,
},
zen.InstanceInfo{
ID: cfg.InstanceID,
Expand Down
5 changes: 1 addition & 4 deletions go/pkg/testutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import (
type Harness struct {
t *testing.T

Clock *clock.TestClock
CachedClock clock.Clock
Clock *clock.TestClock

srv *zen.Server
validator *validation.Validator
Expand All @@ -59,7 +58,6 @@ type Harness struct {

func NewHarness(t *testing.T) *Harness {
clk := clock.NewTestClock()
cachedClock := clock.NewCachedClock(time.Millisecond)
logger := logging.New()

// Start all services in parallel first
Expand Down Expand Up @@ -191,7 +189,6 @@ func NewHarness(t *testing.T) *Harness {
DB: db,
seeder: seeder,
Clock: clk,
CachedClock: cachedClock,
AnalyticsConnectionManager: analyticsConnManager,
Auditlogs: auditlogs.New(auditlogs.Config{
DB: db,
Expand Down
7 changes: 2 additions & 5 deletions go/pkg/uid/uid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/unkeyed/unkey/go/pkg/base58"
"github.com/unkeyed/unkey/go/pkg/batchrand"
"github.com/unkeyed/unkey/go/pkg/clock"
)

// Prefix defines the standard resource type prefixes used throughout the system.
Expand Down Expand Up @@ -52,8 +51,6 @@ const (
DeploymentPrefix Prefix = "d"
)

var clk = clock.NewCachedClock(time.Millisecond)

// epoch starts more recently so that the 32-bit number space gives a
// significantly higher useful lifetime of around 136 years
// from 2023-11-14T22:13:20Z to 2159-12-22T04:41:36Z.
Expand Down Expand Up @@ -119,10 +116,10 @@ func New(prefix Prefix, byteSize ...int) string {
}

if bytes > 4 {
// Calculate seconds since epoch using our cached timestamp instead of time.Now() to avoid syscall
// Calculate seconds since epoch
// subtracting the epochTimestamp should guarantee we're not overflowing
// nolint:gosec
t := uint32(clk.Now().Unix() - epochTimestampSec)
t := uint32(time.Now().Unix() - epochTimestampSec)

// Write timestamp as first 4 bytes (big endian)
binary.BigEndian.PutUint32(buf[:4], t)
Expand Down
Loading