Skip to content

[BUG] 并发度限流运行状态异常 #599

@kongchengzhuge

Description

@kongchengzhuge

Issue Description

Type: bug report

Describe what happened

在使用并发度限流过程中, 发现限流行为异常, 运行仓库中自带的Example时,并发度状态并不符合预期

运行下面Example,期望并发度结果为3(参数为true有两个并发度,参数为false有一个并发度),但是结果却是4

package main

import (
	"log"
	"math/rand"
	"time"

	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/alibaba/sentinel-golang/core/base"
	"github.com/alibaba/sentinel-golang/core/config"
	"github.com/alibaba/sentinel-golang/core/hotspot"
	"github.com/alibaba/sentinel-golang/core/stat"
	"github.com/alibaba/sentinel-golang/logging"
)

func main() {
	conf := config.NewDefaultConfig()
	// for testing, logging output to console
	conf.Sentinel.Log.Logger = logging.NewConsoleLogger()
	err := sentinel.InitWithConfig(conf)
	if err != nil {
		log.Fatal(err)
	}
	testKey := "testKey"

	// the max concurrency is 8
	_, err = hotspot.LoadRules([]*hotspot.Rule{
		{
			Resource:      "abc",
			MetricType:    hotspot.Concurrency,
			ParamIndex:    0,
			ParamKey:      testKey,
			Threshold:     2,
			DurationInSec: 0,
		},
	})
	if err != nil {
		log.Fatalf("Unexpected error: %+v", err)
		return
	}

	go func() {
		node := stat.GetOrCreateResourceNode("abc", base.ResTypeCommon)
		for {
			logging.Info("[HotSpot Concurrency] currentConcurrency", "currentConcurrency", node.CurrentConcurrency())
			time.Sleep(time.Duration(100) * time.Millisecond)
		}
	}()

	logging.Info("[HotSpot Concurrency] Sentinel Go hot-spot param flow control demo is running. You may see the pass/block metric in the metric log.")
	for i := 0; i < 10; i++ {
		go func() {
			for {
				e, b := sentinel.Entry("abc", sentinel.WithArgs(true, rand.Uint32()%30))
				if b != nil {
					// Blocked. We could get the block reason from the BlockError.
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
				} else {
					// Passed, wrap the logic here.
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
					// Be sure the entry is exited finally.
					e.Exit()
				}

			}
		}()
	}

	for {
		e, b := sentinel.Entry("abc",
			sentinel.WithArgs(false, uint32(9)))
		if b != nil {
			// Blocked. We could get the block reason from the BlockError.
			time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
		} else {
			// Passed, wrap the logic here.
			time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
			// Be sure the entry is exited finally.
			e.Exit()
		}
	}
}

Describe what you expected to happen

期望并发度为3,但是结果如下
{"timestamp":"2025-08-03 23:41:09.930","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":0} {"timestamp":"2025-08-03 23:41:09.930","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":3} {"timestamp":"2025-08-03 23:41:09.930","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":3} {"timestamp":"2025-08-03 23:41:09.930","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":3} {"timestamp":"2025-08-03 23:41:09.930","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":3} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":2} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:10.1030","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":3} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4} {"timestamp":"2025-08-03 23:41:11.1130","caller":"hotspot_params_concurrency_example.go:59","logLevel":"INFO","msg":"[HotSpot Concurrency] currentConcurrency","currentConcurrency":4}

How to reproduce it (as minimally and precisely as possible)

  1. 直接编译运行上述Example或直接运行'example/hotspot_param_flow/concurrency'目录下示例

Tell us your environment

Anything else we need to know?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/flow-hotspotIssues or PRs related to hotspot flow controlhelp wantedExtra attention is neededkind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions