-
Notifications
You must be signed in to change notification settings - Fork 28
/
ping.go
40 lines (30 loc) · 917 Bytes
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: Apache-2.0
package database
import (
"fmt"
"time"
)
// Ping sends a "ping" request with backoff to the database.
func (e *engine) Ping() error {
e.logger.Tracef("sending ping request to the %s database", e.Driver())
// create a loop to attempt ping requests 5 times
for i := 0; i < 5; i++ {
// capture database/sql database from gorm.io/gorm database
_sql, err := e.client.DB()
if err != nil {
return err
}
// send ping request to database
err = _sql.Ping()
if err != nil {
// create the duration of time to sleep for before attempting to retry
duration := time.Duration(i+1) * time.Second
e.logger.Warnf("unable to ping %s database - retrying in %v", e.Driver(), duration)
// sleep for loop iteration in seconds
time.Sleep(duration)
continue
}
return nil
}
return fmt.Errorf("unable to successfully ping %s database", e.Driver())
}