Help us shape the future of Go! Take the 2026 Go Developer Survey

corehandlers

package
v1.0.80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 15 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddHostExecEnvUserAgentHandler = request.NamedHandler{
	Name: "core.AddHostExecEnvUserAgentHandler",
	Fn: func(r *request.Request) {
		v := os.Getenv(execEnvVar)
		if len(v) == 0 {
			return
		}

		request.AddToUserAgent(r, execEnvUAKey+"/"+v)
	},
}

AddHostExecEnvUserAgentHandler is a request handler appending the SDK's execution environment to the user agent.

If the environment variable BYTEPLUS_EXECUTION_ENV is set, its value will be appended to the user agent string.

View Source
var AddRetryInfoHeaderHandler = request.NamedHandler{
	Name: "core.AddRetryInfoHeaderHandler",
	Fn: func(r *request.Request) {
		r.HTTPRequest.Header.Set(retryInvocationIDHeader, r.InvocationID)
		r.HTTPRequest.Header.Set(retryAttemptHeader,
			fmt.Sprintf("attempt=%d; max=%d", r.RetryCount+1, r.MaxRetries()+1))
	},
}

AddRetryInfoHeaderHandler annotates every HTTP attempt with the request's invocation id and its attempt/max-attempt count. It runs on every retry (it is registered on Handlers.Sign, which is re-run for each attempt), so the attempt count in the header always reflects the current try.

View Source
var AfterRetryHandler = request.NamedHandler{
	Name: "core.AfterRetryHandler",
	Fn: func(r *request.Request) {

		logger := r.Config.Logger

		if r.Retryable == nil || byteplus.BoolValue(r.Config.EnforceShouldRetryCheck) {
			r.Retryable = byteplus.Bool(r.ShouldRetry(r))
		}

		if r.WillRetry() {
			logger.DebugByLevel(byteplus.LogDebugWithRequestRetries, "[Retry] retry attempt", r.RetryCount+1, "of", r.MaxRetries(), "for request", r.RequestID, "with error", r.Error)
			r.RetryDelay = r.RetryRules(r)

			if sleepFn := r.Config.SleepDelay; sleepFn != nil {
				logger.DebugByLevel(byteplus.LogDebugWithRequestRetries, "[Retry] custom sleepFn, sleeping for", r.RetryDelay)

				sleepFn(r.RetryDelay)
			} else if err := byteplus.SleepWithContext(r.Context(), r.RetryDelay); err != nil {
				r.Error = bytepluserr.New(request.CanceledErrorCode,
					"request context canceled", err)
				r.Retryable = byteplus.Bool(false)
				logger.DebugByLevel(byteplus.LogDebugWithRequestRetries, "[Retry] canceled sleep", r.Error)
				return
			}

			if r.IsErrorExpired() {
				r.Config.Credentials.Expire()
			}

			r.RetryCount++
			r.Error = nil
		}
	}}

AfterRetryHandler performs final checks to determine if the request should be retried and how long to delay.

View Source
var BuildContentLengthHandler = request.NamedHandler{Name: "core.BuildContentLengthHandler", Fn: func(r *request.Request) {
	var length int64

	if slength := r.HTTPRequest.Header.Get("Content-Length"); slength != "" {
		length, _ = strconv.ParseInt(slength, 10, 64)
	} else {
		if r.Body != nil {
			var err error
			length, err = byteplus.SeekerLen(r.Body)
			if err != nil {
				r.Error = bytepluserr.New(request.ErrCodeSerialization, "failed to get request byteplusbody's length", err)
				return
			}
		}
	}

	if length > 0 {
		r.HTTPRequest.ContentLength = length
		r.HTTPRequest.Header.Set("Content-Length", fmt.Sprintf("%d", length))
	} else {
		r.HTTPRequest.ContentLength = 0
		r.HTTPRequest.Header.Del("Content-Length")
	}
}}

BuildContentLengthHandler builds the content length of a request based on the byteplusbody, or will use the HTTPRequest.Header's "Content-Length" if defined. If unable to determine request byteplusbody length and no "Content-Length" was specified it will panic.

The Content-Length will only be added to the request if the length of the byteplusbody is greater than 0. If the byteplusbody is empty or the current `Content-Length` header is <= 0, the header will also be stripped.

View Source
var CustomerRequestHandler = request.NamedHandler{
	Name: "core.CustomerRequestHandler",
	Fn: func(r *request.Request) {
		if r.Config.ExtendHttpRequest != nil {
			r.Config.ExtendHttpRequest(r.Context(), r.HTTPRequest)
		}

		if r.Config.ExtendHttpRequestWithMeta != nil {
			r.Config.ExtendHttpRequestWithMeta(r.Context(), r.HTTPRequest, custom.RequestMetadata{
				ServiceName: r.ClientInfo.ServiceName,
				Version:     r.ClientInfo.APIVersion,
				Action:      r.Operation.Name,
				HttpMethod:  r.Operation.HTTPMethod,
				Region:      *r.Config.Region,
			})
		}
	},
}
View Source
var SDKVersionUserAgentHandler = request.NamedHandler{
	Name: "core.SDKVersionUserAgentHandler",
	Fn: request.MakeAddToUserAgentHandler(byteplus.SDKName, byteplus.SDKVersion,
		runtime.Version(), runtime.GOOS, runtime.GOARCH),
}

SDKVersionUserAgentHandler is a request handler for adding the SDK Version to the user agent.

View Source
var SendHandler = request.NamedHandler{
	Name: "core.SendHandler",
	Fn: func(r *request.Request) {
		sender := sendFollowRedirects
		if r.DisableFollowRedirects {
			sender = sendWithoutFollowRedirects
		}

		if request.NoBody == r.HTTPRequest.Body {

			reqOrig, reqCopy := r.HTTPRequest, *r.HTTPRequest
			reqCopy.Body = nil
			r.HTTPRequest = &reqCopy
			defer func() {
				r.HTTPRequest = reqOrig
			}()
		}

		var err error
		r.HTTPResponse, err = sender(r)
		if err != nil {
			handleSendError(r, err)
		}
	},
}

SendHandler is a request handler to send service request using HTTP client.

View Source
var ValidateEndpointHandler = request.NamedHandler{Name: "core.ValidateEndpointHandler", Fn: func(r *request.Request) {
	if r.ClientInfo.SigningRegion == "" && byteplus.StringValue(r.Config.Region) == "" && r.Config.DynamicCredentials == nil {
		r.Error = byteplus.ErrMissingRegion
	} else if r.ClientInfo.Endpoint == "" {
		r.Error = byteplus.ErrMissingEndpoint
	}
}}

ValidateEndpointHandler is a request handler to validate a request had the appropriate Region and Endpoint set. Will set r.Error if the endpoint or region is not valid.

View Source
var ValidateParametersHandler = request.NamedHandler{Name: "core.ValidateParametersHandler", Fn: func(r *request.Request) {
	if !r.ParamsFilled() {
		return
	}

	if v, ok := r.Params.(request.Validator); ok {
		if err := v.Validate(); err != nil {
			r.Error = err
		}
	}
}}

ValidateParametersHandler is a request handler to validate the input parameters. Validating parameters only has meaning if done prior to the request being sent.

View Source
var ValidateReqSigHandler = request.NamedHandler{
	Name: "core.ValidateReqSigHandler",
	Fn: func(r *request.Request) {

		if r.Config.Credentials == credentials.AnonymousCredentials {
			return
		}

		signedTime := r.Time
		if !r.LastSignedAt.IsZero() {
			signedTime = r.LastSignedAt
		}

		if signedTime.Add(5 * time.Minute).After(time.Now()) {
			return
		}

		fmt.Println("request expired, resigning")
		r.Sign()
	},
}

ValidateReqSigHandler is a request handler to ensure that the request's signature doesn't expire before it is sent. This can happen when a request is built and signed significantly before it is sent. Or significant delays occur when retrying requests that would cause the signature to expire.

View Source
var ValidateResponseHandler = request.NamedHandler{Name: "core.ValidateResponseHandler", Fn: func(r *request.Request) {
	if r.HTTPResponse.StatusCode == 0 || r.HTTPResponse.StatusCode >= 300 {

		r.Error = bytepluserr.New("UnknownError", "unknown error", nil)
	}
}}

ValidateResponseHandler is a request handler to validate service response.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL