My understanding is that you ought to take melatonin in small doses (0.3 or so) an hour or two before sleep in order to get tired, whereupon you sleep (normally, ish, unless you take too much and then you get crazy dreams or sleep paralysis). If I take it and go right to bed I always wake up still in the 'window' of its effect.
I was taking small doses of melatonin before go to sleep for a year or two. To help me begin to sleep. I have a tinnitus. Usually I have a deep sleep. I can get sleep, I can sleep all night without problems. it's only the first hour that can be problematic for me.
I stoped like half year, and I don't having problems to sleep.