#1: Fix Mojang's API HTTPClient default configuration, make mojang.ResponseError interface not applicable to any type, add handling of some possible network errors

This commit is contained in:
ErickSkrauch
2019-04-21 03:04:03 +03:00
parent a8bbacf8b1
commit 7d1506d0d9
4 changed files with 191 additions and 134 deletions

View File

@@ -1,9 +1,11 @@
package queue
import (
"net"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/elyby/chrly/api/mojang"
@@ -100,15 +102,15 @@ func (ctx *JobsQueue) queueRound() {
}
profiles, err := usernamesToUuids(usernames)
switch err.(type) {
case *mojang.TooManyRequestsError, *mojang.ServerError:
for _, job := range jobs {
job.RespondTo <- nil
}
if err != nil {
defer func() {
for _, job := range jobs {
job.RespondTo <- nil
}
}()
maybeShouldPanic(err)
return
case error:
panic(err)
}
var wg sync.WaitGroup
@@ -146,11 +148,9 @@ func (ctx *JobsQueue) getTextures(uuid string) *mojang.SignedTexturesResponse {
shouldCache := true
result, err := uuidToTextures(uuid, true)
switch err.(type) {
case *mojang.EmptyResponse, *mojang.TooManyRequestsError, *mojang.ServerError:
if err != nil {
maybeShouldPanic(err)
shouldCache = false
case error:
panic(err)
}
if shouldCache && result != nil {
@@ -159,3 +159,25 @@ func (ctx *JobsQueue) getTextures(uuid string) *mojang.SignedTexturesResponse {
return result
}
// Starts to panic if there's an unexpected error
func maybeShouldPanic(err error) {
switch err.(type) {
case mojang.ResponseError:
return
case net.Error:
if err.(net.Error).Timeout() {
return
}
if opErr, ok := err.(*net.OpError); ok && (opErr.Op == "dial" || opErr.Op == "read") {
return
}
if err == syscall.ECONNREFUSED {
return
}
}
panic(err)
}