Open corop opened 2 years ago
package main
import (
"fmt"
"log"
"net/http"
"time"
)
// InsertIntoDB attempts to insert data into the database and retries upon failure.
func InsertIntoDB(id int, name string) error {
// Define the retry parameters
maxRetries := 3
retryDelay := 2 * time.Second
// Data to be inserted
data := fmt.Sprintf("INSERT INTO gc.test (id, name) VALUES (%d, '%s')", id, name)
url := fmt.Sprintf("http://10.0.10.141:8123")
// Attempt to insert data with retries
for attempt := 0; attempt < maxRetries; attempt++ {
if err := sendData(url, data); err != nil {
log.Printf("Attempt %d failed: %v", attempt+1, err)
// Wait before retrying
time.Sleep(retryDelay)
continue
}
// Success
log.Printf("Data successfully sent to %s", url)
return nil
}
return fmt.Errorf("failed to insert data after %d attempts", maxRetries)
}
// sendData makes an HTTP request to send data to the database.
// This is a placeholder function. You should replace it with your actual HTTP request logic.
func sendData(url, data string) error {
// Example: Simulate an HTTP request
response, err := http.Post(url, "application/sql", nil) // Replace nil with actual data body
if err != nil || response.StatusCode != http.StatusOK {
// Simulate a database error for demonstration
return fmt.Errorf("database error or authentication failed")
}
return nil
}
func main() {
// Example usage
if err := InsertIntoDB(8, "xcvbx"); err != nil {
log.Fatalf("Error: %v", err)
}
}
Periodicaly not found or database, or auth error
-- auto-generated definition create table test ( id Int32, name String ) engine = MergeTree PARTITION BY id PRIMARY KEY id ORDER BY (id, name) SETTINGS index_granularity = 8192;
⇨ http server started on [::]:8124 2021/09/10 21:26:10.503957 DEBUG: query INSERT INTO gc.test (id, name) VALUES (7, 'xcvbx') 2021/09/10 21:26:11.506905 INFO: sending 1 rows to http://10.0.10.141:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:11.521327 INFO: sent 1 rows to http://10.0.10.141:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:16.768973 DEBUG: query INSERT INTO gc.test (id, name) VALUES (8, 'xcvbx') 2021/09/10 21:26:17.504073 INFO: sending 1 rows to http://10.0.10.142:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:17.517043 INFO: sent 1 rows to http://10.0.10.142:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:17.517161 ERROR: Send (500) Wrong server status 500: response: Code: 516, e.displayText() = DB::Exception: chtdidx: Authentication failed: password is incorrect or there is no user with such name (version 21.2.2.8 (official build))
request: "INSERT INTO gc.test (id, name) VALUES\n(8, 'xcvbx')"; response Code: 516, e.displayText() = DB::Exception: chtdidx: Authentication failed: password is incorrect or there is no user with such name (version 21.2.2.8 (official build))
2021/09/10 21:26:19.228692 DEBUG: query INSERT INTO gc.test (id, name) VALUES (8, 'xcvbx') 2021/09/10 21:26:19.508245 INFO: sending 1 rows to http://10.0.10.143:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:19.522896 INFO: sent 1 rows to http://10.0.10.143:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:19.523012 ERROR: Send (500) Wrong server status 500: response: Code: 516, e.displayText() = DB::Exception: chtdidx: Authentication failed: password is incorrect or there is no user with such name (version 21.2.2.8 (official build))
request: "INSERT INTO gc.test (id, name) VALUES\n(8, 'xcvbx')"; response Code: 516, e.displayText() = DB::Exception: chtdidx: Authentication failed: password is incorrect or there is no user with such name (version 21.2.2.8 (official build))
2021/09/10 21:26:22.539692 DEBUG: query INSERT INTO gc.test (id, name) VALUES (8, 'xcvbx') 2021/09/10 21:26:23.503982 INFO: sending 1 rows to http://10.0.10.141:8123 of INSERT INTO gc.test (id, name) VALUES 2021/09/10 21:26:23.531772 INFO: sent 1 rows to http://10.0.10.141:8123 of INSERT INTO gc.test (id, name) VALUES