/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Package rpctimeout implements logic for timeout controlling.
package rpctimeout

import (
	"context"
	"sync/atomic"
	"time"

	"github.com/cloudwego/kitex/pkg/endpoint"
)

type timeoutAdjustKeyType int

// TimeoutAdjustKey is used to adjust the timeout of RPC timeout middleware.
// Deprecated: this value is kept for historical reason and compatibility.
// It should not be used anymore.
const TimeoutAdjustKey timeoutAdjustKeyType = 1

// MiddlewareBuilder builds timeout middleware.
// Deprecated: this method is kept for historical reason and compatibility.
// It should not be used anymore.
func MiddlewareBuilder(moreTimeout time.Duration) endpoint.MiddlewareBuilder {
	return func(mwCtx context.Context) endpoint.Middleware {
		if p, ok := mwCtx.Value(TimeoutAdjustKey).(*time.Duration); ok {
			*p = moreTimeout
		}
		return endpoint.DummyMiddleware
	}
}

// Controls whether `rpcTimeoutMW` should separate ErrRPCTimeout into different errors according to causes.
// Since atomic.Bool is available only since go1.19, use int32 instead.
var globalNeedFineGrainedErrCode int32 = 0

// EnableGlobalNeedFineGrainedErrCode can be used to set global flag, which applies to all clients.
func EnableGlobalNeedFineGrainedErrCode() {
	atomic.StoreInt32(&globalNeedFineGrainedErrCode, 1)
}

// DisableGlobalNeedFineGrainedErrCode can be used to revert the flag, which is useful in tests.
func DisableGlobalNeedFineGrainedErrCode() {
	atomic.StoreInt32(&globalNeedFineGrainedErrCode, 0)
}

// LoadGlobalNeedFineGrainedErrCode is used to load the flag, and return a bool value.
func LoadGlobalNeedFineGrainedErrCode() bool {
	return atomic.LoadInt32(&globalNeedFineGrainedErrCode) == 1
}

// defaultBusinessTimeoutThreshold is used to determine whether a timeout is set by kitex or business.
// If actual DDL + threshold <  Kitex's expected DDL, it's more likely to be set by business code.
var defaultBusinessTimeoutThreshold = time.Millisecond * 50

// SetBusinessTimeoutThreshold sets the threshold for business timeout.
func SetBusinessTimeoutThreshold(t time.Duration) {
	defaultBusinessTimeoutThreshold = t
}

// LoadBusinessTimeoutThreshold is used the load the threshold, and keeps compatibility if in the future
// there's need for business code to modify the value.
func LoadBusinessTimeoutThreshold() time.Duration {
	return defaultBusinessTimeoutThreshold
}
