RegisterEventSessionEnd with full functionality

@gupta-vaibhav You remember our design discussions well :slight_smile:

it should not include any heavy db queries that may interrupt with shutdown.

Yes exactly. The API for event signals is intentionally kept separate from access to the NakamaModule and database connection pool in its API signature to prevent misuse where developers might add logic which is not rate limited to session connect or disconnect events that cause an IO storm after server reboots, etc.

While the API is designed to prevent this kind of misuse in reality its still not hard to cheat the design to do what you need to do. You’d use a function closure to wrap a reference to the database connection handle and NakamaModule like so:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
    eventSessionEndFn := func(ctx2 context.Context, logger2 runtime.Logger, evt *api.Event) {
        // Take a reference to the *sql.DB pool.
        logger2.Info("%+v", db)
    }
    if err := initializer.RegisterEventSessionEnd(eventSessionEndFn); err != nil {
        return err
    }
    return nil
}

Please use the database connection logic with great care and attention in these cases. Also have a look at a library like ratelimit from Uber to help prevent overload scenarios in your code:

GitHub - uber-go/ratelimit: A Go blocking leaky-bucket rate limit implementation

Hope this helps.

2 Likes