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

70
external/grpc/pool.go vendored
View File

@ -1,47 +1,61 @@
package grpc
import (
cgp "git.loafle.net/commons_go/grpc_pool"
"git.loafle.net/commons_go/logging"
ooas "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_websocket/config"
"google.golang.org/grpc"
)
var grpcPool cgp.Pool
var grpcClient ooas.OverflowApiServerClient
func ExternalInit() {
ph := &grpcPoolHandlers{}
ph.MaxCapacity = config.Config.GRPC.Pool.MaxCapacity
ph.MaxIdle = config.Config.GRPC.Pool.MaxIdle
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
conn, err := grpc.Dial(config.Config.GRPC.Addr, grpc.WithInsecure())
if nil != err {
logging.Logger().Panic(err)
}
grpcClient = ooas.NewOverflowApiServerClient(conn)
}
func ExternalDestroy() {
if nil != grpcPool {
grpcPool.Stop()
}
}
type grpcPoolHandlers struct {
cgp.PoolHandlers
}
// var grpcPool cgp.Pool
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)
// func ExternalInit() {
// ph := &grpcPoolHandlers{}
// ph.MaxCapacity = config.Config.GRPC.Pool.MaxCapacity
// ph.MaxIdle = config.Config.GRPC.Pool.MaxIdle
// ph.IdleTimeout = config.Config.GRPC.Pool.IdleTimeout
// ph.Wait = config.Config.GRPC.Pool.Wait
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
// }