Skip to content

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

License

Notifications You must be signed in to change notification settings

subbuv26/chanup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
}

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages