Skip to content

Commit

Permalink
added feature logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Swetabh333 committed Oct 8, 2024
1 parent 21128a1 commit 7512d70
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ go.work
go.work.sum

# env file
.env
.env

#logFile
loadbalancer.log

#exefile
loaddistrix
27 changes: 16 additions & 11 deletions LoadDistrix.config.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"backend":[
{
"host":"backend1",
"url":"http://localhost:5000"
},
{
"host":"backend2",
"url":"https://webathon-journal.onrender.com"
}
]
}
"backend": [
{
"host": "backend1",
"url": "http://localhost:3000"
},
{
"host": "backend2",
"url": "http://localhost:3001"
},
{
"host": "backend3",
"url": "http://localhost:3002"
}
]
}

48 changes: 47 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ const (

var serverPool lib.ServerPool

// this function creates a log file if it does not already exist
func InitLogger() (*os.File, error) {
logFile, err := os.OpenFile("loadbalancer.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return nil, err
}
log.SetOutput(logFile)
log.SetFlags(log.LstdFlags | log.Lshortfile)
return logFile, nil
}

// this function logs details about incoming requests
func LogRequest(r *http.Request) {
clientIp := r.RemoteAddr
method := r.Method
url := r.URL.String()
log.Printf("Received request from %s : %s, %s ", clientIp, method, url)
}

// this function logs which backend is selected
func LogBackendSelection(backendURL string) {
log.Printf("Routing request to backend: %s", backendURL)
}

// this function measures the time taken to process a request
func TrackresponseTime(start time.Time, backendURL string) {
duration := time.Since(start)
log.Printf("Request to backend %s took %v", backendURL, duration)
}

// this functions returns the retry count from the context
func GetRetryFromContext(r *http.Request) int {
if retry, ok := r.Context().Value(Retry).(int); ok {
Expand All @@ -42,20 +72,36 @@ func GetAttemptsFromContext(r *http.Request) int {
}

func lb(w http.ResponseWriter, r *http.Request) {
//log the request
LogRequest(r)

peer := serverPool.GetNextPeer()
attempts := GetAttemptsFromContext(r)
if attempts > 3 {
http.Error(w, "Service not available, max attempts reached", http.StatusServiceUnavailable)
return
}
if peer != nil {
LogBackendSelection(peer.URL.String())
startTime := time.Now()
peer.ReverseProxy.ServeHTTP(w, r)
// Log response time
TrackresponseTime(startTime, peer.URL.String())
return
}
http.Error(w, "Service not available", http.StatusServiceUnavailable)
}

func main() {

//Initialize logger
logfile, err := InitLogger()
if err != nil {
log.Fatalf("Error initializing logger: %v", err)
}

defer logfile.Close()

// get file name from argument
arg := os.Args
if len(arg) != 2 {
Expand All @@ -67,7 +113,7 @@ func main() {

// read the config file and get the host and url.
var config lib.Config
config, err := lib.ReadConfig(arg[1])
config, err = lib.ReadConfig(arg[1])
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 7512d70

Please sign in to comment.