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

# README

go-api-session

Handle session for API, supports rate limitting

1. 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)
  • Session payload: session can store extra data

2. Usage

Install

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

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.StartSession(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(), sessionValue, owner, "url1")
	if errSession == nil {
		//Valid api call
		//Next processing...
		//...
	} else {
		//Invalid api call
		log.Printf("Failed to update session: %v", errGet)
	}
	

Session payload

//Init session with extra data
session := NewAPISessionWithPayload("user1", map[string]any{
		"nickname": "Jaian",
		"oldTokens":      []string{"token1", "token2"},
	})
//... or set payload directly
session.SetPayload("nickname","Jaian")


//Retrieve old tokens from session by generic helper
knownTokens, ok := GetPayloadSlice[string](session, "oldTokens")//value: []string{"token1", "token2"}

//Retrieve nickname by type assertion
value := session.GetPayload("nickname")
nickname, ok := value.(string)


# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
GetOrCreatePayloadMap returns a map from session payload, if not exist, create a new map.
No description provided by the author
GetPayloadMap returns a map from session payload, if not exist, return nil.
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
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.
SetPayloadMap init a map in session payload.

# 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
No description provided by the author

# Interfaces

No description provided by the author