OpenMix: OpenMix.org

Mix XWP

Universal work pool

A common worker pool

Github

github.com/mix-go/xwp

Installation

go get github.com/mix-go/xwp
Copy the code

Usage

Create a structure to process the task and use type assertion to convert the task data type, for example: I := data.(int)

type Foo struct{}func (t *Foo) Do(data interface{}) {
    // do something
}
Copy the code

Scheduling tasks

  • You can also useRunFUse closures to handle tasks
  • This can be used if you do not want to block executionp.Start()Start the
jobQueue := make(chan interface{}, 200)

p := &xwp.WorkerPool{
    JobQueue:       jobQueue,
    MaxWorkers:     1000,
    InitWorkers:    100,
    MaxIdleWorkers: 100,
    RunI:           &Foo{},
}

go func(a) {
    // Drop the mission
    for i := 0; i < 10000; i++ {
        jobQueue <- i
    }

    // Stop scheduling
    p.Stop()
}()

p.Run() // block wait
Copy the code

Exception handling: Codes executed in the Do method may cause panic exceptions. You can use Recover to obtain abnormal information and record it in the log or perform other processing

func (t *Foo) Do(data interface{}) {
    defer func(a) {
        if err := recover(a); err ! =nil {
            // handle error
        }
    }()
    // do something
}
Copy the code

Check the execution status of Workers: Usually, a timer can be used to print or handle alarms periodically

go func(a) {
    ticker := time.NewTicker(1000 * time.Millisecond)
    for {
        <-ticker.C
        log.Printf("%+v", p.Stat()) // 2021/04/26 14:32:53 &{Active:5 Idle:95 Total:100}
    }
}()
Copy the code

License

Apache License Version 2.0, www.apache.org/licenses/