Code Monkey home page Code Monkey logo

rr-challenge's Introduction

Hacker Challenge - Multithreading Round-Robin

reward $28 bonusly dollars

Here's the worker that processes our bandwidth stuff. It runs on multiple worker boxes and each box runs 25 threads per Shoryuken worker, so if we have 5 worker boxes mutiplied by 25 threads gives us 125 threads.

class BlastBandwidthWorker
  include Shoryuken::Worker

  shoryuken_options auto_delete: true

  def perform(sqs_message, payload)
    payload = JSON.parse(payload)
    blast   = BlastIndex[payload["blast_id"]]
    user    = blast.user

    BandwidthBlast
      .new(user)
      .send_message(blast_id: blast.id, to: payload["to"], message: blast.body)
  end
end

Here's our send_message method; we store the district's phone numbers (5 per district) in the districts table's meta field. Notice the .sample I'm using here is a random approach to distributing the load across the phone numbers. If you run some test across a histogram you can see the distribution is probably more random than the random generator .sample uses.

  def send_message(blast_id:, to:, message:)
    district = BlastIndex[blast_id].district
    profile  = district.meta_hash[:bandwidth_profile]
    body     = MessageRequest.new

    body.application_id = application_id
    body.to             = [to]
    body.from           = profile[:numbers].sample
    body.text           = message

    res = messaging_client.create_message(account_id, body)

    BlastLog[blast_id: blast_id, to_number: to]
      .update(bandwidth_id: res.data.id)
  end

You are challenged to write a turbine utility that will distribute the elements of an array evenly across mutiple threads. Your utility should take an array as an input and return an element of that array and maintain an even distribution across many threads.

Hint: Utilize redis's ability to do atomic operations and implement a circular queue.

rr-challenge's People

Contributors

hkrdrm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.