core

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 5 Imported by: 50

README

Core

Things I wish were built in.

Fuse

A thread-safe one-way switch, used for permanent state changes. The zero value is valid simlar to standard synchronization primitives.

type Server struct {
    msgs chan *Message
    shutdown core.Fuse
}

func NewServer() *Server {
    return &Server{}
}

func (s *Server) Run() {
    for {
        select {
        case msg := <-s.msgs:
            s.Process(msg)
        case <-s.shutdown.Watch():
            return
        }   
    }
}

func (s *Server) DoSomething() {
    if s.shutdown.IsBroken() {
        return errors.New("server closed")
    }
    ...
}

func (s *Server) Shutdown() {
    s.shutdown.Break()
}

Throttle

Golang function throttler.
Similar to debounce, but the first call will execute immediately.
Subsequent calls will always have a minimum duration between executions.

func main() {
    throttle := core.NewThrottle(time.Millisecond * 250)
    
    for i := 0; i < 10; i++ {
        if i == 0 {
            throttle(func() { fmt.Println("hello") })
        } else {
            throttle(func() { fmt.Println("world") })
        }
        time.Sleep(time.Millisecond * 10)
    }
}

Output:

hello (immediately)
world (after 250 ms)

QueueWorker

Execute functions in order of submission.

func main() {
    w := core.NewQueueWorker(QueueWorkerParams{QueueSize: 10})
	w.Submit(func() {
		time.Sleep(time.Second)
		fmt.Printf("hello ")
	})
	w.Submit(func() {
		fmt.Println("world!")	
	})
}

Output:

hello world!

QueuePool

A pool for QueueWorker management, organized by keys. Different worker keys will run in parallel.

func main() {
    p := core.NewQueuePool(QueueWorkerParams{QueueSize: 10})
	p.Submit("worker1", func() {
		time.Sleep(time.Second)
		fmt.Printf(" world")
	})
	p.Submit("worker2", func() {
		fmt.Printf("hello")
	})
	p.Submit("worker1", func() {
		fmt.Println("!")
	})
}

Output:

hello world!

Documentation

Index

Constants

View Source
const (
	DefaultQueueSize = 100
	DefaultCapacity  = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Fuse

type Fuse struct {
	// contains filtered or unexported fields
}

Fuse is a thread-safe one-way switch, used for permanent state changes. Implementation partially borrowed from sync.Once

func (*Fuse) Break added in v0.0.4

func (f *Fuse) Break()

Break breaks the fuse

func (*Fuse) IsBroken added in v0.0.5

func (f *Fuse) IsBroken() bool

IsBroken returns true if the fuse has been broken

func (*Fuse) Once added in v0.0.3

func (f *Fuse) Once(do func())

Once runs the callback and breaks the fuse

func (*Fuse) Watch added in v0.0.4

func (f *Fuse) Watch() <-chan struct{}

Watch returns a channel which will close once the fuse is broken

type QueuePool added in v0.0.6

type QueuePool interface {
	Submit(key string, job func())
	Drain()
	Kill()
}

func NewQueuePool added in v0.0.6

func NewQueuePool(maxWorkers int, params QueueWorkerParams) QueuePool

type QueueWorker added in v0.0.6

type QueueWorker interface {
	Submit(job func())
	Drain()
	Kill()
}

func NewQueueWorker added in v0.0.6

func NewQueueWorker(params QueueWorkerParams) QueueWorker

type QueueWorkerParams added in v0.0.7

type QueueWorkerParams struct {
	QueueSize    int
	DropWhenFull bool
	OnDropped    func()
}

type Throttle

type Throttle func(f func())

Throttle is a function throttler that takes a function as its argument. If ready, it will execute immediately, and it will always wait the specified duration between executions. If multiple functions are added within the same execution window, only the last function added will be executed.

func NewThrottle

func NewThrottle(period time.Duration) Throttle

Jump to

Keyboard shortcuts

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