Categorygithub.com/zeroboo/go-api-session
modulepackage
0.0.7
Repository: https://github.com/zeroboo/go-api-session.git
Documentation: pkg.go.dev

# README

go-api-session

Handle session for API, supports rate limitting

Features

  • Validating session
  • Rate limiting
    • request too frequently (HTTP code 429 Too Many Requests) using Fixed Window algorithm
    • request too fast (HTTP code 425 Too Early)

Usage

Install

go get github.com/zeroboo/go-api-session

Sample

Setup

	//Create session manager
	sessionManager := apisession.NewRedisSessionManager(client,
		"session", //keys will have format of `session:<sessionId>``
		86400000,  //session last for 1 day
		60000,     //Time window is 1 minute
		10,        //Max 10 calls per minute
		1000)      //2 calls must be at least 1 second apart

	owner := "user1"

Create new session

	//User starts a session: create new
	sessionId, errGet := sessionManager.CreateNewSession(context.TODO(), owner)
	if errGet != nil {
		log.Printf("Failed to get session: %v", errGet)
	}
	//...

Use session to verify API calls

	//...
	//Update in api call
	session, errSession := sessionManager.RecordAPICall(context.TODO(), sessionId, owner, "url1")
	if errSession == nil {
		//Valid api call
		//Next processing...
		//...
	} else {
		//Invalid api call
		log.Printf("Failed to update session: %v", errGet)
	}
	

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
returns sha256 hash of the value.
No description provided by the author
No description provided by the author
Create redis session manager Params: - redisClient: redis client - sessionKeyPrefix: prefix for session key - sessionTTL: session time to live in milliseconds - windowSize: time window in milliseconds - maxCallPerWindow: max calls allowed per window - minRequestInterval: minimum milliseconds between 2 request, 0 mean no limit.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Tracks how an api is being called.
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author