Giter Site home page Giter Site logo

daycare's Introduction

Daycare: Thread-Safe deferring registry

Daycare is a cross between a simple key-value map and futures.

The daycare Registry allows concurrent access via Register(key, value) and Lookup(key).

Calls to "Lookup" will block until the key is registered or a call is made to Stop(), at which point all routines waiting on a Lookup will be notified that the key never resolved.

Example:

	package main

	import (
		"fmt"
		"time"

		"github.com/kfsone/daycare"
	)

	func main() {
		r := daycare.NewRegistry()

		r.Start()

		var solution interface{}

		// put something into the registry.
		r.Register("distraction", "squirrel")

		// A worker trying to look up 'answer'.
		go func() {
			answer, valid, err := r.Lookup("answer")
			if err != nil {
				panic(err)
			}
			if !valid {
				panic("no response")
			}
			fmt.Println("got the answer")
			solution = answer
		}()

		// A worker looking for something we'll never register.
		go func() {
			_, valid, err := r.Lookup("suggestions")
			if err != nil {
				panic(err)
			}
			if !valid {
				fmt.Println("suggestions stayed empty, like we wanted.")
			} else {
				panic("there's a suggestion in the box.")
			}
		}()

		// A worker that will register 'question' after a few moments.
		go func() {
			time.Sleep(1200 * time.Millisecond)
			r.Register("question", func() int { return 6 * 7 })
		}()

		// A worker that will register "answer" after more moments.
		go func() {
			time.Sleep(2500 * time.Millisecond)
			r.Register("answer", 42)
		}()

		// Time passes...
		for i := 0; i < 3; i++ {
			time.Sleep(1 * time.Second)
			fmt.Printf("Answer? %v\n", solution)
		}

		r.Stop()

		// Allow our blocked worker to get their notification.
		time.Sleep(100 * time.Millisecond)

		registered, err := r.Values()
		if err != nil {
			panic(err)
		}
		fmt.Printf("registry: %#+v\n", registered)
	}

daycare's People

Contributors

kfsone avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.