This commit is contained in:
crusader 2018-03-22 22:55:19 +09:00
parent 5f0dc6a76e
commit df4f9465fc
2 changed files with 50 additions and 36 deletions

View File

@ -9,16 +9,16 @@ import (
) )
func Exec(ctx context.Context, method string, params []string) (string, error) { func Exec(ctx context.Context, method string, params []string) (string, error) {
if nil == grpcPool { if nil == grpcClient {
return "", fmt.Errorf("App: GRPC Pool is not initialized") return "", fmt.Errorf("App: GRPC Client is not initialized")
} }
var client interface{} // var client interface{}
var err error var err error
if client, err = grpcPool.Get(); nil != err { // if client, err = grpcPool.Get(); nil != err {
return "", err // return "", err
} // }
defer grpcPool.Put(client) // defer grpcPool.Put(client)
sm := strings.Split(method, ".") sm := strings.Split(method, ".")
si := &ooas.ServerInput{ si := &ooas.ServerInput{
@ -27,7 +27,7 @@ func Exec(ctx context.Context, method string, params []string) (string, error) {
Params: params, Params: params,
} }
var so *ooas.ServerOutput var so *ooas.ServerOutput
if so, err = client.(ooas.OverflowApiServerClient).Exec(ctx, si); nil != err { if so, err = grpcClient.(ooas.OverflowApiServerClient).Exec(ctx, si); nil != err {
return "", err return "", err
} }

70
external/grpc/pool.go vendored
View File

@ -1,47 +1,61 @@
package grpc package grpc
import ( import (
cgp "git.loafle.net/commons_go/grpc_pool"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
ooas "git.loafle.net/overflow/overflow_api_server/golang" ooas "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_websocket/config" "git.loafle.net/overflow/overflow_gateway_websocket/config"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
var grpcPool cgp.Pool var grpcClient ooas.OverflowApiServerClient
func ExternalInit() { func ExternalInit() {
ph := &grpcPoolHandlers{} conn, err := grpc.Dial(config.Config.GRPC.Addr, grpc.WithInsecure())
ph.MaxCapacity = config.Config.GRPC.Pool.MaxCapacity if nil != err {
ph.MaxIdle = config.Config.GRPC.Pool.MaxIdle logging.Logger().Panic(err)
ph.IdleTimeout = config.Config.GRPC.Pool.IdleTimeout
ph.Wait = config.Config.GRPC.Pool.Wait
grpcPool = cgp.New(ph)
if err := grpcPool.Start(); nil != err {
logging.Logger().Panicf("App: %v", err)
return
} }
grpcClient = ooas.NewOverflowApiServerClient(conn)
} }
func ExternalDestroy() { func ExternalDestroy() {
if nil != grpcPool {
grpcPool.Stop()
}
} }
type grpcPoolHandlers struct { // var grpcPool cgp.Pool
cgp.PoolHandlers
}
func (h *grpcPoolHandlers) Dial() (*grpc.ClientConn, interface{}, error) { // func ExternalInit() {
var err error // ph := &grpcPoolHandlers{}
conn, err := grpc.Dial(config.Config.GRPC.Addr, grpc.WithInsecure()) // ph.MaxCapacity = config.Config.GRPC.Pool.MaxCapacity
if nil != err { // ph.MaxIdle = config.Config.GRPC.Pool.MaxIdle
return nil, nil, err // ph.IdleTimeout = config.Config.GRPC.Pool.IdleTimeout
} // ph.Wait = config.Config.GRPC.Pool.Wait
c := ooas.NewOverflowApiServerClient(conn)
return conn, c, nil // grpcPool = cgp.New(ph)
}
// if err := grpcPool.Start(); nil != err {
// logging.Logger().Panicf("App: %v", err)
// return
// }
// }
// func ExternalDestroy() {
// if nil != grpcPool {
// grpcPool.Stop()
// }
// }
// type grpcPoolHandlers struct {
// cgp.PoolHandlers
// }
// func (h *grpcPoolHandlers) Dial() (*grpc.ClientConn, interface{}, error) {
// var err error
// conn, err := grpc.Dial(config.Config.GRPC.Addr, grpc.WithInsecure())
// if nil != err {
// return nil, nil, err
// }
// c := ooas.NewOverflowApiServerClient(conn)
// return conn, c, nil
// }