-
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.
- Loading branch information
sado
committed
Oct 9, 2022
1 parent
39061d2
commit e8a2040
Showing
18 changed files
with
414 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package go_kitx | ||
package kitx | ||
|
||
const ( | ||
version = "v0.0.1" | ||
|
||
logo = ` | ||
██████╗ ██████╗ ██╗ ██╗██╗████████╗██╗ ██╗ | ||
██╔════╝ ██╔═══██╗ ██║ ██╔╝██║╚══██╔══╝╚██╗██╔╝ | ||
██║ ███╗██║ ██║█████╗█████╔╝ ██║ ██║ ╚███╔╝ | ||
██║ ██║██║ ██║╚════╝██╔═██╗ ██║ ██║ ██╔██╗ | ||
╚██████╔╝╚██████╔╝ ██║ ██╗██║ ██║ ██╔╝ ██╗ | ||
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ | ||
import "fmt" | ||
|
||
const ( | ||
version = "v0.0.2" | ||
logo = ` | ||
_ _ | ||
| | (_) _ | ||
____ ___ _____ | | _ _ _| |_ _ _ | ||
/ _ | / _ \ (_____) | |_/ )| |(_ _)( \ / ) | ||
( (_| || |_| | | _ ( | | | |_ ) X ( | ||
\___ | \___/ |_| \_)|_| \__)(_/ \_) | ||
(_____| | ||
` | ||
) | ||
|
||
func startingPrint(id, name string) { | ||
fmt.Printf("%s \n", logo) | ||
fmt.Printf("\x1b[%dmKitx Version: %s\x1b[0m \n", 36, version) | ||
fmt.Printf("\x1b[%dmApp ID: %s\x1b[0m \n", 36, id) | ||
fmt.Printf("\x1b[%dmApp Name: %s\x1b[0m \n", 36, name) | ||
fmt.Printf("\x1b[%dmStarting App ...\x1b[0m \n", 34) | ||
fmt.Println("") | ||
} |
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 |
---|---|---|
@@ -1,7 +1,53 @@ | ||
package go_kitx | ||
package kitx | ||
|
||
import "testing" | ||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sado0823/go-kitx/transport/grpc" | ||
"github.com/sado0823/go-kitx/transport/http" | ||
) | ||
|
||
func Test_New(t *testing.T) { | ||
t.Log(logo) | ||
} | ||
|
||
func Test_NewApp(t *testing.T) { | ||
|
||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
go func() { | ||
hs := http.NewServer(http.WithServerAddress("0.0.0.0:7001")) | ||
gs := grpc.NewServer(grpc.WithServerAddress("0.0.0.0:7002")) | ||
|
||
app := New( | ||
WithName("demo.app"), | ||
WithVersion("v0.0.00001"), | ||
WithMetadata(map[string]string{}), | ||
WithServer(hs, gs), | ||
) | ||
|
||
wg.Done() | ||
err := app.Run() | ||
if err != nil { | ||
t.Log(err) | ||
return | ||
} | ||
}() | ||
wg.Wait() | ||
time.Sleep(time.Second) | ||
|
||
client, err := grpc.DialInsecure(context.Background(), | ||
grpc.WithClientEndpoint("direct:///0.0.0.0:7002,0.0.0.0:7001"), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log(client.Target()) | ||
|
||
err = client.Invoke(context.Background(), "/abc", 1, map[string]interface{}{}) | ||
t.Log(err) | ||
} |
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,201 @@ | ||
package kitx | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/sado0823/go-kitx/kit/registry" | ||
"github.com/sado0823/go-kitx/transport" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type ( | ||
AppI interface { | ||
ID() string | ||
Name() string | ||
Version() string | ||
Metadata() map[string]string | ||
Endpoint() []string | ||
} | ||
|
||
App struct { | ||
ctxWithCancel context.Context | ||
opt *option | ||
ctxCancel context.CancelFunc | ||
lock sync.Mutex | ||
registrySvc *registry.Service | ||
} | ||
|
||
Option func(o *option) | ||
|
||
appKey struct{} | ||
) | ||
|
||
func NewContext(ctx context.Context, s AppI) context.Context { | ||
return context.WithValue(ctx, appKey{}, s) | ||
} | ||
|
||
func FromContext(ctx context.Context) (s AppI, ok bool) { | ||
s, ok = ctx.Value(appKey{}).(AppI) | ||
return | ||
} | ||
|
||
func New(opts ...Option) *App { | ||
opt := &option{ | ||
ctx: context.Background(), | ||
signals: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT}, | ||
registrarTimeout: time.Second * 5, | ||
stopTimeout: time.Second * 10, | ||
servers: nil, | ||
} | ||
for _, o := range opts { | ||
o(opt) | ||
} | ||
|
||
opt.Fix() | ||
|
||
ctx, cancelFunc := context.WithCancel(opt.ctx) | ||
|
||
return &App{ | ||
ctxWithCancel: ctx, | ||
opt: opt, | ||
ctxCancel: cancelFunc, | ||
} | ||
} | ||
|
||
func (a *App) ID() string { | ||
return a.opt.id | ||
} | ||
|
||
func (a *App) Name() string { | ||
return a.opt.name | ||
} | ||
|
||
func (a *App) Version() string { | ||
return a.opt.version | ||
} | ||
|
||
func (a *App) Metadata() map[string]string { | ||
return a.opt.metadata | ||
} | ||
|
||
func (a *App) Endpoint() []string { | ||
if a.registrySvc != nil { | ||
return a.registrySvc.Endpoints | ||
} | ||
return []string{} | ||
} | ||
|
||
func (a *App) Stop() error { | ||
a.lock.Lock() | ||
svc := a.registrySvc | ||
a.lock.Unlock() | ||
|
||
if a.opt.registrar != nil && svc != nil { | ||
ctx, cancel := context.WithTimeout(NewContext(a.ctxWithCancel, a), a.opt.registrarTimeout) | ||
defer cancel() | ||
if err := a.opt.registrar.Deregister(ctx, svc); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if a.ctxCancel != nil { | ||
a.ctxCancel() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *App) Run() error { | ||
registrySvc, err := a.genRegistrySvc() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a.lock.Lock() | ||
a.registrySvc = registrySvc | ||
a.lock.Unlock() | ||
|
||
startingPrint(a.ID(), a.Name()) | ||
|
||
var ( | ||
eg, ctxWithApp = errgroup.WithContext(NewContext(a.ctxWithCancel, a)) | ||
wg sync.WaitGroup | ||
) | ||
|
||
for _, server := range a.opt.servers { | ||
server := server | ||
// server stop go-routine | ||
eg.Go(func() error { | ||
<-ctxWithApp.Done() // ctx will be canceled when app stop | ||
stopCtx, stopCancel := context.WithTimeout(NewContext(a.opt.ctx, a), a.opt.stopTimeout) | ||
defer stopCancel() | ||
return server.Stop(stopCtx) | ||
}) | ||
wg.Add(1) | ||
// server start go-routine | ||
eg.Go(func() error { | ||
wg.Done() | ||
return server.Start(NewContext(a.opt.ctx, a)) | ||
}) | ||
} | ||
// wait all server started | ||
wg.Wait() | ||
|
||
// use registry | ||
if a.opt.registrar != nil { | ||
regisCtx, regisCancel := context.WithTimeout(ctxWithApp, a.opt.registrarTimeout) | ||
defer regisCancel() | ||
return a.opt.registrar.Register(regisCtx, a.registrySvc) | ||
} | ||
|
||
// wait signals for stop | ||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, a.opt.signals...) | ||
eg.Go(func() error { | ||
select { | ||
case <-ctxWithApp.Done(): | ||
return nil | ||
case <-sig: | ||
return a.Stop() | ||
} | ||
}) | ||
|
||
err = eg.Wait() | ||
if err != nil && !errors.Is(err, context.Canceled) { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *App) genRegistrySvc() (*registry.Service, error) { | ||
endpoints := make([]string, 0, len(a.opt.endpoints)) | ||
for _, endpoint := range a.opt.endpoints { | ||
endpoints = append(endpoints, endpoint.String()) | ||
} | ||
if len(endpoints) == 0 { | ||
for _, server := range a.opt.servers { | ||
if endpoint, ok := server.(transport.Endpointer); ok { | ||
url, err := endpoint.Endpoint() | ||
if err != nil { | ||
return nil, err | ||
} | ||
endpoints = append(endpoints, url.String()) | ||
} | ||
} | ||
} | ||
return ®istry.Service{ | ||
ID: a.opt.id, | ||
Name: a.opt.name, | ||
Version: a.opt.version, | ||
Metadata: a.opt.metadata, | ||
Endpoints: endpoints, | ||
}, 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
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
File renamed without changes.
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
Oops, something went wrong.