@@ -11,7 +11,6 @@ import (
1111 "fmt"
1212 "io/fs"
1313 "log"
14- "net/http"
1514 "os"
1615 "path/filepath"
1716 "regexp"
@@ -59,46 +58,59 @@ func RefreshToken(chromedpCtx context.Context) map[string][]string {
5958 panic (err )
6059 }
6160
61+ delayedRetryCallback := func (numRetries int ) {
62+ time .Sleep (250 * time .Millisecond * time .Duration (numRetries ))
63+ }
64+
6265 VPrintf ("Getting new token..." )
63- _ , err = chromedp .RunResponse (chromedpCtx ,
64- chromedp .ActionFunc (func (ctx context.Context ) error {
65- err := network .ClearBrowserCookies ().Do (ctx )
66- return err
67- }),
68- chromedp .Navigate (`https://wat.utdallas.edu/login` ),
69- chromedp .WaitVisible (`form#login-form` ),
70- chromedp .SendKeys (`input#netid` , netID ),
71- chromedp .SendKeys (`input#password` , password ),
72- chromedp .WaitVisible (`button#login-button` ),
73- chromedp .Click (`button#login-button` ),
74- chromedp .WaitVisible (`body` ),
75- )
66+ err = Retry (func () error {
67+ _ , err = chromedp .RunResponse (chromedpCtx ,
68+ chromedp .ActionFunc (func (ctx context.Context ) error {
69+ err := network .ClearBrowserCookies ().Do (ctx )
70+ return err
71+ }),
72+ chromedp .Navigate (`https://wat.utdallas.edu/login` ),
73+ chromedp .WaitVisible (`form#login-form` ),
74+ chromedp .SendKeys (`input#netid` , netID ),
75+ chromedp .SendKeys (`input#password` , password ),
76+ chromedp .WaitVisible (`button#login-button` ),
77+ chromedp .Click (`button#login-button` ),
78+ chromedp .WaitVisible (`body` ),
79+ )
80+ return err
81+ }, 3 , delayedRetryCallback )
82+
7683 if err != nil {
7784 panic (err )
7885 }
7986
8087 time .Sleep (250 * time .Millisecond )
8188
8289 var cookieStrs []string
83- _ , err = chromedp .RunResponse (chromedpCtx ,
84- chromedp .Navigate (`https://coursebook.utdallas.edu/` ),
85- chromedp .ActionFunc (func (ctx context.Context ) error {
86- cookies , err := network .GetCookies ().Do (ctx )
87- cookieStrs = make ([]string , len (cookies ))
88- gotToken := false
89- for i , cookie := range cookies {
90- cookieStrs [i ] = fmt .Sprintf ("%s=%s" , cookie .Name , cookie .Value )
91- if cookie .Name == "PTGSESSID" {
92- VPrintf ("Got new token: PTGSESSID = %s" , cookie .Value )
93- gotToken = true
90+
91+ err = Retry (func () error {
92+ _ , err = chromedp .RunResponse (chromedpCtx ,
93+ chromedp .Navigate (`https://coursebook.utdallas.edu/` ),
94+ chromedp .ActionFunc (func (ctx context.Context ) error {
95+ cookies , err := network .GetCookies ().Do (ctx )
96+ cookieStrs = make ([]string , len (cookies ))
97+ gotToken := false
98+ for i , cookie := range cookies {
99+ cookieStrs [i ] = fmt .Sprintf ("%s=%s" , cookie .Name , cookie .Value )
100+ if cookie .Name == "PTGSESSID" {
101+ VPrintf ("Got new token: PTGSESSID = %s" , cookie .Value )
102+ gotToken = true
103+ }
94104 }
95- }
96- if ! gotToken {
97- return errors .New ("failed to get a new token" )
98- }
99- return err
100- }),
101- )
105+ if ! gotToken {
106+ return errors .New ("failed to get a new token" )
107+ }
108+ return err
109+ }),
110+ )
111+ return err
112+ }, 3 , delayedRetryCallback )
113+
102114 if err != nil {
103115 panic (err )
104116 }
@@ -249,22 +261,16 @@ func Regexpf(format string, vars ...interface{}) *regexp.Regexp {
249261 return regexp .MustCompile (fmt .Sprintf (format , vars ... ))
250262}
251263
252- // Attempts to run the given HTTP request with the given HTTP client, wrapping the request with a retry callback
253- func RetryHTTP (requestCreator func () * http.Request , client * http.Client , retryCallback func (res * http.Response , numRetries int )) (res * http.Response , err error ) {
254- // Retry loop for requests
255- numRetries := 0
256- for {
257- // Perform HTTP request, retrying if we get a non-200 response code
258- res , err = client .Do (requestCreator ())
259- // Retry handling
260- if res .StatusCode != 200 {
261- retryCallback (res , numRetries )
262- numRetries ++
263- continue
264+ // Attempts to retry running the given error-returning function up to a maximum number of retries, at which point the last error is returned. A callback is called between each retry.
265+ func Retry (action func () error , maxRetries int , retryCallback func (numRetries int )) error {
266+ for retries := 1 ; ; retries ++ {
267+ // Perform the action
268+ err := action ()
269+ if err == nil || retries > maxRetries {
270+ return err
264271 }
265- break
272+ retryCallback ( retries )
266273 }
267- return res , err
268274}
269275
270276func GetCoursePrefixes (chromedpCtx context.Context ) []string {
0 commit comments