package middleware

import (
	"fmt"
	"log/slog"
	"slgserver/net"
	"time"
)

func ElapsedTime() net.MiddlewareFunc {
	return func(next net.HandlerFunc) net.HandlerFunc {
		return func(req *net.WsMsgReq, rsp *net.WsMsgRsp) {
			bt := time.Now().UnixNano()
			next(req, rsp)
			et := time.Now().UnixNano()
			diff := (et - bt)/int64(time.Millisecond)

		slog.Info("ElapsedTime:",
			"msgName", req.Body.Name,
			"cost", fmt.Sprintf("%dms", diff))
		}
	}
}