Skip to content

Commit

Permalink
async usage
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Mar 27, 2024
1 parent 1b4e8e2 commit 53a72b8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (m *MemoryStore) GetAccountUsage(ctx context.Context, projectID uint64, ser
return usage, nil
}

func (m *MemoryStore) GetAccessKeyUsage(ctx context.Context, accessKey string, service *proto.Service, min, max time.Time) (proto.AccessUsage, error) {
func (m *MemoryStore) GetAccessKeyUsage(ctx context.Context, projectID uint64, accessKey string, service *proto.Service, min, max time.Time) (proto.AccessUsage, error) {
m.Lock()
defer m.Unlock()
if _, ok := m.accessKeys[accessKey]; !ok {
Expand Down
29 changes: 27 additions & 2 deletions proto/clients/quotacontrol.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// quota-control v0.1.0 b4b7dfcf9ec0caa2d786bc56bfcb6127a0eb915d
// quota-control v0.1.0 8550ef3b468c2264c0fa011d79d50062d38b1138
// --
// Code generated by [email protected] with [email protected] generator. DO NOT EDIT.
//
Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v0.1.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "b4b7dfcf9ec0caa2d786bc56bfcb6127a0eb915d"
export const WebRPCSchemaHash = "8550ef3b468c2264c0fa011d79d50062d38b1138"

//
// Types
Expand Down Expand Up @@ -115,6 +115,7 @@ export interface QuotaControl {
clearAccessQuotaCache(args: ClearAccessQuotaCacheArgs, headers?: object, signal?: AbortSignal): Promise<ClearAccessQuotaCacheReturn>
getAccountUsage(args: GetAccountUsageArgs, headers?: object, signal?: AbortSignal): Promise<GetAccountUsageReturn>
getAccessKeyUsage(args: GetAccessKeyUsageArgs, headers?: object, signal?: AbortSignal): Promise<GetAccessKeyUsageReturn>
getAsyncUsage(args: GetAsyncUsageArgs, headers?: object, signal?: AbortSignal): Promise<GetAsyncUsageReturn>
prepareUsage(args: PrepareUsageArgs, headers?: object, signal?: AbortSignal): Promise<PrepareUsageReturn>
clearUsage(args: ClearUsageArgs, headers?: object, signal?: AbortSignal): Promise<ClearUsageReturn>
notifyEvent(args: NotifyEventArgs, headers?: object, signal?: AbortSignal): Promise<NotifyEventReturn>
Expand Down Expand Up @@ -239,6 +240,16 @@ export interface GetAccessKeyUsageArgs {
export interface GetAccessKeyUsageReturn {
usage: AccessUsage
}
export interface GetAsyncUsageArgs {
projectID: number
service?: Service
from?: string
to?: string
}

export interface GetAsyncUsageReturn {
usage: AccessUsage
}
export interface PrepareUsageArgs {
projectID: number
cycle: Cycle
Expand Down Expand Up @@ -516,6 +527,20 @@ export class QuotaControl implements QuotaControl {
})
}

getAsyncUsage = (args: GetAsyncUsageArgs, headers?: object, signal?: AbortSignal): Promise<GetAsyncUsageReturn> => {
return this.fetch(
this.url('GetAsyncUsage'),
createHTTPRequest(args, headers, signal)).then((res) => {
return buildResponse(res).then(_data => {
return {
usage: <AccessUsage>(_data.usage),
}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

prepareUsage = (args: PrepareUsageArgs, headers?: object, signal?: AbortSignal): Promise<PrepareUsageReturn> => {
return this.fetch(
this.url('PrepareUsage'),
Expand Down
88 changes: 77 additions & 11 deletions proto/proto.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions proto/proto.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=proto.ridl [email protected] -pkg=proto -server -client -out=./proto.gen.go
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=proto.ridl [email protected] -client -out=./clients/quotacontrol.gen.ts
package proto

import (
Expand All @@ -6,9 +8,6 @@ import (
"time"
)

//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=proto.ridl [email protected] -pkg=proto -server -client -out=./proto.gen.go
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=proto.ridl [email protected] -client -out=./clients/quotacontrol.gen.ts

func Ptr[T any](v T) *T {
return &v
}
Expand Down Expand Up @@ -200,5 +199,4 @@ func (c *Cycle) Advance(now time.Time) {
c.Start = c.Start.AddDate(0, 1, 0)
c.End = c.End.AddDate(0, 1, 0)
}

}
1 change: 1 addition & 0 deletions proto/proto.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ service QuotaControl
- ClearAccessQuotaCache(projectID: uint64) => (ok: bool)
- GetAccountUsage(projectID: uint64, service?: Service, from?: timestamp, to?: timestamp) => (usage: AccessUsage)
- GetAccessKeyUsage(accessKey: string, service?: Service, from?: timestamp, to?: timestamp) => (usage: AccessUsage)
- GetAsyncUsage(projectID: uint64, service?: Service, from?: timestamp, to?: timestamp) => (usage: AccessUsage)
- PrepareUsage(projectID: uint64, cycle: Cycle, now: timestamp) => (ok: bool)
- ClearUsage(projectID: uint64, now: timestamp) => (ok: bool)
- NotifyEvent(projectID: uint64, eventType: EventType) => (ok: bool)
Expand Down
17 changes: 15 additions & 2 deletions quotacontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type AccessKeyStore interface {
}

type UsageStore interface {
GetAccessKeyUsage(ctx context.Context, accessKey string, service *proto.Service, min, max time.Time) (proto.AccessUsage, error)
GetAccessKeyUsage(ctx context.Context, projectID uint64, accessKey string, service *proto.Service, min, max time.Time) (proto.AccessUsage, error)
GetAccountUsage(ctx context.Context, projectID uint64, service *proto.Service, min, max time.Time) (proto.AccessUsage, error)
UpdateAccessUsage(ctx context.Context, projectID uint64, accessKey string, service proto.Service, time time.Time, usage proto.AccessUsage) error
}
Expand Down Expand Up @@ -108,6 +108,19 @@ func (q qcHandler) GetAccountUsage(ctx context.Context, projectID uint64, servic
return &usage, nil
}

func (q qcHandler) GetAsyncUsage(ctx context.Context, projectID uint64, service *proto.Service, from, to *time.Time) (*proto.AccessUsage, error) {
min, max, err := q.GetTimeRange(ctx, projectID, from, to)
if err != nil {
return nil, err
}

usage, err := q.store.UsageStore.GetAccessKeyUsage(ctx, projectID, "", service, min, max)
if err != nil {
return nil, err
}
return &usage, nil
}

func (q qcHandler) GetAccessKeyUsage(ctx context.Context, accessKey string, service *proto.Service, from, to *time.Time) (*proto.AccessUsage, error) {
projectID, err := GetProjectID(accessKey)
if err != nil {
Expand All @@ -119,7 +132,7 @@ func (q qcHandler) GetAccessKeyUsage(ctx context.Context, accessKey string, serv
return nil, err
}

usage, err := q.store.UsageStore.GetAccessKeyUsage(ctx, accessKey, service, min, max)
usage, err := q.store.UsageStore.GetAccessKeyUsage(ctx, projectID, accessKey, service, min, max)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 53a72b8

Please sign in to comment.