-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from getoutreach/async
feat: async contract
- Loading branch information
Showing
29 changed files
with
841 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: async infra for example application | ||
|
||
// Package async provides async infra for example application | ||
package async | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
) | ||
|
||
// Publisher service | ||
type Publisher struct { | ||
*plumber.BaseLooper | ||
broker string | ||
} | ||
|
||
func NewPublisher(broker string) *Publisher { | ||
return &Publisher{ | ||
broker: broker, | ||
BaseLooper: contract.NewWorker("async.Publisher"), | ||
} | ||
} | ||
|
||
func (p *Publisher) Publish(ctx context.Context, e *contract.Entity) error { | ||
fmt.Printf("Publishing entity #%v name=%s\n", e.ID, e.Name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: database infra for example application | ||
|
||
// Package database provides database infra for example application | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
) | ||
|
||
// EntityMask provides a mask to format an entity | ||
const EntityMask = "Entity %v" | ||
|
||
// Repository represents a plain database repository | ||
type Repository struct { | ||
id int64 | ||
} | ||
|
||
func NewRepository() (*Repository, error) { | ||
return &Repository{}, nil | ||
} | ||
|
||
func (s *Repository) Get(ctx context.Context, id int64) (*contract.Entity, error) { | ||
return &contract.Entity{ | ||
ID: id, | ||
Name: fmt.Sprintf(EntityMask, id), | ||
}, nil | ||
} | ||
|
||
func (s *Repository) Create(ctx context.Context, name string) (*contract.Entity, error) { | ||
nextID := atomic.AddInt64(&s.id, 1) | ||
return &contract.Entity{ | ||
ID: nextID, | ||
Name: fmt.Sprintf(EntityMask, nextID), | ||
}, nil | ||
} | ||
|
||
// BatchingRepository represents a database repository that can batch single entity reads into batch query | ||
type BatchingRepository struct { | ||
*plumber.BaseLooper | ||
inner contract.Repository | ||
} | ||
|
||
func NewBatchingRepository(inner contract.Repository, batchSize int) (*BatchingRepository, error) { | ||
r := &BatchingRepository{ | ||
inner: inner, | ||
BaseLooper: contract.NewWorker("database.BatchingRepository"), | ||
} | ||
return r, nil | ||
} | ||
|
||
func (s *BatchingRepository) Get(ctx context.Context, id int64) (*contract.Entity, error) { | ||
// Here suppose to be a logic that batches requests into a single batch query | ||
return s.inner.Get(ctx, id) | ||
} | ||
|
||
func (s *BatchingRepository) Create(ctx context.Context, name string) (*contract.Entity, error) { | ||
return s.inner.Create(ctx, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: graphql infra for example application | ||
|
||
// Package graphql provides graphql infra for example application | ||
package graphql | ||
|
||
import ( | ||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
"github.com/getoutreach/plumber/example/service" | ||
) | ||
|
||
// Server represents a graphql server | ||
type Server struct { | ||
*plumber.BaseLooper | ||
port int32 | ||
querier *service.QueryService | ||
mutator contract.MutatorService | ||
} | ||
|
||
// NewServer returns intance of the *Server | ||
func NewServer( | ||
port int32, | ||
querier *service.QueryService, | ||
mutator contract.MutatorService, | ||
) (*Server, error) { | ||
return &Server{ | ||
port: port, | ||
querier: querier, | ||
mutator: mutator, | ||
BaseLooper: contract.NewWorker("graphql.Server"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: grpc infra for example application | ||
|
||
// Package grpc provides grpc infra for example application | ||
package grpc | ||
|
||
import ( | ||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
"github.com/getoutreach/plumber/example/service" | ||
) | ||
|
||
// Server represents a grpc server | ||
type Server struct { | ||
*plumber.BaseLooper | ||
port int32 | ||
querier *service.QueryService | ||
mutator contract.MutatorService | ||
} | ||
|
||
// NewServer returns intance of the *Server | ||
func NewServer( | ||
port int32, | ||
querier *service.QueryService, | ||
mutator contract.MutatorService, | ||
) (*Server, error) { | ||
return &Server{ | ||
port: port, | ||
querier: querier, | ||
mutator: mutator, | ||
BaseLooper: contract.NewWorker("grpc.Server"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: application root dependency container | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
) | ||
|
||
// Config represents a application configuration structure | ||
type Config struct { | ||
AsyncBroker string | ||
} | ||
|
||
// Definer allows to redefine container on startup | ||
type Definer = func(ctx context.Context, cf *Config, a *Container) | ||
|
||
// Container represents root application dependency container | ||
type Container struct { | ||
plumber.Container | ||
Async *Async | ||
Database *Database | ||
GraphQL *GraphQL | ||
GRPC *GRPC | ||
Service *Service | ||
} | ||
|
||
// NewApplication returns instance of the root dependency container | ||
func NewApplication(ctx context.Context, cf *Config, definers ...Definer) *Container { | ||
a := &Container{ | ||
GRPC: new(GRPC), | ||
Database: new(Database), | ||
GraphQL: new(GraphQL), | ||
Service: new(Service), | ||
Async: new(Async), | ||
} | ||
return plumber.DefineContainers(ctx, cf, definers, a, | ||
a.Async, a.Database, a.GRPC, a.GraphQL, a.Service, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: async related dependencies | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/adapter/async" | ||
) | ||
|
||
// Async service represents async processing related dependency container | ||
type Async struct { | ||
Publisher plumber.R[*async.Publisher] | ||
} | ||
|
||
// Define resolves dependencies | ||
func (c *Async) Define(ctx context.Context, cf *Config, a *Container) { | ||
c.Publisher.Define(func() *async.Publisher { | ||
return async.NewPublisher(cf.AsyncBroker) | ||
}) | ||
} |
Oops, something went wrong.