Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client.ExportStringSession() and add RunMiddleware field to client. #41

Merged
merged 24 commits into from
Jan 10, 2024
Merged

Fix client.ExportStringSession() and add RunMiddleware field to client. #41

merged 24 commits into from
Jan 10, 2024

Conversation

KoNekoD
Copy link
Contributor

@KoNekoD KoNekoD commented Dec 27, 2023

No description provided.

@KoNekoD KoNekoD closed this Dec 29, 2023
@KoNekoD KoNekoD reopened this Jan 6, 2024
@@ -123,6 +123,20 @@ type ClientOpts struct {
ErrorHandler dispatcher.ErrorHandler
// Custom middlewares
Middlewares []telegram.Middleware
// Custom Run() Middleware
RunMiddleware func(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of any potential need of this middleware function, can you tell us what made you add this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overlay Run function(needed for FloodWait middleware)
https://pkg.go.dev/github.com/gotd/contrib/middleware/floodwait

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I'd suggest to rename (L 128) origRun to runFunc with respect to naming scheme throughout the library.

client.go Outdated
@@ -135,6 +149,9 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*
}

ctx, cancel := context.WithCancel(context.Background())
if opts.Ctx != nil && opts.CtxCancel != nil {
ctx, cancel = opts.Ctx, opts.CtxCancel
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this external context and cancel function helps and what problem does it solve?
Also, you should consider adding the original ctx and cancel declaration of the library in an else condition as the created cancellable context would never be used in case the condition present in line 152 comes out to be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the idea, this is needed to create a time out, before, if the account was broken, the client would wait forever, has this been fixed now? I did this to transmit a 60-second timer there. Maybe you have an idea on how to do this better? It’s just that my decision is dirty, I admit, I can’t find out the reason for the failure, and this is important! Please help with the solution, you, as a library designer, probably know whether the client will hang forever or time out, and is there such a thing at all or is there some other processing mechanism?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just take Context as optional input argument rather than taking Context and a CancelFunc as cancel function can be derived later by the library itself to ease it for the user.
Something like:

if opts.Context == nil {
   opts.Context = context.Background()
}
ctx, cancel := context.WithCancel(opts.Context)

What do you think about it?

client.go Outdated
c.initialize(&wg),
)
}

c.err = c.Run(c.ctx, c.initialize(&wg))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should consider removing this because you are already calling the c.Run function above it.

Please clarify the need of RunMiddleware before proceeding to this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 6974b37

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@celestix The thing is that I need access to the flood wait middleware, now I have removed it in my code, but I need to make my own handler and most likely I will still need access to this method. If you think this is freaky, then suggest how I can do this. I launch 10 clients, put them in an array, launch consumers for AMQP and put tasks there, for example, join a chat, and at the moment the task is executed, the client catches a flood wait, each client is launched according to the session that is in the Account table, I need to tag this account like floodwaited for time (and there is a time when floodwait will finish its work), so if you can offer an idea on how I can do this without wrapping RunMiddleware, then I will be glad. I myself think this is a crutch and dirty code, we both want to see this project clean) But it is closed for extension, and we need to think together how to solve this. we can discuss this in chat

@celestix celestix changed the title My to origin Fix client.ExportStringSession() and add RunMiddleware field to client. Jan 6, 2024
client.go Outdated
@@ -135,6 +149,9 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*
}

ctx, cancel := context.WithCancel(context.Background())
if opts.Ctx != nil && opts.CtxCancel != nil {
ctx, cancel = opts.Ctx, opts.CtxCancel
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just take Context as optional input argument rather than taking Context and a CancelFunc as cancel function can be derived later by the library itself to ease it for the user.
Something like:

if opts.Context == nil {
   opts.Context = context.Background()
}
ctx, cancel := context.WithCancel(opts.Context)

What do you think about it?

@@ -123,6 +123,20 @@ type ClientOpts struct {
ErrorHandler dispatcher.ErrorHandler
// Custom middlewares
Middlewares []telegram.Middleware
// Custom Run() Middleware
RunMiddleware func(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I'd suggest to rename (L 128) origRun to runFunc with respect to naming scheme throughout the library.

@celestix celestix merged commit 5c67391 into celestix:beta Jan 10, 2024
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants