@@ -99,88 +99,97 @@ func ResolveAction(c *cli.Context) error {
9999// Resolve save the resolved IP addresses + their countries in a transaction
100100func resolve (ctx context.Context , dbh * sql.DB , mmc * maxmind.Client , uclient * udger.Client , dbmaddrs models.MultiAddressSlice ) error {
101101 log .WithField ("size" , len (dbmaddrs )).Infoln ("Resolving batch of multi addresses..." )
102+
103+ for _ , dbmaddr := range dbmaddrs {
104+ if err := resolveAddr (ctx , dbh , mmc , uclient , dbmaddr ); err != nil {
105+ log .WithField ("maddr" , dbmaddr .Maddr ).WithError (err ).Warnln ("Error resolving multi address" )
106+ }
107+ }
108+
109+ return nil
110+ }
111+
112+ func resolveAddr (ctx context.Context , dbh * sql.DB , mmc * maxmind.Client , uclient * udger.Client , dbmaddr * models.MultiAddress ) error {
113+ logEntry := log .WithField ("maddr" , dbmaddr .Maddr )
102114 txn , err := dbh .BeginTx (ctx , nil )
103115 if err != nil {
104116 return errors .Wrap (err , "begin txn" )
105117 }
106118 defer db .Rollback (txn )
107119
108- for _ , dbmaddr := range dbmaddrs {
109- logEntry := log .WithField ("maddr" , dbmaddr .Maddr )
110-
111- maddr , err := ma .NewMultiaddr (dbmaddr .Maddr )
112- if err != nil {
113- logEntry .WithError (err ).Warnln ("Error parsing multi address - deleting row" )
114- if _ , err = dbmaddr .Delete (ctx , txn ); err != nil {
115- logEntry .WithError (err ).Warnln ("Error deleting multi address" )
116- }
117- continue
120+ maddr , err := ma .NewMultiaddr (dbmaddr .Maddr )
121+ if err != nil {
122+ logEntry .WithError (err ).Warnln ("Error parsing multi address - deleting row" )
123+ if _ , err = dbmaddr .Delete (ctx , txn ); err != nil {
124+ logEntry .WithError (err ).Warnln ("Error deleting multi address" )
118125 }
126+ return errors .Wrap (err , "parse multi address" )
127+ }
119128
120- dbmaddr .Resolved = true
121- dbmaddr .IsPublic = null .BoolFrom (manet .IsPublicAddr (maddr ))
122- dbmaddr .IsRelay = null .BoolFrom (isRelayedMaddr (maddr ))
129+ dbmaddr .Resolved = true
130+ dbmaddr .IsPublic = null .BoolFrom (manet .IsPublicAddr (maddr ))
131+ dbmaddr .IsRelay = null .BoolFrom (isRelayedMaddr (maddr ))
123132
124- addrInfos , err := mmc .MaddrInfo (ctx , maddr )
125- if err != nil {
126- logEntry .WithError (err ).Warnln ("Error deriving address information from maddr " , maddr )
127- }
133+ addrInfos , err := mmc .MaddrInfo (ctx , maddr )
134+ if err != nil {
135+ logEntry .WithError (err ).Warnln ("Error deriving address information from maddr " , maddr )
136+ }
128137
129- if len (addrInfos ) == 0 {
130- dbmaddr .HasManyAddrs = null .BoolFrom (false )
131- } else if len (addrInfos ) == 1 {
132- dbmaddr .HasManyAddrs = null .BoolFrom (false )
133- var addr string
134- var addrInfo * maxmind.AddrInfo
135- for k , v := range addrInfos {
136- addr , addrInfo = k , v
137- break
138+ if len (addrInfos ) == 0 {
139+ dbmaddr .HasManyAddrs = null .BoolFrom (false )
140+ } else if len (addrInfos ) == 1 {
141+ dbmaddr .HasManyAddrs = null .BoolFrom (false )
142+ var addr string
143+ var addrInfo * maxmind.AddrInfo
144+ for k , v := range addrInfos {
145+ addr , addrInfo = k , v
146+ break
147+ }
148+ dbmaddr .Asn = null .NewInt (int (addrInfo .ASN ), addrInfo .ASN != 0 )
149+ dbmaddr .Country = null .NewString (addrInfo .Country , addrInfo .Country != "" )
150+ dbmaddr .Continent = null .NewString (addrInfo .Continent , addrInfo .Continent != "" )
151+ dbmaddr .Addr = null .NewString (addr , addr != "" )
152+
153+ if uclient != nil {
154+ datacenterID , err := uclient .Datacenter (addr )
155+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
156+ logEntry .WithError (err ).WithField ("addr" , addr ).Warnln ("Error resolving ip address to datacenter" )
138157 }
139- dbmaddr .Asn = null .NewInt (int (addrInfo .ASN ), addrInfo .ASN != 0 )
140- dbmaddr .Country = null .NewString (addrInfo .Country , addrInfo .Country != "" )
141- dbmaddr .Continent = null .NewString (addrInfo .Continent , addrInfo .Continent != "" )
142- dbmaddr .Addr = null .NewString (addr , addr != "" )
158+ dbmaddr .IsCloud = null .NewInt (datacenterID , datacenterID != 0 )
159+ }
160+ } else if len (addrInfos ) > 1 { // not "else" because the MaddrInfo could have failed and we still want to update the maddr
161+ dbmaddr .HasManyAddrs = null .BoolFrom (true )
162+ // Due to dnsaddr protocols each multi address can point to multiple
163+ // IP addresses each in a different country.
164+ for addr , addrInfo := range addrInfos {
143165
166+ datacenterID := 0
144167 if uclient != nil {
145- datacenterID , err : = uclient .Datacenter (addr )
168+ datacenterID , err = uclient .Datacenter (addr )
146169 if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
147170 logEntry .WithError (err ).WithField ("addr" , addr ).Warnln ("Error resolving ip address to datacenter" )
171+ } else if datacenterID > 0 {
172+ dbmaddr .IsCloud = null .IntFrom (datacenterID )
148173 }
149- dbmaddr .IsCloud = null .NewInt (datacenterID , datacenterID != 0 )
150174 }
151- } else if len (addrInfos ) > 1 { // not "else" because the MaddrInfo could have failed and we still want to update the maddr
152- dbmaddr .HasManyAddrs = null .BoolFrom (true )
153- // Due to dnsaddr protocols each multi address can point to multiple
154- // IP addresses each in a different country.
155- for addr , addrInfo := range addrInfos {
156-
157- datacenterID := 0
158- if uclient != nil {
159- datacenterID , err = uclient .Datacenter (addr )
160- if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
161- logEntry .WithError (err ).WithField ("addr" , addr ).Warnln ("Error resolving ip address to datacenter" )
162- } else if datacenterID > 0 {
163- dbmaddr .IsCloud = null .IntFrom (datacenterID )
164- }
165- }
166175
167- // Save the IP address + country information + asn information
168- ipaddr := & models.IPAddress {
169- Asn : null .NewInt (int (addrInfo .ASN ), addrInfo .ASN != 0 ),
170- IsCloud : null .NewInt (datacenterID , datacenterID != 0 ),
171- Country : null .NewString (addrInfo .Country , addrInfo .Country != "" ),
172- Continent : null .NewString (addrInfo .Continent , addrInfo .Continent != "" ),
173- Address : addr ,
174- }
175- if err := dbmaddr .AddIPAddresses (ctx , txn , true , ipaddr ); err != nil {
176- logEntry .WithError (err ).WithField ("addr" , ipaddr .Address ).Warnln ("Could not insert ip address" )
177- }
176+ // Save the IP address + country information + asn information
177+ ipaddr := & models.IPAddress {
178+ Asn : null .NewInt (int (addrInfo .ASN ), addrInfo .ASN != 0 ),
179+ IsCloud : null .NewInt (datacenterID , datacenterID != 0 ),
180+ Country : null .NewString (addrInfo .Country , addrInfo .Country != "" ),
181+ Continent : null .NewString (addrInfo .Continent , addrInfo .Continent != "" ),
182+ Address : addr ,
183+ }
184+ if err := dbmaddr .AddIPAddresses (ctx , txn , true , ipaddr ); err != nil {
185+ logEntry .WithError (err ).WithField ("addr" , ipaddr .Address ).Warnln ("Could not insert ip address" )
186+ return errors . Wrap ( err , "add ip addresses" )
178187 }
179188 }
180- if _ , err = dbmaddr . Update ( ctx , txn , boil . Infer ()); err != nil {
181- logEntry . WithError ( err ). Warnln ( "Could not update multi address" )
182- continue
183- }
189+ }
190+ if _ , err = dbmaddr . Update ( ctx , txn , boil . Infer ()); err != nil {
191+ logEntry . WithError ( err ). Warnln ( "Could not update multi address" )
192+ return errors . Wrap ( err , "update multi address" )
184193 }
185194
186195 return txn .Commit ()
0 commit comments