php接口请求超时设置,php grpc client如何设置超时时间?

grpc-php-client 写日志到 grpc-golang-server

但是需要给php写日志时候设置给超时

客户端:

require dirname(__FILE__).'/vendor/autoload.php';

include_once dirname(__FILE__).'/Apibeatlog/ApiBeatClient.php';

include_once dirname(__FILE__).'/Apibeatlog/LogRequestData.php';

include_once dirname(__FILE__).'/Apibeatlog/LogResponseData.php';

include_once dirname(__FILE__).'/GPBMetadata/Apibeat.php';

function LogInfo($i)

{

//1.创建client

$client = new Apibeatlog\ApiBeatClient('localhost:1234', [

'credentials' => Grpc\ChannelCredentials::createInsecure(),

'timeout' => 1000,

]);

//2.创建request

$request = new Apibeatlog\LogRequestData();

$request->setSource("1111111111");

$request->setMessage("22222");

$request->setData(json_encode(array("i"=>$i)));

$request->setService("apigateway");

$request->setHost("127.0.0.1");

set_time_limit(1);

//3.发起请求

list($reply, $status) = $client->LogInfo($request)->wait();

//4.获取相应

if ($status->code == 0) {

$code = $reply->getCode();

$msg = $reply->getMsg();

//TODO

} else {

$code = -1;

$msg = 'grpc request failed';

}

echo "code:".$code."
";

echo "msg:".$msg."
";

//set_time_limit(0);

}

try {

echo LogInfo(1)."
";

} catch (Exception $e) {

echo 'Caught exception: ', $e->getMessage(), "
";

}

echo "永远能看到下面的业务1";

try {

echo LogInfo(2)."
";

} catch (Exception $e) {

echo 'Caught exception: ', $e->getMessage(), "
";

}

echo "永远能看到下面的业务2";

?>

服务端

func (s *ApiBeatRpcServer) LogInfo(ctx context.Context, in *apibeatlog.LogRequestData) (res *apibeatlog.LogResponseData, err error) {

var data models.LogData

if len(in.Message) > models.MAX_MESSAGE_SIZE {

data.Message = string([]byte(in.Message)[:models.MAX_MESSAGE_SIZE])

} else {

data.Message = in.Message

}

data.Source = in.Source

data.Host = in.Host

data.Service = in.Service

var f interface{}

err = json.Unmarshal([]byte(in.Data), &f)

if err != nil || f == nil {

data.Data = nil

} else {

data.Data = f.(map[string]interface{})

}

fmt.Println(in)

time.Sleep(time.Second*5) //模拟要写很长时间

//go kafka.SendLog(config.Config.Topic, config.Config.Key, data)

return &apibeatlog.LogResponseData {

Code: strconv.Itoa(models.ERROR_OK),

Msg:"ok",

}, nil

}

你可能感兴趣的:(php接口请求超时设置)