refactor geoip (#1442)

- Introduce a new Client type to remove the global variables from the file
- Use the sync package to prevent race conditions with the cache and
  enabled flag
- Cache results for IPs, even if the result is nil

There are still data races around the client.Geo variable, but that can be
resolved in a future commit.
This commit is contained in:
Tim Cooper
2021-10-12 15:21:37 -05:00
committed by GitHub
parent 01b3489287
commit 12eb59f611
2 changed files with 48 additions and 44 deletions

View File

@@ -38,6 +38,8 @@ type Server struct {
// unregister requests from clients.
unregister chan uint // the ChatClient id
geoipClient *geoip.Client
}
// NewChat will return a new instance of the chat server.
@@ -51,6 +53,7 @@ func NewChat() *Server {
inbound: make(chan chatClientEvent),
unregister: make(chan uint),
maxSocketConnectionLimit: maximumConcurrentConnectionLimit,
geoipClient: geoip.NewClient(),
}
return server
@@ -117,7 +120,7 @@ func (s *Server) Addclient(conn *websocket.Conn, user *user.User, accessToken st
// Asynchronously, optionally, fetch GeoIP data.
go func(client *Client) {
client.Geo = geoip.GetGeoFromIP(ipAddress)
client.Geo = s.geoipClient.GetGeoFromIP(ipAddress)
}(client)
return client