github.com/google/go-github/v66@v66.0.0/github/rate_limit.go (about) 1 // Copyright 2023 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import "context" 9 10 // RateLimitService provides access to rate limit functions in the GitHub API. 11 type RateLimitService service 12 13 // Rate represents the rate limit for the current client. 14 type Rate struct { 15 // The number of requests per hour the client is currently limited to. 16 Limit int `json:"limit"` 17 18 // The number of remaining requests the client can make this hour. 19 Remaining int `json:"remaining"` 20 21 // The time at which the current rate limit will reset. 22 Reset Timestamp `json:"reset"` 23 } 24 25 func (r Rate) String() string { 26 return Stringify(r) 27 } 28 29 // RateLimits represents the rate limits for the current client. 30 type RateLimits struct { 31 // The rate limit for non-search API requests. Unauthenticated 32 // requests are limited to 60 per hour. Authenticated requests are 33 // limited to 5,000 per hour. 34 // 35 // GitHub API docs: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting 36 Core *Rate `json:"core"` 37 38 // The rate limit for search API requests. Unauthenticated requests 39 // are limited to 10 requests per minutes. Authenticated requests are 40 // limited to 30 per minute. 41 // 42 // GitHub API docs: https://docs.github.com/en/rest/search#rate-limit 43 Search *Rate `json:"search"` 44 45 // GitHub API docs: https://docs.github.com/en/graphql/overview/resource-limitations#rate-limit 46 GraphQL *Rate `json:"graphql"` 47 48 // GitHub API dos: https://docs.github.com/en/rest/rate-limit 49 IntegrationManifest *Rate `json:"integration_manifest"` 50 51 SourceImport *Rate `json:"source_import"` 52 CodeScanningUpload *Rate `json:"code_scanning_upload"` 53 ActionsRunnerRegistration *Rate `json:"actions_runner_registration"` 54 SCIM *Rate `json:"scim"` 55 DependencySnapshots *Rate `json:"dependency_snapshots"` 56 CodeSearch *Rate `json:"code_search"` 57 AuditLog *Rate `json:"audit_log"` 58 } 59 60 func (r RateLimits) String() string { 61 return Stringify(r) 62 } 63 64 // Get returns the rate limits for the current client. 65 // 66 // GitHub API docs: https://docs.github.com/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user 67 // 68 //meta:operation GET /rate_limit 69 func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, error) { 70 req, err := s.client.NewRequest("GET", "rate_limit", nil) 71 if err != nil { 72 return nil, nil, err 73 } 74 75 response := new(struct { 76 Resources *RateLimits `json:"resources"` 77 }) 78 79 // This resource is not subject to rate limits. 80 ctx = context.WithValue(ctx, bypassRateLimitCheck, true) 81 resp, err := s.client.Do(ctx, req, response) 82 if err != nil { 83 return nil, resp, err 84 } 85 86 if response.Resources != nil { 87 s.client.rateMu.Lock() 88 if response.Resources.Core != nil { 89 s.client.rateLimits[CoreCategory] = *response.Resources.Core 90 } 91 if response.Resources.Search != nil { 92 s.client.rateLimits[SearchCategory] = *response.Resources.Search 93 } 94 if response.Resources.GraphQL != nil { 95 s.client.rateLimits[GraphqlCategory] = *response.Resources.GraphQL 96 } 97 if response.Resources.IntegrationManifest != nil { 98 s.client.rateLimits[IntegrationManifestCategory] = *response.Resources.IntegrationManifest 99 } 100 if response.Resources.SourceImport != nil { 101 s.client.rateLimits[SourceImportCategory] = *response.Resources.SourceImport 102 } 103 if response.Resources.CodeScanningUpload != nil { 104 s.client.rateLimits[CodeScanningUploadCategory] = *response.Resources.CodeScanningUpload 105 } 106 if response.Resources.ActionsRunnerRegistration != nil { 107 s.client.rateLimits[ActionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration 108 } 109 if response.Resources.SCIM != nil { 110 s.client.rateLimits[ScimCategory] = *response.Resources.SCIM 111 } 112 if response.Resources.DependencySnapshots != nil { 113 s.client.rateLimits[DependencySnapshotsCategory] = *response.Resources.DependencySnapshots 114 } 115 if response.Resources.CodeSearch != nil { 116 s.client.rateLimits[CodeSearchCategory] = *response.Resources.CodeSearch 117 } 118 if response.Resources.AuditLog != nil { 119 s.client.rateLimits[AuditLogCategory] = *response.Resources.AuditLog 120 } 121 s.client.rateMu.Unlock() 122 } 123 124 return response.Resources, resp, nil 125 }