/chanup

A package that has API to deliver a complex use case of go channels

Primary LanguageGoApache License 2.0Apache-2.0

GoDoc Go Report Card License

ChanUp Channels

A Wrapper on top of go channels which supports one complex use case. A ChanUp channel buffer has a length of One. ChanUp channel can be used to make producer and consumer of channel independent of each other. The execution flow of no process gets blocked.

Functionality

  • ChanUp channel never blocks producer or consumer.
  • A producer Puts a value if channel is empty
  • A producer Updates channel with new value if it is holding a stale value.
  • A consumer gets a value if available.

Installation

go get github.com/subbuv26/chanup

Usage

Example 1:

package main

import (
	"fmt"
	"github.com/subbuv26/chanup"
)

type testType struct {
	a int
	s string
}

func main() {
	ch := chanup.GetChan()
	status := ch.Put(testType{
		a: 10,
		s: "Sample",
	})
	if status == chanup.FAILED {
		// Log
	}
	
	testValue := testType{
		a: 20,
		s: "Sample2",
	}
	status = ch.Update(testValue)

	if status != chanup.UPDATE {
		// Log
	}

	val := ch.Get()
	if val == nil {
		// Log
	}
	tv := val.(testType)
	fmt.Println(tv)
}

Example 2:

package main

import (
	"fmt"
	"github.com/subbuv26/chanup"
	"time"
)

type testType struct {
	a int
	s string
}

func producer(chp *chanup.ChanUp) {
	tv := testType{
		100,
		"sample",
	}
	for {
		time.Sleep(time.Second)
		chp.Update(tv)
		tv.a += 1
	}
}

func consumer(chc *chanup.ChanUp) {
	for {
		if val := chc.Get(); val != nil {
			tv := val.(testType)
			fmt.Println(tv)
			time.Sleep(time.Second * 5)
		}
	}
}

func main() {
	ch := chanup.GetChan()

	go producer(ch)

	consumer(ch)
}