net.yatomiya.nicherry.services.bbs.AccessIntervalManager.java Source code

Java tutorial

Introduction

Here is the source code for net.yatomiya.nicherry.services.bbs.AccessIntervalManager.java

Source

/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package net.yatomiya.nicherry.services.bbs;

import java.util.*;
import com.squareup.okhttp.*;

public class AccessIntervalManager {
    int interval;

    class HostData {
        String host;
        long lastAccessTime = -1;
    }

    Map<String, HostData> dataMap = new HashMap<>();

    public void waitInterval(Request request) {
        String host = request.url().getHost();
        HostData data;
        synchronized (this) {
            data = dataMap.get(host);
            if (data == null) {
                data = new HostData();
                data.host = host;

                dataMap.put(host, data);
            }
        }
        synchronized (data) {
            long current = System.currentTimeMillis();
            if (data.lastAccessTime >= 0) {
                long elapsed = current - data.lastAccessTime;
                long remain = interval - elapsed;
                if (remain > 0) {
                    try {
                        Thread.sleep(remain);
                    } catch (InterruptedException e) {
                    }
                }
                data.lastAccessTime = current;
            }
        }
    }
}

/**
 * Access is allowed by accessCountInPeriod times in timePeriod.
 * If exceeded, request would be waited until timePeriod passed.
 * getDispatcher().setMaxRequestsPerHost() must be 1.
 */
/*
public class AccessIntervalManager {
long timePeriod = 1000;
int accessCountInPeriod = 1;
class HostData {
    String host;
    List<Long> accessTimeList = new ArrayList<>();
}
Map<String, HostData> dataMap = new HashMap<>();
    
public void waitInterval(Request request) {
    String host = request.url().getHost();
    HostData data;
    synchronized (this) {
        data = dataMap.get(host);
        if (data == null) {
            data = new HostData();
            data.host = host;
    
            dataMap.put(host, data);
        }
    }
    synchronized (data) {
        long current = System.currentTimeMillis();
        List<Long> list = data.accessTimeList;
        if (list.size() >= accessCountInPeriod) {
            while (list.size() > accessCountInPeriod) {
                list.remove(0);
            }
    
            long elapsed = current - list.get(0);
            if (elapsed < timePeriod) {
                try {
                    Thread.sleep(timePeriod - elapsed);
                } catch (InterruptedException e) {
                }
            }
        }
        list.add(current);
    }
}
}
*/