Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/ async-wire Public archive
forked from google/wire

A fork of Wire for asynchronous graphs

License

Notifications You must be signed in to change notification settings

deliveroo/async-wire

 
 

Repository files navigation

async-wire: A fork of wire for asynchronous dependency graphs

async-wire extends google/wire by introducing a new provider type called wire.AsyncFunc. Functions wrapped by AsyncFunc are executed in a Goroutine. To synchronise inputs / outputs between providers, channels are used.

Project goals

  1. First class support for asynchronous dependency graphs
  2. Compatibility with existing Wire features

Roadmap

  • Add missing wire features to async functions: cleanup, variadic args
  • dotfile generation (google#42)

AsyncFunc

// wire.go

//go:build wireinject
// +build wireinject

type A int
type B int

func Sum(ctx context.Context) (int, error) {
	wire.Build(
		wire.AsyncFunc(slowA),
		wire.AsyncFunc(slowB),
		sum,
	)
	return 0, nil
}

func slowA() A {
  time.Sleep(1 * time.Second)
  return A(1) 
}

func slowB() B {
  time.Sleep(1 * time.Second)
  return B(1) 
}

func sum(a A, b B) int {
    return int(a) + int(b)
}

// wire_gen.go

func Sum() (int, error) {
    g, err := errgroup.WithContext(ctx)

    aChan := make(chan A, 1)
    g.Go(func() error {
       a := slowA()
       ...
    })

    bChan := make(chan B, 1)
    g.Go(func() error {
       b := slowB()
       ...
    })

    sumChan := make(chan int, 1)
    g.Go(func() error) {
       ...
       s := sum(a, b)
       ...
    }

    if err := g.Wait(); err != nil {
        return 0, err
    }

    return <-sumChan, nil
}

// app.go

t := time.Now()
s, err := Sum() 
fmt.Printf("Sum = %d after %d second", s, time.Since(t).Seconds())
// Sum = 2 after 1 second

About

A fork of Wire for asynchronous graphs

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 95.7%
  • Shell 4.3%