BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Optimized database and Redis initialization with connection testing and retry mechanisms, and enhanced the domain DNS records retrieval logic to prevent empty records when cache is unavailable.
Jackb27b8733a28a
diff --git a/core/internal/service/database_initialization/database_initialization.go b/core/internal/service/database_initialization/database_initialization.goindex 1555b124..175a57ec 100644--- a/core/internal/service/database_initialization/database_initialization.go+++ b/core/internal/service/database_initialization/database_initialization.go@@ -3,8 +3,11 @@ package database_initializationimport ("billionmail-core/internal/consts""billionmail-core/internal/service/public"+ "context""fmt""github.com/gogf/gf/v2/database/gdb"+ "github.com/gogf/gf/v2/frame/g"+ "time")var registeredHandlers = make([]func(), 0, 256)@@ -37,6 +40,27 @@ func InitDatabase() (err error) {return fmt.Errorf("Set database configuration failed: %v", err)}+ // Testing database connection until successful+ connectionOK := false+ for i := 0; i < 10; i++ {+ _, err = g.DB().Exec(context.Background(), "SELECT 1")++ if err != nil {+ // Wait for 5 seconds before retrying+ g.Log().Debug(context.Background(), "Database connection failed, retrying in 5 seconds...")+ time.Sleep(time.Second * 5)+ continue+ }++ connectionOK = true+ g.Log().Debug(context.Background(), "Database connection successful")+ break+ }++ if !connectionOK {+ return fmt.Errorf("database connection test failed after 10 attempts")+ }+// Execute registered handlersfor _, handler := range registeredHandlers {if handler != nil {diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.goindex ddab55fb..35a9dcc5 100644--- a/core/internal/service/domains/domains.go+++ b/core/internal/service/domains/domains.go@@ -156,7 +156,7 @@ func FreshRecords(ctx context.Context, domain ...string) {// retrieve A recordgo func(d string) {defer wg.Done()- dr, err := GetARecord(d)+ dr, err := GetARecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get A record for domain %s: %v", d, err)} else {@@ -167,7 +167,7 @@ func FreshRecords(ctx context.Context, domain ...string) {// retrieve MX recordgo func(d string) {defer wg.Done()- dr, err := GetMXRecord(d)+ dr, err := GetMXRecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get MX record for domain %s: %v", d, err)} else {@@ -178,7 +178,7 @@ func FreshRecords(ctx context.Context, domain ...string) {// retrieve SPF recordgo func(d string) {defer wg.Done()- dr, err := GetSPFRecord(d)+ dr, err := GetSPFRecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get SPF record for domain %s: %v", d, err)} else {@@ -189,7 +189,7 @@ func FreshRecords(ctx context.Context, domain ...string) {// retrieve DKIM recordgo func(d string) {defer wg.Done()- dr, err := GetDKIMRecord(d)+ dr, err := GetDKIMRecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get DKIM record for domain %s: %v", d, err)} else {@@ -200,7 +200,7 @@ func FreshRecords(ctx context.Context, domain ...string) {// retrieve DMARC recordgo func(d string) {defer wg.Done()- dr, err := GetDMARCRecord(d)+ dr, err := GetDMARCRecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get DMARC record for domain %s: %v", d, err)} else {@@ -211,7 +211,7 @@ func FreshRecords(ctx context.Context, domain ...string) {//retrieve PTR recordgo func(d string) {defer wg.Done()- dr, err := GetPTRRecord(d)+ dr, err := GetPTRRecord(d, true)if err != nil {g.Log().Error(ctx, "Failed to get PTR record for domain %s: %v", d, err)} else {@@ -226,7 +226,7 @@ func FreshRecords(ctx context.Context, domain ...string) {}// GetDKIMRecord retrieves the DKIM record for a given domain.-func GetDKIMRecord(domain string) (record v1.DNSRecord, err error) {+func GetDKIMRecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {// Create DKIM directorydkimPath := public.AbsPath(filepath.Join(consts.RSPAMD_LIB_PATH, "dkim", domain))@@ -356,28 +356,32 @@ func GetDKIMRecord(domain string) (record v1.DNSRecord, err error) {Value: dkimRecord,}- // Validate the DKIM record- record.Valid = ValidateTXTRecord(record, domain)+ if validateImmediate {+ // Validate the DKIM record+ record.Valid = ValidateTXTRecord(record, domain)+ }return}// GetDMARCRecord retrieves the DMARC record for a given domain.-func GetDMARCRecord(domain string) (record v1.DNSRecord, err error) {+func GetDMARCRecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {record = v1.DNSRecord{Type: "TXT",Host: "_dmarc",Value: fmt.Sprintf("v=DMARC1;p=quarantine;rua=mailto:admin@%s", domain),}- // Validate the DMARC record- record.Valid = ValidateTXTRecord(record, domain)+ if validateImmediate {+ // Validate the DMARC record+ record.Valid = ValidateTXTRecord(record, domain)+ }return}// GetSPFRecord retrieves the SPF record for a given domain.-func GetSPFRecord(domain string) (record v1.DNSRecord, err error) {+func GetSPFRecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {serverIP, err := public.GetServerIP()if err != nil {@@ -398,28 +402,32 @@ func GetSPFRecord(domain string) (record v1.DNSRecord, err error) {Value: fmt.Sprintf("v=spf1 +a +mx +%s:%s -all", ipType, serverIP),}- // Validate the SPF record- record.Valid = ValidateTXTRecord(record, domain)+ if validateImmediate {+ // Validate the SPF record+ record.Valid = ValidateTXTRecord(record, domain)+ }return}// GetMXRecord retrieves the MX record for a given domain.-func GetMXRecord(domain string) (record v1.DNSRecord, err error) {+func GetMXRecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {record = v1.DNSRecord{Type: "MX",Host: "@",Value: "mail." + domain,}- // Validate the MX record- record.Valid = ValidateMXRecord(record, domain)+ if validateImmediate {+ // Validate the MX record+ record.Valid = ValidateMXRecord(record, domain)+ }return}// GetARecord retrieves the A record for a given domain.-func GetARecord(domain string) (record v1.DNSRecord, err error) {+func GetARecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {serverIP, err := public.GetServerIP()if err != nil {@@ -439,14 +447,16 @@ func GetARecord(domain string) (record v1.DNSRecord, err error) {Value: serverIP,}- // Validate the A record- record.Valid = ValidateARecord(record)+ if validateImmediate {+ // Validate the A record+ record.Valid = ValidateARecord(record)+ }return}// GetPTRRecord retrieves the PTR record for a given domain.-func GetPTRRecord(domain string) (record v1.DNSRecord, err error) {+func GetPTRRecord(domain string, validateImmediate bool) (record v1.DNSRecord, err error) {serverIP, err := public.GetServerIP()if err != nil {@@ -460,8 +470,10 @@ func GetPTRRecord(domain string) (record v1.DNSRecord, err error) {Value: "mail." + domain,}- // Validate the PTR record- record.Valid = ValidatePTRRecord(record)+ if validateImmediate {+ // Validate the PTR record+ record.Valid = ValidatePTRRecord(record)+ }return}@@ -475,6 +487,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := aRecord.(v1.DNSRecord); ok {records.A = v}+ } else {+ records.A, _ = GetARecord(domain, false)}// Get MX record from cache@@ -484,6 +498,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := mxRecord.(v1.DNSRecord); ok {records.MX = v}+ } else {+ records.MX, _ = GetMXRecord(domain, false)}// Get SPF record from cache@@ -493,6 +509,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := spfRecord.(v1.DNSRecord); ok {records.SPF = v}+ } else {+ records.SPF, _ = GetSPFRecord(domain, false)}// Get DKIM record from cache@@ -502,6 +520,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := dkimRecord.(v1.DNSRecord); ok {records.DKIM = v}+ } else {+ records.DKIM, _ = GetDKIMRecord(domain, false)}// Get DMARC record from cache@@ -511,6 +531,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := dmarcRecord.(v1.DNSRecord); ok {records.DMARC = v}+ } else {+ records.DMARC, _ = GetDMARCRecord(domain, false)}// Get PTR record from cache@@ -520,6 +542,8 @@ func GetRecordsInCache(domain string) (records v1.DNSRecords) {if v, ok := ptrRecord.(v1.DNSRecord); ok {records.PTR = v}+ } else {+ records.PTR, _ = GetPTRRecord(domain, false)}returndiff --git a/core/internal/service/redis_initialization/redis_initialization.go b/core/internal/service/redis_initialization/redis_initialization.goindex 86ebb9a2..0024466a 100644--- a/core/internal/service/redis_initialization/redis_initialization.go+++ b/core/internal/service/redis_initialization/redis_initialization.go@@ -2,8 +2,11 @@ package redis_initializationimport ("billionmail-core/internal/service/public"+ "context""fmt""github.com/gogf/gf/v2/database/gredis"+ "github.com/gogf/gf/v2/frame/g"+ "time")// InitRedis initialize redis configuration@@ -28,5 +31,30 @@ func InitRedis() (err error) {Pass: passwd,})+ // Testing redis connection until it successful+ connectionOK := false+ for i := 0; i < 10; i++ {+ k := "bm_test_connection"+ if err := g.Redis().SetEX(context.Background(), k, k, 1); err != nil {+ g.Log().Error(context.Background(), "Redis connection test failed: ", err, " Waiting for 5 seconds before retrying...")+ time.Sleep(5 * time.Second)+ continue+ }++ if _, err := g.Redis().Del(context.Background(), k); err != nil {+ g.Log().Error(context.Background(), "Redis connection test failed: ", err, " Waiting for 5 seconds before retrying...")+ time.Sleep(5 * time.Second)+ continue+ }++ connectionOK = true+ g.Log().Debug(context.Background(), "Redis connection test successful")+ break+ }++ if !connectionOK {+ return fmt.Errorf("redis connection test failed after 10 attempts")+ }+return nil}diff --git a/core/internal/service/rspamd/worker_controller.go b/core/internal/service/rspamd/worker_controller.goindex a6975dd6..c33756cf 100644--- a/core/internal/service/rspamd/worker_controller.go+++ b/core/internal/service/rspamd/worker_controller.go@@ -7,7 +7,6 @@ import ("context""errors""github.com/gogf/gf/util/grand"- "github.com/gogf/gf/v2/frame/g""path/filepath""strings")@@ -55,10 +54,7 @@ static_dir = "${WWWDIR}";`)}// Save the password- _, err = g.DB().Model("bm_options").OnConflict("name").OnDuplicate("value").Save(g.Map{- "name": "rspamd_worker_controller_password",- "value": passwordPlain,- })+ err = public.OptionsMgrInstance.SetOption(context.Background(), "rspamd_worker_controller_password", passwordPlain)if err != nil {return