Much imporoved websocket connection closure will now detect closing of
websockets immediately.
This commit is contained in:
		
							parent
							
								
									cb03b79630
								
							
						
					
					
						commit
						bac7a9a6f1
					
				| @ -136,13 +136,13 @@ func main() { | |||||||
| 	sessionService := websocketutil.WebSocketService{ | 	sessionService := websocketutil.WebSocketService{ | ||||||
| 		Handler: func(w http.ResponseWriter, r *http.Request, conn net.Conn) { | 		Handler: func(w http.ResponseWriter, r *http.Request, conn net.Conn) { | ||||||
| 			ctx, cancel := context.WithCancel(context.Background()) | 			ctx, cancel := context.WithCancel(context.Background()) | ||||||
| 			websession := websessions.NewSession(conn, ctx) | 			websession := websessions.NewSession(conn, ctx, cancel) | ||||||
| 			defer websessions.SessionClosed(websession) | 			defer websessions.SessionClosed(websession) | ||||||
| 			location, err := matchmaker.GetUserLocation(r) | 			location, err := matchmaker.GetUserLocation(r) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				panic(err) | 				panic(err) | ||||||
| 			} | 			} | ||||||
| 			websession.WriteNotifications(location, cancel) | 			websession.WriteNotifications(location, ctx, cancel) | ||||||
| 		}, | 		}, | ||||||
| 		Text: true, | 		Text: true, | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -51,7 +51,8 @@ func (sessions *WebSessions) notifyWebSessions(notification *models.State) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Context) *WebSession { | func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Context, | ||||||
|  | 	cancel context.CancelFunc) *WebSession { | ||||||
| 	sessions.mutex.Lock() | 	sessions.mutex.Lock() | ||||||
| 	defer sessions.mutex.Unlock() | 	defer sessions.mutex.Unlock() | ||||||
| 	session := &WebSession{ | 	session := &WebSession{ | ||||||
| @ -70,6 +71,7 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte | |||||||
| 				session.notifications <- sessions.lastNotification | 				session.notifications <- sessions.lastNotification | ||||||
| 			} else { | 			} else { | ||||||
| 				log.Printf("Got error reading %v", err) | 				log.Printf("Got error reading %v", err) | ||||||
|  | 				cancel() | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| @ -84,19 +86,22 @@ func GetUserLocation(r *http.Request) (*time.Location, error) { | |||||||
| 	if tzName == "" { | 	if tzName == "" { | ||||||
| 		tzName = r.Header.Get("X-Timezone") | 		tzName = r.Header.Get("X-Timezone") | ||||||
| 	} | 	} | ||||||
|  | 	//log.Printf("Got timezone from request %v %v", tzName, r.URL.Path)
 | ||||||
| 	if tzName == "" { | 	if tzName == "" { | ||||||
| 		tzName = "UTC" | 		tzName = "UTC" | ||||||
| 	} | 	} | ||||||
| 	return time.LoadLocation(tzName) | 	return time.LoadLocation(tzName) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (session *WebSession) WriteNotifications(location *time.Location, cancel context.CancelFunc) { | func (session *WebSession) WriteNotifications(location *time.Location, ctx context.Context, cancel context.CancelFunc) { | ||||||
| 	timer := time.NewTicker(10 * time.Second) | 	timer := time.NewTicker(10 * time.Second) | ||||||
| 	defer timer.Stop() | 	defer timer.Stop() | ||||||
| 	// if for some reason we cannot send notifications to the web client then the context is canceled.
 | 	// if for some reason we cannot send notifications to the web client then the context is canceled.
 | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
| 	for { | 	for { | ||||||
| 		select { | 		select { | ||||||
|  | 		case <-ctx.Done(): | ||||||
|  | 			return | ||||||
| 		case notification, ok := <-session.notifications: | 		case notification, ok := <-session.notifications: | ||||||
| 			if !ok { | 			if !ok { | ||||||
| 				log.Println("channel closed") | 				log.Println("channel closed") | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user