Smarter pinging. Don't waster resources #3240
This commit is contained in:
		
							parent
							
								
									868fdd68ab
								
							
						
					
					
						commit
						5fd66d2c97
					
				| @ -84,7 +84,7 @@ export default class Ws extends JsonRpcBase { | ||||
|     this._connecting = false; | ||||
| 
 | ||||
|     if (this._autoConnect) { | ||||
|       this._connect(); | ||||
|       setTimeout(() => this._connect(), 500); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -25,6 +25,8 @@ export default class Status { | ||||
|     this._pingable = false; | ||||
|     this._apiStatus = {}; | ||||
|     this._status = {}; | ||||
| 
 | ||||
|     this._pollPingTimeoutId = null; | ||||
|   } | ||||
| 
 | ||||
|   start () { | ||||
| @ -64,14 +66,43 @@ export default class Status { | ||||
|       }); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * Pinging should be smart. It should only | ||||
|    * be used when the UI is connecting or the | ||||
|    * Node is deconnected. | ||||
|    * | ||||
|    * @see src/views/Connection/connection.js | ||||
|    */ | ||||
|   _shouldPing = () => { | ||||
|     const { isConnected, isConnecting } = this._apiStatus; | ||||
|     return isConnecting || !isConnected; | ||||
|   } | ||||
| 
 | ||||
|   _stopPollPing = () => { | ||||
|     if (!this._pollPingTimeoutId) { | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     clearTimeout(this._pollPingTimeoutId); | ||||
|     this._pollPingTimeoutId = null; | ||||
|   } | ||||
| 
 | ||||
|   _pollPing = () => { | ||||
|     const dispatch = (pingable, timeout = 500) => { | ||||
|     // Already pinging, don't try again
 | ||||
|     if (this._pollPingTimeoutId) { | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     const dispatch = (pingable, timeout = 1000) => { | ||||
|       if (pingable !== this._pingable) { | ||||
|         this._pingable = pingable; | ||||
|         this._store.dispatch(statusCollection({ isPingable: pingable })); | ||||
|       } | ||||
| 
 | ||||
|       setTimeout(this._pollPing, timeout); | ||||
|       this._pollPingTimeoutId = setTimeout(() => { | ||||
|         this._stopPollPing(); | ||||
|         this._pollPing(); | ||||
|       }, timeout); | ||||
|     }; | ||||
| 
 | ||||
|     fetch('/', { method: 'HEAD' }) | ||||
| @ -108,6 +139,13 @@ export default class Status { | ||||
|       this._apiStatus = apiStatus; | ||||
|     } | ||||
| 
 | ||||
|     // Ping if necessary, otherwise stop pinging
 | ||||
|     if (this._shouldPing()) { | ||||
|       this._pollPing(); | ||||
|     } else { | ||||
|       this._stopPollPing(); | ||||
|     } | ||||
| 
 | ||||
|     if (!isConnected) { | ||||
|       nextTimeout(250); | ||||
|       return; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user