MCPcopy Create free account
hub / github.com/apache/kvrocks-controller / RedirectIfNotLeader

Function RedirectIfNotLeader

server/middleware/middleware.go:63–87  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

61}
62
63func RedirectIfNotLeader(c *gin.Context) {
64 storage, _ := c.MustGet(consts.ContextKeyStore).(*store.ClusterStore)
65 if storage.Leader() == "" {
66 c.JSON(http.StatusBadRequest, gin.H{"error": "no leader now, please retry later"})
67 c.Abort()
68 return
69 }
70
71 _, isRaftMode := storage.GetEngine().(*raft.Node)
72 // Raft engine will forward the request to the leader node under the hood,
73 // so we don't need to do the redirect.
74 if !storage.IsLeader() && !isRaftMode {
75 if !c.GetBool(consts.HeaderIsRedirect) {
76 c.Set(consts.HeaderIsRedirect, true)
77 peerAddr := helper.ExtractAddrFromSessionID(storage.Leader())
78 c.Redirect(http.StatusTemporaryRedirect, "http://"+peerAddr+c.Request.RequestURI)
79 c.Redirect(http.StatusTemporaryRedirect, "http://"+storage.Leader()+c.Request.RequestURI)
80 } else {
81 c.JSON(http.StatusBadRequest, gin.H{"error": "no leader now, please retry later"})
82 c.Abort()
83 }
84 return
85 }
86 c.Next()
87}
88
89func RequiredNamespace(c *gin.Context) {
90 s, _ := c.MustGet(consts.ContextKeyStore).(*store.ClusterStore)

Callers

nothing calls this directly

Calls 5

ExtractAddrFromSessionIDFunction · 0.92
GetEngineMethod · 0.80
IsLeaderMethod · 0.80
LeaderMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected