Google Cloud Functions& PubSub - 空转后延迟

时间:2017-12-06 16:15:01

标签: google-cloud-functions google-cloud-pubsub

我触发HTTP云功能,然后将消息发布到我的PubSub主题。每隔几秒发布一次,这很快就会有效,通常延迟时间为1秒。但是,如果我暂时不发布任何内容(比如5分钟),那么发布会有很长的延迟(约20秒)。

如何让它一直快速?思路:

  • 以某种方式配置Pub / Sub发布者立即执行此操作。但是,无法在API文档中找到这样的论点。
  • 更改发布商功能。

发布商功能:

"use strict";
const PubSub = require("@google-cloud/pubsub");
const pubsub = PubSub({
    projectId: "pubsub-forwarders"
});

exports.testFunction = function testFunction(req, res) {
    publishMessage("testtopic", "testmessage");
    res.status(200).send();
}

function publishMessage(topicName, data) {
    const topic = pubsub.topic(topicName);
    const publisher = topic.publisher();
    const dataBuffer = Buffer.from(data);

    return publisher.publish(dataBuffer)
        .then((results) => {
            console.log("Message", JSON.stringify(results), "published.");
            return JSON.stringify(results);
    });
}

云功能日志:

16:57:05.938 Function execution started
16:57:05.948 Function execution took 11 ms, finished with status code: 200
16:57:22.987 Message "179492329652563" published.

非常感谢!

1 个答案:

答案 0 :(得分:1)

原因是发送响应后Google Cloud Function正在关闭实例。

只需将异步发布更改为同步,并在成功发布后发送响应即可。它将发布时间缩短到数十毫秒。

y<-matrix(c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 0, 0, 1,
        1, 1, 0, 0, 0, 0))
    covariates:
   elevation.m   habitat birdsmallabund1000 birdsmallbio1000 birdmlabund1000 birdmlbio1000 totalbirdabund1000 totalbirdbio1000
1      1828.80 Shrubland           3.141687         56.23548       0.6509944      59.95943           3.792681        116.19491
2      2194.56 Shrubland           3.000875         53.71551       0.7360382      67.78902           3.736913        121.50453
3       502.92 Grassland           4.514240         80.80469       1.6084328     148.13317           6.122673        228.93787
4       944.88 Grassland           3.822037         68.41400       1.3260143     122.12721           5.148051        190.54121
5        31.09  Cropland           8.920525        159.67709       7.4512331     686.25585          16.371758        845.93294
6        27.13    Forest           1.797454         32.17478       0.4961018      45.69141           2.293556         77.86619
7      1981.20    Forest           3.088385         55.28250       0.6813842      62.75632           3.769769        118.03882
8       667.50 Grassland           3.806842         68.14256       1.0256961      94.46905           4.832538        162.61162
9       746.76    Forest           3.838743         68.71368       2.0089101     185.02212           5.847653        253.73580
10      335.28  Cropland           5.946937        106.44980       2.5720764     236.88950           8.519014        343.33930
11     1981.20    Forest           3.128799         56.00533       0.8153540      75.09276           3.944153        131.09809
12       11.00  Cropland          11.140732        199.41941      19.5284010    1798.56555          30.669133       1997.98496
13       78.02  Cropland           8.126014        145.45553       9.1868735     846.10811          17.312888        991.56364
14      359.05 Grassland           2.452983         43.90811       0.8911695      82.07812           3.344153        125.98624
15     2673.00 Shrubland           2.495147         44.66269       0.7071599      65.13039           3.202307        109.79308
16     1433.00    Forest           3.361337         60.16762       0.7642005      70.38107           4.125537        130.54869
17      986.00 Grassland           2.704057         48.40334       0.8036595      74.01893           3.507717        122.42228
18       73.00    Forest           2.330310         41.71185       3.1883850     293.65346           5.518695        335.36531
19       52.00  Cropland          10.041209        179.73819      11.2941925    1040.19205          21.335402       1219.93023
20     1343.00    Forest           3.416229         61.15091       0.8014320      73.81400           4.217661        134.96492
21      182.00  Cropland           7.466508        133.65036       3.7916468     349.20716          11.258154        482.85752
22     2569.00 Shrubland           2.873588         51.43683       0.5789976      53.32339           3.452586        104.76022
23       44.00  Cropland          10.583373        189.44232       7.8829753     726.02148          18.466348        915.46380
24      396.00    Forest           6.894909        123.41822       0.6509944      59.95943           7.545903        183.37765
25      690.00 Grassland           4.415274         79.03309       2.1759745     200.40819           6.591249        279.44129
26      341.00 Grassland           2.264121         40.52745       0.8555290      78.79133           3.119650        119.31877
   rodentabund1000 rodentbio1000 mouseabund1000 ratabund1000 mousebio1000 ratbio1000 insectabund1000 insectbio1000 totalpreyabund1000
1           6.9000      97.00204           2.20        4.700     31.00000      66.00       18.176486      5.329819              28.90
2           2.5700     144.38381           0.80        1.700     46.00000      98.00       10.546850      7.401802              16.90
3           6.4800      65.38299           6.48        0.000     65.38299       0.00       28.617933      3.848231              41.20
4           2.6000     142.31028           2.60        0.000    142.31028       0.00       14.587196      7.313924              22.30
5           2.1900      51.58449           2.10        0.100     49.00000       2.58       10.673089      3.164183              29.20
6          15.2000     289.66071          11.00        4.300    208.60000      81.10       43.969812     13.152176              61.40
7           0.0792      88.23654           0.06        0.020     63.50000      24.70        5.588603      4.928905               9.44
8           4.9800      72.64881           4.98        0.000     72.64881       0.00       21.473533      4.198045              31.30
9           3.8400     277.79909           2.80        1.100    200.00000      77.80       39.422315     12.705869              49.10
10          1.0400      54.81910           0.99        0.050     52.10000       2.70        6.609045      3.327131              16.20
11          0.0304     234.19762           0.02        0.009    169.00000      65.60       40.190727     11.035245              44.20
12          1.6800      46.26389           1.60        0.080     43.90000       2.30        9.259345      2.892185              41.60
13          1.9300      46.94789           1.80        0.100     44.60000       2.30        9.743338      2.927446              29.00
14          5.4700      58.29962           5.47        0.000     58.29962       0.00       23.514118      3.500606              32.30
15          0.7930     116.01280           0.25        0.540     37.00000      79.00        5.242146      6.178571               9.24
16          4.4200      78.53620           3.20        1.200     56.50000      22.00       33.639882      4.477020              42.20
17          1.8400      71.48462           1.84        0.000     71.48462       0.00       11.868661      4.142421              17.20
18         15.1000    1421.35490          10.90        4.200   1023.00000     398.00      107.499518     48.908490             128.00
19          0.9110      51.58449           0.87        0.050     49.00000       2.60        6.632146      3.164183              28.90
20          5.1300      77.68672           3.70        1.400     55.90000      21.80       38.424278      4.436998              47.80
21          1.4800      49.80229           1.40        0.070     47.30000       2.50        8.632346      3.073644              21.40
22          0.9370      97.50520           0.30        0.640     31.20000      66.30        6.034518      5.352635              10.40
23          1.9700      48.54587           1.87        0.100     46.00000       2.40       10.450692      3.009477              30.90
24          5.8000     139.49852           4.20        1.600    100.00000      39.10       95.264864      7.194400             109.00
25          3.8500      68.44883           3.85        0.000     68.44883       0.00       19.893488      3.996622              30.30
26          8.3800     125.20267           8.38        0.000    125.20267       0.00       36.045071      6.579974              47.50
   totalpreybio1000 ground canopy height meanannualrainfall.mm avgtempduringsurvey.f
1          218.5268     80      0   0.30                1008.0                  65.0
2          273.2901     60      0   0.43                1384.7                  58.0
3          298.1691     85      0   0.55                 634.4                  69.0
4          340.1654     95      0   0.20                1371.0                  66.0
5          900.6816     80      0   0.35                 409.9                  78.0
6          380.6791     95     85   2.93                2044.1                  76.0
7          211.2043     95     90   9.00                 918.3                  58.0
8          239.4585     95      0   0.10                 734.2                  65.8
9          544.2408     90     60   5.00                2004.5                  68.5
10         401.4855     70      0   0.20                 467.5                  77.5
11         376.3310     80     75  20.00                1842.8                  54.3
12        2047.1410     50      5   0.40                 306.8                  80.5
13        1041.4390     75      0   0.25                 320.7                  79.5
14         187.7865     35     10   0.11                 525.8                  74.3
15         231.9844     50      0   0.50                1177.5                  52.9
16         213.5619     80      0   0.40                 808.0                  65.5
17         198.0493     75      5   0.30                 718.9                  67.4
18        1805.6287     95     65   6.00                3550.6                  74.5
19        1274.6789     10     15   0.73                 409.9                  80.6
20         217.0886     73      0   0.50                 797.7                  65.0
21         535.7335     75      0   0.45                 376.6                  78.4
22         207.6181     40      0   0.55                1012.9                  50.0
23         967.0191     70      0   0.45                 352.4                  77.0
24         330.0706    100     65   8.30                1352.1                  78.0
25         351.8867     65      0   0.15                 677.8                  75.5
26         251.1014     70      0   0.70                1249.7                  73.0

> umf <-unmarkedFrameOccu(y=y, siteCovs=covariates, obsCovs=NULL)
> summary(umf)
unmarkedFrame Object

26 sites
Maximum number of observations per site: 1 
Mean number of observations per site: 1 
Sites with at least one detection: 11 

Tabulation of y observations:
 0  1 
15 11 

Site-level covariates:
  elevation.m          habitat  birdsmallabund1000 birdsmallbio1000 birdmlabund1000   birdmlbio1000     totalbirdabund1000
 Min.   :  11.0   Cropland :7   Min.   : 1.797     Min.   : 32.17   Min.   : 0.4961   Min.   :  45.69   Min.   : 2.294    
 1st Qu.: 104.0   Forest   :8   1st Qu.: 2.905     1st Qu.: 52.01   1st Qu.: 0.7431   1st Qu.:  68.44   1st Qu.: 3.745    
 Median : 585.2   Grassland:7   Median : 3.612     Median : 64.65   Median : 0.9584   Median :  88.27   Median : 4.990    
 Mean   : 864.3   Shrubland:4   Mean   : 4.830     Mean   : 86.45   Mean   : 3.1721   Mean   : 292.15   Mean   : 8.002    
 3rd Qu.:1410.5                 3rd Qu.: 6.658     3rd Qu.:119.18   3rd Qu.: 3.0343   3rd Qu.: 279.46   3rd Qu.: 8.276    
 Max.   :2673.0                 Max.   :11.141     Max.   :199.42   Max.   :19.5284   Max.   :1798.57   Max.   :30.669    
 totalbirdbio1000  rodentabund1000   rodentbio1000     mouseabund1000    ratabund1000      mousebio1000       ratbio1000     
 Min.   :  77.87   Min.   : 0.0304   Min.   :  46.26   Min.   : 0.020   Min.   :0.00000   Min.   :  31.00   Min.   :  0.000  
 1st Qu.: 121.73   1st Qu.: 1.5300   1st Qu.:  55.69   1st Qu.: 1.093   1st Qu.:0.00225   1st Qu.:  46.33   1st Qu.:  0.575  
 Median : 172.99   Median : 2.5850   Median :  78.11   Median : 2.150   Median :0.09000   Median :  57.40   Median :  2.650  
 Mean   : 378.60   Mean   : 4.0616   Mean   : 154.43   Mean   : 3.218   Mean   :0.84458   Mean   : 113.75   Mean   : 40.645  
 3rd Qu.: 341.35   3rd Qu.: 5.3850   3rd Qu.: 135.92   3rd Qu.: 4.112   3rd Qu.:1.17500   3rd Qu.:  93.16   3rd Qu.: 65.900  
 Max.   :1997.98   Max.   :15.2000   Max.   :1421.35   Max.   :11.000   Max.   :4.70000   Max.   :1023.00   Max.   :398.000  
 insectabund1000   insectbio1000    totalpreyabund1000 totalpreybio1000     ground           canopy          height       
 Min.   :  5.242   Min.   : 2.892   Min.   :  9.24     Min.   : 187.8   Min.   : 10.00   Min.   : 0.00   Min.   : 0.1000  
 1st Qu.:  9.380   1st Qu.: 3.370   1st Qu.: 21.62     1st Qu.: 221.9   1st Qu.: 66.25   1st Qu.: 0.00   1st Qu.: 0.3000  
 Median : 16.382   Median : 4.457   Median : 30.60     Median : 335.1   Median : 75.00   Median : 0.00   Median : 0.4500  
 Mean   : 25.462   Mean   : 7.163   Mean   : 37.53     Mean   : 540.2   Mean   : 72.62   Mean   :18.27   Mean   : 2.2635  
 3rd Qu.: 35.444   3rd Qu.: 7.041   3rd Qu.: 43.70     3rd Qu.: 542.1   3rd Qu.: 88.75   3rd Qu.:13.75   3rd Qu.: 0.7225  
 Max.   :107.500   Max.   :48.908   Max.   :128.00     Max.   :2047.1   Max.   :100.00   Max.   :90.00   Max.   :20.0000  
 meanannualrainfall.mm avgtempduringsurvey.f
 Min.   : 306.8        Min.   :50.00        
 1st Qu.: 482.1        1st Qu.:65.12        
 Median : 802.9        Median :71.00        
 Mean   :1017.6        Mean   :69.55        
 3rd Qu.:1326.5        3rd Qu.:77.38        
 Max.   :3550.6        Max.   :80.60  
    Call:
occu(formula = ~height + elevation.m ~ height, data = umf)

Occupancy (logit-scale):
            Estimate    SE      z P(>|z|)
(Intercept)    0.584 0.634  0.921   0.357
height        -1.049 1.037 -1.011   0.312

Detection (logit-scale):
             Estimate    SE         z P(>|z|)
(Intercept)  0.000961   NaN       NaN     NaN
height      -0.002226 32553 -6.84e-08       1
elevation.m  2.058074   NaN       NaN     NaN

AIC: 38.09015 
Number of sites: 26
optim convergence code: 0
optim iterations: 26 
Bootstrap iterations: 0 

Warning message:
In sqrt(diag(vcov(obj))) : NaNs produced

请参阅参考文献:https://cloud.google.com/functions/docs/bestpractices/tips#do_not_start_background_activities