overflow_gateway_websocket/external/grpc/client.go

43 lines
774 B
Go
Raw Permalink Normal View History

2017-11-10 13:24:10 +00:00
package grpc
import (
"context"
"fmt"
"strings"
2018-03-22 13:57:27 +00:00
"sync"
2017-11-10 13:24:10 +00:00
ooas "git.loafle.net/overflow/overflow_api_server/golang"
)
2018-03-22 13:57:27 +00:00
var execMtx sync.RWMutex
2017-11-10 13:24:10 +00:00
func Exec(ctx context.Context, method string, params []string) (string, error) {
2018-03-22 13:55:19 +00:00
if nil == grpcClient {
return "", fmt.Errorf("App: GRPC Client is not initialized")
2017-11-10 13:24:10 +00:00
}
2018-03-22 13:55:19 +00:00
// var client interface{}
2017-11-10 13:24:10 +00:00
var err error
2018-03-22 13:55:19 +00:00
// if client, err = grpcPool.Get(); nil != err {
// return "", err
// }
// defer grpcPool.Put(client)
2017-11-10 13:24:10 +00:00
sm := strings.Split(method, ".")
si := &ooas.ServerInput{
Target: sm[0],
Method: sm[1],
Params: params,
}
2018-03-22 13:57:27 +00:00
execMtx.RLock()
so, err := grpcClient.(ooas.OverflowApiServerClient).Exec(ctx, si)
if nil != err {
execMtx.RUnlock()
2017-11-10 13:24:10 +00:00
return "", err
}
2018-03-22 13:57:27 +00:00
execMtx.RUnlock()
2017-11-10 13:24:10 +00:00
return so.Result, nil
}