Code Monkey home page Code Monkey logo

grails-browser-detection's People

Contributors

benorama avatar burtbeckwith avatar mathifonseca avatar nolatechhelp avatar nvinet avatar rgoldberg avatar sagrawal31 avatar sbglasius avatar timbonicus avatar vishesheltropy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grails-browser-detection's Issues

Nesting

Hi, Is it possible to nest these?

EG:
< browser:choice>
< browser:isNotWindowsMobile>
< browser:isMsie versionLower="9">
...
</ browser:isMsie>
</ browser:isNotWindowsMobile>
</ browser:choice>

UserAgentIdentService.getUserAgent() creates tons of sessions

  • Grails 3.3.9
  • browser-detection plugin 3.4.0
  • Jetty

Our application has many instances and many requests per instance in parallel, mostly unique users. Our application is stateless, users are not guaranteed to land on the same application instance next time, there is no session-syncing between the instances.

Our load and performance tests showed after migrating from Grails 2, that there are tons of sessions now. While the specific plugin code did not change since grails 2, we didn't make to configure the session timeouts properly under Grails 3 so far, so the problem became visible.

What the code in question does:

getUserAgent() wants to check if there is already cached user agent info in the session. Probably to safe a call to safe a call of parseUserAgent().
For this it calls
request.session.getAttribute(AGENT_INFO_TOKEN)
which leads to a call of
request.getSession()
which leads to a call of
request.getSession(true)
which creates a session, if there is none.

In our scenario about 90% of the requests don't have a session at this point, unless browser-detection created one in a further call or request.

If there was no user agent session information (or in the very rare cases, where user agent string changed while the session), getUserAgent() parses the user agent string and ads user agent infos to the (newly created) session.

In our scenario it’s much more expensive to create a session for any unique user on any instance, then just parsing the user agent string on any request.

To cache the agent info for repeated calls of getUserAgent() within a single request, it would be better to store it in the request scope (request.setAttribute(AGENT_INFO_TOKEN, userAgent)).

	UserAgent getUserAgent() {

		String userAgentString = getUserAgentString()
		UserAgent userAgent = request.session.getAttribute(AGENT_INFO_TOKEN)

		// returns cached instance
		if (userAgent && userAgent.userAgentString == userAgentString) {
			return userAgent
		}

		if (userAgent && userAgent.userAgentString != userAgent) {
			log.warn "User agent string has changed in a single session!"
			log.warn "Previous User Agent: $userAgent.userAgentString"
			log.warn "New User Agent: $userAgentString"
			log.warn "Discarding existing agent info and creating new..."
		}
		else {
			log.debug "User agent info does not exist in session scope, creating..."
		}

		// fallback for users without user-agent header
		if (userAgentString == null) {
			log.warn "User agent header is not set"
			userAgentString = ""
		}

		userAgent = parseUserAgent(userAgentString)

		request.session.setAttribute(AGENT_INFO_TOKEN, userAgent)
		return userAgent
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.