From d073a3d52a2bd8610a58be74d21d48df43720b15 Mon Sep 17 00:00:00 2001 From: Grigory Buteyko Date: Thu, 22 Aug 2024 17:08:27 +0300 Subject: [PATCH] default timeout in tl clients --- internal/tlcodegen/qt_namespace.qtpl | 7 ++++- internal/tlcodegen/qt_namespace.qtpl.go | 7 ++++- .../tlcodegen/test/gen/schema/meta/meta.go | 30 +++++-------------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/internal/tlcodegen/qt_namespace.qtpl b/internal/tlcodegen/qt_namespace.qtpl index 8ead0025..9afca9bb 100644 --- a/internal/tlcodegen/qt_namespace.qtpl +++ b/internal/tlcodegen/qt_namespace.qtpl @@ -10,6 +10,7 @@ package {%s= gen.GlobalPackageName + name %} import ( {%- if gen.options.GenerateRPCCode && anyFunctions -%} "context" + "time" {%q= gen.options.BasicRPCPath %} {%q= gen.BasicPackageNameFull %} @@ -34,7 +35,8 @@ type Client struct { Client *rpc.Client Network string // should be either "tcp4" or "unix" Address string - ActorID int64 // should be non-zero when using rpc-proxy + ActorID int64 // should be >0 for routing via rpc-proxy + Timeout time.Time // used if not set in extra or ctx } {%= writeClientsCode(gen.GlobalPackageName, namespace.types, directImports, ourTypes) %} @@ -122,6 +124,9 @@ func (c *Client) {%s= typeString %}(ctx context.Context, args {%s= typeString %} if extra != nil { req.Extra = *extra } + if _, ok := ctx.Deadline(); !ok { + rpc.UpdateExtraTimeout(&req.Extra, c.Timeout) + } req.Body, err = args.WriteBoxedGeneral(req.Body) if err != nil { return internal.ErrorClientWrite("{%s= tlName %}", err) diff --git a/internal/tlcodegen/qt_namespace.qtpl.go b/internal/tlcodegen/qt_namespace.qtpl.go index a5a90e5e..82977be9 100644 --- a/internal/tlcodegen/qt_namespace.qtpl.go +++ b/internal/tlcodegen/qt_namespace.qtpl.go @@ -29,6 +29,7 @@ import ( `) if gen.options.GenerateRPCCode && anyFunctions { qw422016.N().S(` "context" + "time" `) qw422016.N().Q(gen.options.BasicRPCPath) @@ -68,7 +69,8 @@ type Client struct { Client *rpc.Client Network string // should be either "tcp4" or "unix" Address string - ActorID int64 // should be non-zero when using rpc-proxy + ActorID int64 // should be >0 for routing via rpc-proxy + Timeout time.Time // used if not set in extra or ctx } `) @@ -243,6 +245,9 @@ func streamwriteClientCode(qw422016 *qt422016.Writer, bytesVersion bool, shortPa if extra != nil { req.Extra = *extra } + if _, ok := ctx.Deadline(); !ok { + rpc.UpdateExtraTimeout(&req.Extra, c.Timeout) + } req.Body, err = args.WriteBoxedGeneral(req.Body) if err != nil { return internal.ErrorClientWrite("`) diff --git a/internal/tlcodegen/test/gen/schema/meta/meta.go b/internal/tlcodegen/test/gen/schema/meta/meta.go index 6c68fb63..9a0f7238 100644 --- a/internal/tlcodegen/test/gen/schema/meta/meta.go +++ b/internal/tlcodegen/test/gen/schema/meta/meta.go @@ -91,14 +91,14 @@ func CreateObjectFromName(name string) Object { } func CreateFunctionBytes(tag uint32) Function { - if item := FactoryItemByTLTagBytes(tag); item != nil && item.createFunctionBytes != nil { + if item := FactoryItemByTLTag(tag); item != nil && item.createFunctionBytes != nil { return item.createFunctionBytes() } return nil } func CreateObjectBytes(tag uint32) Object { - if item := FactoryItemByTLTagBytes(tag); item != nil && item.createObjectBytes != nil { + if item := FactoryItemByTLTag(tag); item != nil && item.createObjectBytes != nil { return item.createObjectBytes() } return nil @@ -106,7 +106,7 @@ func CreateObjectBytes(tag uint32) Object { // name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba" func CreateFunctionFromNameBytes(name string) Function { - if item := FactoryItemByTLNameBytes(name); item != nil && item.createFunctionBytes != nil { + if item := FactoryItemByTLName(name); item != nil && item.createFunctionBytes != nil { return item.createFunctionBytes() } return nil @@ -114,7 +114,7 @@ func CreateFunctionFromNameBytes(name string) Function { // name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba" func CreateObjectFromNameBytes(name string) Object { - if item := FactoryItemByTLNameBytes(name); item != nil && item.createObjectBytes != nil { + if item := FactoryItemByTLName(name); item != nil && item.createObjectBytes != nil { return item.createObjectBytes() } return nil @@ -199,22 +199,10 @@ func FactoryItemByTLName(name string) *TLItem { return itemsByName[name] } -func FactoryItemByTLTagBytes(tag uint32) *TLItem { - return itemsBytesByTag[tag] -} - -func FactoryItemByTLNameBytes(name string) *TLItem { - return itemsBytesByName[name] -} - var itemsByTag = map[uint32]*TLItem{} var itemsByName = map[string]*TLItem{} -var itemsBytesByTag = map[uint32]*TLItem{} - -var itemsBytesByName = map[string]*TLItem{} - func SetGlobalFactoryCreateForFunction(itemTag uint32, createObject func() Object, createFunction func() Function, createFunctionLong func() Function) { item := itemsByTag[itemTag] if item == nil { @@ -242,7 +230,7 @@ func SetGlobalFactoryCreateForEnumElement(itemTag uint32) { } func SetGlobalFactoryCreateForFunctionBytes(itemTag uint32, createObject func() Object, createFunction func() Function, createFunctionLong func() Function) { - item := itemsBytesByTag[itemTag] + item := itemsByTag[itemTag] if item == nil { panic(fmt.Sprintf("factory cannot find function tag #%08x to set", itemTag)) } @@ -252,7 +240,7 @@ func SetGlobalFactoryCreateForFunctionBytes(itemTag uint32, createObject func() } func SetGlobalFactoryCreateForObjectBytes(itemTag uint32, createObject func() Object) { - item := itemsBytesByTag[itemTag] + item := itemsByTag[itemTag] if item == nil { panic(fmt.Sprintf("factory cannot find item tag #%08x to set", itemTag)) } @@ -260,7 +248,7 @@ func SetGlobalFactoryCreateForObjectBytes(itemTag uint32, createObject func() Ob } func SetGlobalFactoryCreateForEnumElementBytes(itemTag uint32) { - item := itemsBytesByTag[itemTag] + item := itemsByTag[itemTag] if item == nil { panic(fmt.Sprintf("factory cannot find enum tag #%08x to set", itemTag)) } @@ -288,10 +276,6 @@ func fillObject(n1 string, n2 string, item *TLItem) { itemsByName[item.tlName] = item itemsByName[n1] = item itemsByName[n2] = item - itemsBytesByTag[item.tag] = item - itemsBytesByName[item.tlName] = item - itemsBytesByName[n1] = item - itemsBytesByName[n2] = item item.createObject = pleaseImportFactoryObject item.createObjectBytes = pleaseImportFactoryBytesObject // code below is as fast, but allocates some extra strings which are already in binary const segment due to JSON code