增加日志与退出时404问题处理
This commit is contained in:
1 parent
2a2f78c81e
commit
648b274c55
7 files changed
+226
-2
No files matched your search
@@ -4,16 +4,20 @@ import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -144,12 +148,80 @@ func (c *OSSHTTPClient) do(req *http.Request) (*http.Response, error) {
|
||||
if err == nil {
|
||||
return response, nil
|
||||
}
|
||||
diagnostic := diagnoseOSSTransport(req, err)
|
||||
log.Printf(
|
||||
"zhinian-api OSS transport request failed method=%s endpointClass=%s errorClass=%s",
|
||||
diagnostic.Method,
|
||||
diagnostic.EndpointClass,
|
||||
diagnostic.ErrorClass,
|
||||
)
|
||||
if ctxErr := req.Context().Err(); ctxErr != nil {
|
||||
return nil, ctxErr
|
||||
}
|
||||
return nil, &OSSError{Err: errors.New("OSS transport request failed")}
|
||||
}
|
||||
|
||||
type ossTransportDiagnostic struct {
|
||||
Method string
|
||||
EndpointClass string
|
||||
ErrorClass string
|
||||
}
|
||||
|
||||
func diagnoseOSSTransport(req *http.Request, err error) ossTransportDiagnostic {
|
||||
diagnostic := ossTransportDiagnostic{ErrorClass: classifyOSSTransportError(err)}
|
||||
if req == nil {
|
||||
diagnostic.Method = "unknown"
|
||||
diagnostic.EndpointClass = "unknown"
|
||||
return diagnostic
|
||||
}
|
||||
diagnostic.Method = req.Method
|
||||
host := strings.ToLower(req.URL.Hostname())
|
||||
switch {
|
||||
case strings.Contains(host, "-internal.") || strings.HasSuffix(host, "-internal"):
|
||||
diagnostic.EndpointClass = "internal"
|
||||
case strings.HasSuffix(host, ".aliyuncs.com"):
|
||||
diagnostic.EndpointClass = "public"
|
||||
default:
|
||||
diagnostic.EndpointClass = "custom"
|
||||
}
|
||||
return diagnostic
|
||||
}
|
||||
|
||||
func classifyOSSTransportError(err error) string {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return "canceled"
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return "timeout"
|
||||
}
|
||||
var dnsErr *net.DNSError
|
||||
if errors.As(err, &dnsErr) {
|
||||
if dnsErr.IsTimeout {
|
||||
return "dns_timeout"
|
||||
}
|
||||
return "dns"
|
||||
}
|
||||
var tlsErr *tls.CertificateVerificationError
|
||||
if errors.As(err, &tlsErr) {
|
||||
return "tls_certificate"
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, syscall.ECONNRESET):
|
||||
return "connection_reset"
|
||||
case errors.Is(err, syscall.ECONNREFUSED):
|
||||
return "connection_refused"
|
||||
case errors.Is(err, syscall.ENETUNREACH):
|
||||
return "network_unreachable"
|
||||
case errors.Is(err, syscall.EHOSTUNREACH):
|
||||
return "host_unreachable"
|
||||
}
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
return "timeout"
|
||||
}
|
||||
return "transport"
|
||||
}
|
||||
|
||||
func ossObjectURL(endpoint, bucket, key, subresource string) (*url.URL, error) {
|
||||
endpoint = strings.TrimSpace(endpoint)
|
||||
if !strings.Contains(endpoint, "://") {
|
||||
|
||||
Reference in new issue
Block a user