Skip to content

Batch Mojang API requests using dataloader pattern#154

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/chunk-requests-to-mojang
Draft

Batch Mojang API requests using dataloader pattern#154
Copilot wants to merge 3 commits intomainfrom
copilot/chunk-requests-to-mojang

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 27, 2025

Mojang's API provides bulk endpoints that accept up to 10 requests. Individual calls were inefficient and hitting rate limits unnecessarily.

Implementation

Dataloader pattern with goroutines:

  • Two background goroutines collect requests from buffered channels
  • Batch triggers on 10 requests or 50ms timeout, whichever comes first
  • POST /profiles/minecraft bulk endpoint for usernames (up to 10)
  • UUID requests batched but use individual calls (no bulk endpoint exists)

API modifications:

// Before: direct HTTP call per request
func (m *Mojang) GetAccountByUsername(ctx context.Context, username string) (domain.Account, error) {
    return m.getProfile(ctx, fmt.Sprintf("https://api.mojang.com/users/profiles/minecraft/%s", username))
}

// After: enqueue to batch processor
func (m *Mojang) GetAccountByUsername(ctx context.Context, username string) (domain.Account, error) {
    responseChan := make(chan usernameResponse, 1)
    req := usernameRequest{username: username, response: responseChan}
    
    select {
    case m.usernameRequestChan <- req:
    case <-ctx.Done():
        return domain.Account{}, ctx.Err()
    }
    
    select {
    case resp := <-responseChan:
        return resp.account, resp.err
    case <-ctx.Done():
        return domain.Account{}, ctx.Err()
    }
}

Fallback handling:

  • Bulk request failures automatically retry as individual calls
  • Case-insensitive username matching (Minecraft usernames are case-insensitive)

Lifecycle management:

  • Shutdown() method for graceful cleanup
  • sync.WaitGroup ensures goroutines complete before shutdown
Original prompt

Chunk requests to the mojang account provider by collecting them and making a request towards their bulk endpoints (one for username and one for uuid). Use a dataloader approach where the get single methods put a request onto a queue (one for uuid and one for name). These channels are listened to by two goroutines that should fire a bulk request once they hit 10 requests or a timeout is reached. The single calls should then be notified of the response (or error) and handle it accordingly


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 27, 2025 11:28
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
… context comments

Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement chunked requests to Mojang account provider Batch Mojang API requests using dataloader pattern Nov 27, 2025
Copilot AI requested a review from Amund211 November 27, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants