Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
github.com/karrick/gocjq
concurrent job queue for go
It is often convenient to partition a job into multiple steps, and have them run concurrently, like a car factory might have different stages in a manufactoring facility.
It is also convenient to specify how many workers are desired at each stage of the process. For one stage you may need 5 workers, but for another stage you may want between 25 and 50.
This library, go concurent job queues, gocjq, makes setting up these sort of examples quite easy. There are two interfaces, namely an enqueue and dequeue facility.
In this example, it is important to ensure the job state does not have an error prior to commencing the next stage. If the job already has an error, then skip following steps.
type someJob struct {
a, b float64
err error
}
func (self *someJob) Add() {
if self.err == nil {
self.a += self.b
}
}
func (self *someJob) Divide() {
if self.err == nil {
if self.b != 0 {
self.err = fmt.Errorf("divide by zero")
} else {
self.a /= self.b
}
}
}
func main() {
queue, err := gocjq.NewQueue(
gocjq.Stage(gocjq.Method("Divide"), gocjq.Min(4), gocjq.Max(64)),
gocjq.Stage(gocjq.Method("Add"), gocjq.Min(32)))
if err != nil {
log.Fatal(err)
}
defer queue.Quit()
input := queue.Input()
go func() {
input <- &someJob{a: 13, b: 42}
}()
v := <- queue.Output()
val := v.(*someJob)
if val.err != nil {
log.Printf("[ERROR] Actual: %#v; Expected: %#v", val.err, nil)
}
if val.a != (13/42)+42 {
log.Printf("[ERROR] Actual: %#v; Expected: %#v", val.a, (13/42)+42)
}
}
Sometimes there are no actions to take for a job after processing is complete. This is often the case when the final stage of a job has some sort of side effect, and the completed jobs can be discarded.
In this example, a job queue sump is created to drain completed jobs.
type someJob struct {
a, b int
err error
}
func (self *someJob) Add() {
if err == nil {
self.a += self.b
}
}
func (self *someJob) Divide() {
if err == nil {
if self.b != 0 {
self.err = fmt.Errorf("divide by zero")
} else {
self.a /= self.b
}
}
}
func (self *someJob) Print() {
if err == nil {
fmt.Println("job result: ", self.a)
} else {
fmt.Println("job error: ", self.err)
}
}
func main() {
queue, err := gocjq.NewQueue(
gocjq.Stage(gocjq.Method("Divide"), gocjq.Min(4)),
gocjq.Stage(gocjq.Method("Add")),
gocjq.Stage(gocjq.Method("Print"), gocjq.Min(2)),
gocjq.OutputSump())
if err != nil {
log.Fatal(err)
}
defer queue.Quit()
input := queue.Input()
jobSent := make(chan struct{})
go func() {
input <- &someJob{a: 13, b: 42}
jobSent <- struct{}{}
}()
<-jobSent
queue.Quit()
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.