-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Abdulrahman-Tayara/feature/sticky-session
feature: implement sticky session strategy
- Loading branch information
Showing
10 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package strategy | ||
|
||
import ( | ||
"math/rand" | ||
"net/http" | ||
"tayara/go-lb/models" | ||
"time" | ||
) | ||
|
||
var ( | ||
defaultSessionName = "lbsession" | ||
defaultTTLSeconds = 300 | ||
) | ||
|
||
type StickySessionStrategy struct { | ||
cfg Configs | ||
|
||
servers []*models.Server | ||
} | ||
|
||
func NewStickySessionStrategy(cfg Configs) ILoadBalancerStrategy { | ||
if cfg.StickySessionCookieName == "" { | ||
cfg.StickySessionCookieName = defaultSessionName | ||
} | ||
if cfg.StickySessionTTLSeconds <= 0 { | ||
cfg.StickySessionTTLSeconds = defaultTTLSeconds | ||
} | ||
|
||
return &StickySessionStrategy{ | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func (s *StickySessionStrategy) Next(request *http.Request) *models.Server { | ||
cookie, err := request.Cookie(s.cfg.StickySessionCookieName) | ||
if err != nil || cookie.Value == "" { | ||
cookieValue := generateSessionID() | ||
cookie.Value = cookieValue | ||
request.AddCookie(&http.Cookie{ | ||
Name: s.cfg.StickySessionCookieName, | ||
Value: cookieValue, | ||
Expires: time.Now().Add(time.Second * time.Duration(s.cfg.StickySessionTTLSeconds)), | ||
HttpOnly: true, | ||
}) | ||
} | ||
|
||
return s.getServer(cookie.Value) | ||
} | ||
|
||
func (s *StickySessionStrategy) getServer(sessionId string) *models.Server { | ||
hash := hashSessionToInt(sessionId) | ||
index := hash % len(s.servers) | ||
return s.servers[index] | ||
} | ||
|
||
func hashSessionToInt(sessionId string) int { | ||
hash := 0 | ||
for _, char := range sessionId { | ||
hash += int(char) | ||
} | ||
return hash | ||
} | ||
|
||
func (*StickySessionStrategy) RequestServed(server *models.Server, request *http.Request) { | ||
} | ||
|
||
func (*StickySessionStrategy) UpdateServers(servers []*models.Server) { | ||
} | ||
|
||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
|
||
func generateSessionID() string { | ||
b := make([]rune, 20) | ||
for i := range b { | ||
b[i] = letterRunes[rand.Intn(len(letterRunes))] | ||
} | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package strategy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHasSessionToInt(t *testing.T) { | ||
session := "ABCDEFG" | ||
|
||
num := hashSessionToInt(session) | ||
|
||
assert.Equal(t, 476, num) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters