xlump

package
v0.25.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

GoKit - xlump

Lump kits for Golang development.

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xlump"
)

How it work

//             .---------.                   .---------.                    .---------.
// Task.Add -> |         |                   |         |                    |         |
// Task.Add -> |         |    Worker.Work    |         |                    |         |
// Task.Add -> | TaskIn  | -> Worker.Work -> | TaskOut | -> Merger.Merge -> | TaskSum |
// Task.Add -> |         |    Worker.Work    |         |                    |         |
// Task.Add -> |         |                   |         |                    |         |
//             '---------'                   '---------'                    '---------'

Documentation

Visit the docs on GoDoc

Example

Plus 1 to every number and sum to total
// Worker: Plus 1 to every number
mathPlus := func(t xlump.Task) xlump.Task {
    return t.(int) + 1
}

// Merger: Sum to total
mathSum := func(r xlump.Task, t xlump.Task) xlump.Task {
    return r.(int) + t.(int)
}

// New a work queue
wq := xlump.New(100)
// Set Worker func
wq.SetWorker(mathPlus, 10)
// Set Merger func
wq.SetMerger(mathSum, 0)

// Add number to queue
for i := 0; i < 1000; i++ {
    wq.Add(i)
}

// Wait for result and print
result := wq.Wait()
fmt.Println("sum is:", result)

License

Copyright 2012-2024 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Author

func Author() string

Author returns package author

func License

func License() string

License returns package license

func Version

func Version() string

Version returns package version

Types

type Merge

type Merge func(Task, Task) Task

Merge merge worker result

type Queue

type Queue struct {
	TaskIn   chan Task
	TaskOut  chan Task
	TaskSum  chan Task
	WorkWait sync.WaitGroup
}

Queue is the work queue

func New

func New(bufferSize int) *Queue

New returns new work queue

func (*Queue) Add

func (q *Queue) Add(task Task)

Add add task to queue

func (*Queue) SetMerger

func (q *Queue) SetMerger(merge Merge, result Task) *Queue

SetMerger start merger to do merge

func (*Queue) SetWorker

func (q *Queue) SetWorker(work Work, number int) *Queue

SetWorker start worker to do work

func (*Queue) Wait

func (q *Queue) Wait() Task

Wait close in queue and wait for result

type Task

type Task interface{}

Task is task put to queue

type Work

type Work func(Task) Task

Work work with the task

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL