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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user