Answer the question
In order to leave comments, you need to log in
How to send a wait command to the pool worker?
When I access the site api, my session may expire, which leads to re-authorization. During authorization, my pool.Work continues to work, and in the meantime, the request handler receives information from the server, saying that the session has expired and access to the site is denied. Thus it turns out I do not get answers from the server with the information I need.
Taking the keywords from the file, I send them to pool.Work, after which it distributes them among the workers and the func DoWork handler itself is called, which can return a response from the server.
Question:
How can I pause when DoWork has received an expired session notification before getting a new session.
Answer the question
In order to leave comments, you need to log in
As a rough option:
1. a pool of workers that pull information, passing data about the session, but do not (re)authorize themselves
2. a separate goroutine that deals with (re)authorization
3. the current session (token, cookies)
4. Condition Variable ( https://golang.org/pkg/sync/#Cond), which synchronizes access to the current session
Initially:
there is no current session, the workers are waiting for a signal from the Condition Variable
The authorization gorutine sees that there is no current session, logs in, sets the current session and broadcast the event using a Condition Variable. Then it falls asleep until the signal from the Condition Variable.
Workers wake up and start working.
Session expired:
The worker receives the status of authorization required (for example, status 401), resets the current session (if it is set and equal to the one it knew about), and broadcasts the event using the Condition Variable.
The authorization goroutine wakes up, sees that there is no current session, and starts the authorization process again.
At this time, other workers receive 401, they check that the current session is empty and fall asleep before the signal from the Condition Variable
The authorization goroutine, having completed the authorization process, sets the current session and broadcasts the event using the Condition Variable. Then it goes back to sleep until the signal from Condition Variable.
Paused workers wake up and retry requests.
Workers that have not had time to fall asleep simply retry the request with the new session value.
How are we supposed to guess what a session is in your understanding? You haven't even written how your worker pool is organized and how your workers process tasks.
Make the architecture stateless and send the session along with the task.
To store session state, Go has context.Context , which can be canceled (note that you have to handle the cancellation manually).
As WinPooh32
already wrote - use the Context package .
In particular, the cancellation context that you will handle in the workers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question