comparison src/goodjava/webserver/handlers/DomainHandler.java @ 1601:de5a81327d9c

fix deadlock
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 22:39:03 -0600
parents 1b809d2fdf03
children 8df0b80e715e
comparison
equal deleted inserted replaced
1600:8a717f0b7f45 1601:de5a81327d9c
1 package goodjava.webserver.handlers; 1 package goodjava.webserver.handlers;
2 2
3 import java.io.Closeable; 3 import java.io.Closeable;
4 import java.io.IOException; 4 import java.io.IOException;
5 import java.util.Map; 5 import java.util.Map;
6 import java.util.HashMap;
7 import java.util.Set; 6 import java.util.Set;
7 import java.util.Collections;
8 import java.util.Timer; 8 import java.util.Timer;
9 import java.util.TimerTask; 9 import java.util.TimerTask;
10 import java.util.concurrent.ConcurrentHashMap; 10 import java.util.concurrent.ConcurrentHashMap;
11 import goodjava.util.SoftCacheMap; 11 import goodjava.util.SoftCacheMap;
12 import goodjava.logging.Logger; 12 import goodjava.logging.Logger;
35 logger.info("dontGc.clear()"); 35 logger.info("dontGc.clear()");
36 } 36 }
37 } 37 }
38 38
39 private static final long HOUR = 1000L*60*60; 39 private static final long HOUR = 1000L*60*60;
40 private final Map<String,Handler> map = new SoftCacheMap<String,Handler>(); 40 private final Map<String,Handler> map = Collections.synchronizedMap(new SoftCacheMap<String,Handler>());
41 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet(); 41 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
42 private final Timer timer = new Timer(); 42 private final Timer timer = new Timer();
43 43
44 private final Factory factory; 44 private final Factory factory;
45 45
64 dontGc.add(handler); 64 dontGc.add(handler);
65 return handler.handle(request); 65 return handler.handle(request);
66 } 66 }
67 67
68 public Handler getHandler(String domain) { 68 public Handler getHandler(String domain) {
69 domain = domain.toLowerCase(); 69 domain = domain.toLowerCase().intern();
70 synchronized(map) { 70 synchronized(domain) {
71 Handler handler = map.get(domain); 71 Handler handler = map.get(domain);
72 if( handler == null ) { 72 if( handler == null ) {
73 //if(ref!=null) logger.info("gc "+domain); 73 //if(ref!=null) logger.info("gc "+domain);
74 handler = factory.newHandler(domain); 74 handler = factory.newHandler(domain);
75 if( handler == null ) 75 if( handler == null )
80 } 80 }
81 } 81 }
82 82
83 public void removeHandler(String domain) { 83 public void removeHandler(String domain) {
84 //logger.info("removeHandler "+domain); 84 //logger.info("removeHandler "+domain);
85 domain = domain.toLowerCase(); 85 domain = domain.toLowerCase().intern();
86 synchronized(map) { 86 synchronized(domain) {
87 Handler handler = map.remove(domain); 87 Handler handler = map.remove(domain);
88 if( handler != null ) { 88 if( handler != null ) {
89 close(handler); 89 close(handler);
90 } 90 }
91 } 91 }