ing
This commit is contained in:
35
external/grpc/client.go
vendored
Normal file
35
external/grpc/client.go
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
ooas "git.loafle.net/overflow/overflow_api_server/golang"
|
||||
)
|
||||
|
||||
func Exec(ctx context.Context, method string, params []string) (string, error) {
|
||||
if nil == grpcPool {
|
||||
return "", fmt.Errorf("App: GRPC Pool is not initialized")
|
||||
}
|
||||
|
||||
var client interface{}
|
||||
var err error
|
||||
if client, err = grpcPool.Get(); nil != err {
|
||||
return "", err
|
||||
}
|
||||
defer grpcPool.Put(client)
|
||||
|
||||
sm := strings.Split(method, ".")
|
||||
si := &ooas.ServerInput{
|
||||
Target: sm[0],
|
||||
Method: sm[1],
|
||||
Params: params,
|
||||
}
|
||||
var so *ooas.ServerOutput
|
||||
if so, err = client.(ooas.OverflowApiServerClient).Exec(ctx, si); nil != err {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return so.Result, nil
|
||||
}
|
||||
49
external/grpc/pool.go
vendored
Normal file
49
external/grpc/pool.go
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
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_app/conf"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var grpcPool cgp.Pool
|
||||
|
||||
func ExternalInit() {
|
||||
ph := &grpcPoolHandlers{}
|
||||
ph.MaxCapacity = conf.Config.GRPC.Pool.MaxCapacity
|
||||
ph.MaxIdle = conf.Config.GRPC.Pool.MaxIdle
|
||||
ph.IdleTimeout = conf.Config.GRPC.Pool.IdleTimeout
|
||||
ph.Wait = conf.Config.GRPC.Pool.Wait
|
||||
|
||||
grpcPool = cgp.New(ph)
|
||||
|
||||
if err := grpcPool.Start(); nil != err {
|
||||
logging.Logger().Panic(fmt.Sprintf("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(conf.Config.GRPC.Addr, grpc.WithInsecure())
|
||||
if nil != err {
|
||||
return nil, nil, err
|
||||
}
|
||||
c := ooas.NewOverflowApiServerClient(conn)
|
||||
|
||||
return conn, c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user