Giter Site home page Giter Site logo

go-tasks's Introduction

Go Tasks

The issue with Go routines and their use in tasks (in sneaker bots) is stopping them when a user requests it. As you know modern bots have stop functions for each task, which sparked me to create this for Go.

Use of channels

I have used channels to signal a go routine to stop. Two new channels are created when you start a task:

  • TaskIDStop
  • TaskIDStopped

These channels are unique. They are then stored in an array, known as "taskArray". I use a struct to form this array, which consists of your Task ID and your two unique channels.

Starting the task

To start a task, you would create both channels and a Task ID. This item would then be appended to taskArray, and then you call your function:

go Task(task.taskIDStop, task.taskIDStopped, &waitgroup)

After you have started your task, you can now stop it at any time.

Note task.taskIDStop and task.taskIDStopped are chan struct{}.

Calling the stop function

To stop the task you call:

stop(TaskID)

Beneath the hood, this is:

Find index of TaskID in taskArray (i)
Close the taskIDStop channel (i)
Wait for the taskIDStopped channel (i) to close upon the go routine exit
Return task stopped to UI
Remove task from taskArray

This finds the specified task in your task array, and the related channels. It then closes the taskIDStop channel.

Handling the stop

To know when the channel has closed, for each operation in your function you would check using a select statement, like so:

defer close(taskIDStopped)

Select {
default:
 // ATC, Submit checkout etc
case <-taskIDStop:
 waitgroup.Done()
 return
 //Task exits
 //Closes taskIDStopped upon exit
}

You must defer the closing of the taskIDStopped channel. Defering the closing will allow it to close the channel when your go routine exits, therefore signaling your go routine has stopped successfully.

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.