package debug

import (
	"container/ring"
	"context"
	"time"
)

func Start(ctx context.Context) {
	go startReceiveConsuming(ctx)
}

type Consuming struct {
	Auth  time.Duration
	Data  time.Duration
	Proxy time.Duration
	Total time.Duration
}

var ConsumingCh = make(chan Consuming, 1024)
var consumingList = ring.New(3000)

func startReceiveConsuming(ctx context.Context) {
	InitConsumingList()
	for {
		select {
		case <-ctx.Done():
			return
		case c := <-ConsumingCh:
			consumingList.Value = c
			consumingList = consumingList.Next()
		}
	}
}

func InitConsumingList() {
	for i := 0; i < consumingList.Len(); i++ {
		consumingList.Value = nil
		consumingList = consumingList.Next()
	}
}

func ConsumingList() []Consuming {
	consuming := make([]Consuming, 0, consumingList.Len())
	consumingList.Do(func(value interface{}) {
		if value != nil {
			consuming = append(consuming, value.(Consuming))
		}
	})
	return consuming
}
