refactoring
This commit is contained in:
28
grpc/client.go
Normal file
28
grpc/client.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
oas "git.loafle.net/overflow/overflow_api_server/golang"
|
||||
)
|
||||
|
||||
func Exec(service string, method string, params []string) (string, error) {
|
||||
c, err := _pool.Get()
|
||||
if nil != err {
|
||||
|
||||
}
|
||||
defer _pool.Put(c)
|
||||
|
||||
si := &oas.ServerInput{
|
||||
Target: service,
|
||||
Method: method,
|
||||
Params: params,
|
||||
}
|
||||
ctx := context.Background()
|
||||
so, err := c.(oas.OverflowApiServerClient).Exec(ctx, si)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return so.Result, nil
|
||||
}
|
||||
28
grpc/pool.go
Normal file
28
grpc/pool.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.loafle.net/commons_go/config"
|
||||
cgp "git.loafle.net/commons_go/grpc_pool"
|
||||
"git.loafle.net/commons_go/logging"
|
||||
)
|
||||
|
||||
var _pool cgp.Pool
|
||||
|
||||
func InitializePool(ctx context.Context) {
|
||||
var err error
|
||||
h := &poolHandlers{
|
||||
ctx: ctx,
|
||||
logger: logging.WithContext(ctx),
|
||||
}
|
||||
h.cfg = config.Sub("grpc")
|
||||
h.MaxIdle = h.cfg.GetInt("pool.MaxIdle")
|
||||
h.MaxCapacity = h.cfg.GetInt("pool.MaxCapacity")
|
||||
|
||||
_pool, err = cgp.New(ctx, h)
|
||||
if nil != err {
|
||||
h.logger.Fatal(fmt.Sprintf("GRpc Pool: %v", err))
|
||||
}
|
||||
}
|
||||
30
grpc/pool_handlers.go
Normal file
30
grpc/pool_handlers.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"git.loafle.net/commons_go/config"
|
||||
cgp "git.loafle.net/commons_go/grpc_pool"
|
||||
oas "git.loafle.net/overflow/overflow_api_server/golang"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type poolHandlers struct {
|
||||
cgp.PoolHandlers
|
||||
ctx context.Context
|
||||
logger *zap.Logger
|
||||
cfg config.Configurator
|
||||
}
|
||||
|
||||
func (h *poolHandlers) OnCreate() (*grpc.ClientConn, interface{}, error) {
|
||||
var err error
|
||||
conn, err := grpc.Dial(config.GetString("grpc.addr"), grpc.WithInsecure())
|
||||
if nil != err {
|
||||
return nil, nil, err
|
||||
}
|
||||
c := oas.NewOverflowApiServerClient(conn)
|
||||
return conn, c, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user