Skip to content

random int #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions go/isuports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -99,28 +100,7 @@ func createTenantDB(id int64) error {

// システム全体で一意なIDを生成する
func dispenseID(ctx context.Context) (string, error) {
var id int64
var lastErr error
for i := 0; i < 100; i++ {
var ret sql.Result
ret, err := adminDB.ExecContext(ctx, "INSERT INTO id_generator (stub) VALUES (?);", "a")
if err != nil {
if merr, ok := err.(*mysql.MySQLError); ok && merr.Number == 1213 { // deadlock
lastErr = fmt.Errorf("error INSERT INTO id_generator: %w", err)
continue
}
return "", fmt.Errorf("error INSERT INTO id_generator: %w", err)
}
id, err = ret.LastInsertId()
if err != nil {
return "", fmt.Errorf("error ret.LastInsertId: %w", err)
}
break
}
if id != 0 {
return fmt.Sprintf("%x", id), nil
}
return "", lastErr
return fmt.Sprintf("%x", rand.Int63()), nil
}

// 全APIにCache-Control: privateを設定する
Expand Down
2 changes: 1 addition & 1 deletion sql/init.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DELETE FROM tenant WHERE id > 100;
DELETE FROM visit_history WHERE created_at >= '1654041600';
UPDATE id_generator SET id=2678400000 WHERE stub='a';
DELETE FROM id_generator;
ALTER TABLE id_generator AUTO_INCREMENT=2678400000;