@@ -10,6 +10,7 @@ import (
1010 "strconv"
1111 "strings"
1212 "time"
13+ "io"
1314)
1415
1516func debug (s string , x ... interface {}) {
@@ -167,20 +168,19 @@ func send_msg_to_discord(embeds []DiscordEmbed, webhookURL string, username stri
167168 log .Println ("Create discord json false" )
168169 return
169170 }
170- body := bytes .NewReader (payloadBytes )
171-
172- req , err := http .NewRequest ("POST" , webhookURL , body )
173- if err != nil {
174- log .Println ("Create discord request false" )
175- return
176- }
177- req .Header .Set ("Content-Type" , "application/json" )
178171
179172 // Retry loop with exponential backoff and special handling for 429
180173 var resp * http.Response
181174 var attempt int
182175 maxRetries := 5
183176 for attempt = 0 ; attempt <= maxRetries ; attempt ++ {
177+ req , err := http .NewRequest ("POST" , webhookURL , bytes .NewReader (payloadBytes ))
178+ if err != nil {
179+ log .Println ("Create discord request false" )
180+ return
181+ }
182+ req .Header .Set ("Content-Type" , "application/json" )
183+
184184 resp , err = client .Do (req )
185185 if err != nil {
186186 // network error, retry
@@ -192,7 +192,10 @@ func send_msg_to_discord(embeds []DiscordEmbed, webhookURL string, username stri
192192 // handle 429 (rate limit)
193193 if resp .StatusCode == 429 {
194194 ra := resp .Header .Get ("Retry-After" )
195+ // read and log a small part of body for debugging
196+ b , _ := io .ReadAll (io .LimitReader (resp .Body , 1024 ))
195197 resp .Body .Close ()
198+ log .Printf ("discord: rate limited (429). Retry-After=%s; body=%s" , ra , strings .TrimSpace (string (b )))
196199 var wait time.Duration
197200 if ra != "" {
198201 // try seconds first
@@ -215,24 +218,33 @@ func send_msg_to_discord(embeds []DiscordEmbed, webhookURL string, username stri
215218
216219 // retry on 5xx server errors
217220 if resp .StatusCode >= 500 && resp .StatusCode < 600 {
221+ // read part of body for debug
222+ b , _ := io .ReadAll (io .LimitReader (resp .Body , 1024 ))
218223 resp .Body .Close ()
224+ log .Printf ("discord: server error %d, body=%s" , resp .StatusCode , strings .TrimSpace (string (b )))
219225 backoff := time .Duration (1 << attempt ) * 500 * time .Millisecond
220226 time .Sleep (backoff )
221227 continue
222228 }
223229
224- // other status codes (2xx or 4xx) - do not retry
225- resp .Body .Close ()
230+ // other status codes (2xx success or 4xx client error) - do not retry
231+ if resp .StatusCode < 200 || resp .StatusCode >= 300 {
232+ b , _ := io .ReadAll (io .LimitReader (resp .Body , 2048 ))
233+ resp .Body .Close ()
234+ log .Printf ("discord: unexpected status %d. body=%s" , resp .StatusCode , strings .TrimSpace (string (b )))
235+ } else {
236+ resp .Body .Close ()
237+ }
226238 break
227239 }
228240
229241 if err != nil {
230- fmt .Printf ("Send discord request false : %v\n " , err )
242+ log .Printf ("discord: request failed : %v" , err )
231243 return
232244 }
233245 // if we exhausted retries, log and continue to next batch
234246 if attempt > maxRetries {
235- fmt .Printf ("Send discord request failed after %d attempts\n " , maxRetries )
247+ log .Printf ("discord: send failed after %d attempts" , maxRetries )
236248 }
237249 }
238250}
0 commit comments