github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/ratelimit_test.go (about)

     1  package discordgo
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  // This test takes ~2 seconds to run
    12  func TestRatelimitReset(t *testing.T) {
    13  	rl := NewRatelimiter()
    14  
    15  	sendReq := func(endpoint string) {
    16  		bucket := rl.LockBucket(endpoint)
    17  
    18  		headers := http.Header(make(map[string][]string))
    19  
    20  		headers.Set("X-RateLimit-Remaining", "0")
    21  		// Reset for approx 2 seconds from now
    22  		headers.Set("X-RateLimit-Reset", fmt.Sprint(float64(time.Now().Add(time.Second*2).UnixNano())/1e9))
    23  		headers.Set("Date", time.Now().Format(time.RFC850))
    24  
    25  		err := bucket.Release(headers)
    26  		if err != nil {
    27  			t.Errorf("Release returned error: %v", err)
    28  		}
    29  	}
    30  
    31  	sent := time.Now()
    32  	sendReq("/guilds/99/channels")
    33  	sendReq("/guilds/55/channels")
    34  	sendReq("/guilds/66/channels")
    35  
    36  	sendReq("/guilds/99/channels")
    37  	sendReq("/guilds/55/channels")
    38  	sendReq("/guilds/66/channels")
    39  
    40  	// We hit the same endpoint 2 times, so we should only be ratelimited 2 second
    41  	// And always less than 4 seconds (unless you're on a stoneage computer or using swap or something...)
    42  	if time.Since(sent) >= time.Second && time.Since(sent) < time.Second*4 {
    43  		t.Log("OK", time.Since(sent))
    44  	} else {
    45  		t.Error("Did not ratelimit correctly, got:", time.Since(sent))
    46  	}
    47  }
    48  
    49  // This test takes ~1 seconds to run
    50  func TestRatelimitGlobal(t *testing.T) {
    51  	rl := NewRatelimiter()
    52  
    53  	sendReq := func(endpoint string) {
    54  		bucket := rl.LockBucket(endpoint)
    55  
    56  		headers := http.Header(make(map[string][]string))
    57  
    58  		headers.Set("X-RateLimit-Global", "1")
    59  		// Reset for approx 1 seconds from now
    60  		headers.Set("X-RateLimit-Reset-After", "1")
    61  
    62  		err := bucket.Release(headers)
    63  		if err != nil {
    64  			t.Errorf("Release returned error: %v", err)
    65  		}
    66  	}
    67  
    68  	sent := time.Now()
    69  
    70  	// This should trigger a global ratelimit
    71  	sendReq("/guilds/99/channels")
    72  	time.Sleep(time.Millisecond * 100)
    73  
    74  	// This shouldn't go through in less than 1 second
    75  	sendReq("/guilds/55/channels")
    76  
    77  	if time.Since(sent) >= time.Second && time.Since(sent) < time.Second*2 {
    78  		t.Log("OK", time.Since(sent))
    79  	} else {
    80  		t.Error("Did not ratelimit correctly, got:", time.Since(sent))
    81  	}
    82  }
    83  
    84  func BenchmarkRatelimitSingleEndpoint(b *testing.B) {
    85  	rl := NewRatelimiter()
    86  	for i := 0; i < b.N; i++ {
    87  		sendBenchReq("/guilds/99/channels", rl)
    88  	}
    89  }
    90  
    91  func BenchmarkRatelimitParallelMultiEndpoints(b *testing.B) {
    92  	rl := NewRatelimiter()
    93  	b.RunParallel(func(pb *testing.PB) {
    94  		i := 0
    95  		for pb.Next() {
    96  			sendBenchReq("/guilds/"+strconv.Itoa(i)+"/channels", rl)
    97  			i++
    98  		}
    99  	})
   100  }
   101  
   102  // Does not actually send requests, but locks the bucket and releases it with made-up headers
   103  func sendBenchReq(endpoint string, rl *RateLimiter) {
   104  	bucket := rl.LockBucket(endpoint)
   105  
   106  	headers := http.Header(make(map[string][]string))
   107  
   108  	headers.Set("X-RateLimit-Remaining", "10")
   109  	headers.Set("X-RateLimit-Reset", fmt.Sprint(float64(time.Now().UnixNano())/1e9))
   110  	headers.Set("Date", time.Now().Format(time.RFC850))
   111  
   112  	bucket.Release(headers)
   113  }