Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect ratio tracking with multiple BitTorrent clients #81

Open
Psmths opened this issue May 9, 2024 · 0 comments
Open

Incorrect ratio tracking with multiple BitTorrent clients #81

Psmths opened this issue May 9, 2024 · 0 comments

Comments

@Psmths
Copy link

Psmths commented May 9, 2024

I have encountered an issue with ratio tracking in the event that users are seeding from multiple BitTorrent clients. I think that this is caused by ratio for each torrent being tracked only by userId, and conflicting information between multiple clients under the same userId were causing the upload/download to increase indefinitely.

I was able to fix this issue by adding a peerId field to the schema:

const Progress = new mongoose.Schema({
  infoHash: String,
  userId: mongoose.Schema.ObjectId,
  peerId: String,
  uploaded: {
    session: Number,
    total: Number,
  },
  downloaded: {
    session: Number,
    total: Number,
  },
  left: Number,
});

And updating the announce API endpoint as follows:

const peerId = params.peer_id // Grabbing the peer ID from the announce query
const prevProgressRecord = await Progress.findOne({
    userId: user._id,
    peerId: peerId,
    infoHash,
}).lean();

...

await Progress.findOneAndUpdate(
    { userId: user._id, peerId: peerId, infoHash },
    {
      $set: {
        userId: user._id,
        infoHash,
        uploaded: {
          session: uploaded,
          total:
            (prevProgressRecord?.uploaded?.total ?? 0) + uploadDeltaSession,
        },
        left: Number(params.left),
      },
    },
    { upsert: true }
  );

Since making these changes it seems that ratio has been tracked correctly.

A possible issue with this is that the Peer ID changes when a BitTorrent client is restarted, and I believe some clients also change their Peer ID during runtime occasionally which could lead to a large amount of entries over time.

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

No branches or pull requests

1 participant