(dbUrl string, d time.Duration)
| 160 | } |
| 161 | |
| 162 | func CheckDbConnection(dbUrl string, d time.Duration) errors.Error { |
| 163 | ctx := context.Background() |
| 164 | |
| 165 | result := make(chan errors.Error, 1) |
| 166 | done := make(chan struct{}, 1) |
| 167 | go func() { |
| 168 | defer func() { |
| 169 | if err := recover(); err != nil { |
| 170 | result <- errors.Default.New(fmt.Sprintf("panic when checking db connection: %v", err)) |
| 171 | } |
| 172 | }() |
| 173 | db, err := MakeDbConnection(dbUrl, &gorm.Config{}) |
| 174 | if err != nil { |
| 175 | result <- errors.Convert(err) |
| 176 | return |
| 177 | } |
| 178 | if d > 0 { |
| 179 | var cancel context.CancelFunc |
| 180 | ctx, cancel = context.WithTimeout(context.Background(), d) |
| 181 | defer cancel() |
| 182 | } |
| 183 | if err := db.WithContext(ctx).Exec("SELECT 1").Error; err != nil { |
| 184 | done <- struct{}{} |
| 185 | } else { |
| 186 | result <- errors.Convert(err) |
| 187 | } |
| 188 | }() |
| 189 | select { |
| 190 | case <-time.After(d): |
| 191 | return errors.Default.New("timeout") |
| 192 | case <-done: |
| 193 | return nil |
| 194 | case err := <-result: |
| 195 | return err |
| 196 | } |
| 197 | } |
nothing calls this directly
no test coverage detected