代码之家  ›  专栏  ›  技术社区  ›  Michael Fernandes

如何设置google云数据存储的超时。get?

  •  0
  • Michael Fernandes  · 技术社区  · 7 年前

    如果客户机有互联网连接,一切都会顺利的。

    这是我的代码:

    try{
        let search = datastore.key(['Client', Client_id])           
        datastore.get(search, /*{timeout: 1000},*/ function (err, entity) {
            console.log('limit >>>', entity.limit)
            evt.emit('comparedate', res, entity.limit)    
        });
    }
    catch(error){
        console.log('Error >>>', error)
    }
    

    我的问题是:连接尝试没有时间限制。当客户端无法访问internet时,请求将永远保持“挂起”状态,并且不会转到捕获条件。

    我尝试了一些参数,比如: Global#CallOptions

    谢谢你的帮助!

    编辑我知道这不是最值得信赖的方式。但现在我决定用这个密码:

    evt.on('isonline', (res) => {
        try{
            require('dns').lookup('google.com',function(err) {
                if (err && err.code == "ENOTFOUND") {
                    console.log('NO INTERNET')
                    evt.emit('readofflinedata', res)
                } else {
                    console.log('WITH INTERNET')
                    evt.emit('readonlinedata', res)
                }
            })
        }
        catch(error){
            res.status(200).send({ error: true, message: error.message })
        }   
    })
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   callmehiphop    7 年前

    数据存储客户端在内部使用一个名为 google-gax . 您可以通过传入来配置超时等 gax options .

    datastore.get(key, {
      gaxOptions: {timeout: 1000}
    }, (err, entity) => {
      // ...
    });
    
        2
  •  2
  •   Alex Riquelme    7 年前

    var Promise = require("bluebird");
    var elt = new Promise((resolve, reject) => {
       fun(param, (err) => {
         if (err) reject(err);
         doSomething(); // <- datastore.get() funtion
         resolve();
    });
    
    elt.timeout(1000).then(() => console.log('done'))
                     .catch(Promise.TimeoutError, (e) => console.log("timed out"))