diff --git a/go/apps/api/routes/register.go b/go/apps/api/routes/register.go index 430521ad0a..adb8a6d182 100644 --- a/go/apps/api/routes/register.go +++ b/go/apps/api/routes/register.go @@ -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, }, ) diff --git a/go/apps/api/routes/services.go b/go/apps/api/routes/services.go index 1ca3461e53..6e9e66d219 100644 --- a/go/apps/api/routes/services.go +++ b/go/apps/api/routes/services.go @@ -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" @@ -31,6 +30,4 @@ type Services struct { PprofPassword string UsageLimiter usagelimiter.Service AnalyticsConnectionManager analytics.ConnectionManager - - CachedClock clock.Clock } diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/200_test.go b/go/apps/api/routes/v2_ratelimit_multi_limit/200_test.go index 17574357f5..96dad56fb3 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/200_test.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/200_test.go @@ -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) diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/400_test.go b/go/apps/api/routes/v2_ratelimit_multi_limit/400_test.go index 81edad21b1..fce158ee69 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/400_test.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/400_test.go @@ -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) @@ -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) diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/401_test.go b/go/apps/api/routes/v2_ratelimit_multi_limit/401_test.go index 62873686d6..11b4863c49 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/401_test.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/401_test.go @@ -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) diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/403_test.go b/go/apps/api/routes/v2_ratelimit_multi_limit/403_test.go index 980375d64e..db39c1f7c3 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/403_test.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/403_test.go @@ -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) @@ -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) diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/410_test.go b/go/apps/api/routes/v2_ratelimit_multi_limit/410_test.go index 39d7233eaf..f195a1b525 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/410_test.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/410_test.go @@ -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) diff --git a/go/apps/api/routes/v2_ratelimit_multi_limit/handler.go b/go/apps/api/routes/v2_ratelimit_multi_limit/handler.go index 6a03a58144..6e9fd4a7a5 100644 --- a/go/apps/api/routes/v2_ratelimit_multi_limit/handler.go +++ b/go/apps/api/routes/v2_ratelimit_multi_limit/handler.go @@ -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" @@ -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 } @@ -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 != "" { @@ -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, @@ -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 diff --git a/go/apps/api/run.go b/go/apps/api/run.go index 67a5d73ba5..7920dd0164 100644 --- a/go/apps/api/run.go +++ b/go/apps/api/run.go @@ -42,7 +42,6 @@ func Run(ctx context.Context, cfg Config) error { } clk := clock.New() - cachedClock := clock.NewCachedClock(time.Millisecond) shutdowns := shutdown.New() @@ -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, diff --git a/go/pkg/testutil/http.go b/go/pkg/testutil/http.go index 639924914e..d1b524c7ef 100644 --- a/go/pkg/testutil/http.go +++ b/go/pkg/testutil/http.go @@ -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 @@ -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 @@ -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, diff --git a/go/pkg/uid/uid.go b/go/pkg/uid/uid.go index dbdfb25011..ee09cdab49 100644 --- a/go/pkg/uid/uid.go +++ b/go/pkg/uid/uid.go @@ -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. @@ -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. @@ -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)